#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> //#pragma GCC target ("avx2") //#pragma GCC optimize ("Ofast") //#pragma GCC optimize ("unroll-loops") #define f first #define s second #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) ((int) (x).size()) #define pb push_back #define mp make_pair #define int long long using namespace std; using namespace __gnu_pbds; template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template <typename T> inline bool umin(T &a, const T &b) { if(a > b) { a = b; return 1; } return 0; } template <typename T> inline bool umax(T &a, const T &b) { if(a < b) { a = b; return 1; } return 0; } typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const ll mod = 1e9 + 7; const ll base = 1e6 + 9; const ll inf = 1e18; const int MAX = 3e5 + 42; const int LG = 20; //random_device rd; //mt19937 gen(rd()); //uniform_int_distribution<ll> dis(1, inf); const int phi_minus_one = mod - 2; class Mint { public: int x; public: void norm() { x %= mod; if(x < 0) x += mod; } Mint(int a, bool small) { x = a; if(x >= mod) x -= mod; if(x < 0) x += mod; } Mint() { x = 0; } Mint(ll a) { x = a % mod; if(x < 0) x += mod; } friend ostream &operator <<(ostream &out, const Mint &a) { out << a.x; return out; } friend istream &operator >>(istream &in, Mint &a) { in >> a.x; return in; } Mint operator +(const Mint &b) const { return Mint(x + b.x, 1); } Mint operator +(int a) { return Mint(x + a, 1); } Mint operator -(const Mint &b) const { return Mint(x - b.x, 1); } Mint operator -(int a) { return Mint(x - a, 1); } friend Mint operator -(Mint a) { return Mint(mod - a); } Mint operator *(const Mint &b) const { return Mint(1LL * x * b.x); } Mint operator *(int a) { return Mint(1LL * x * a); } Mint& operator +=(const Mint &b) { x += b.x; if(x >= mod) x -= mod; return *this; } Mint& operator +=(int a) { x += a; if(x >= mod) x -= mod; return *this; } Mint& operator -=(Mint b) { x += mod - b.x; if(x >= mod) x -= mod; return *this; } Mint& operator -=(int a) { x += mod - a; if(x >= mod) x -= mod; return *this; } Mint& operator *=(Mint b) { x = (ll) x * b.x % mod; return *this; } Mint& operator *=(int a) { x = (ll) x * a % mod; return *this; } Mint& operator ++() { if(++x == mod) x = 0; return *this; } Mint bpow(ll n) { Mint a(x); Mint ans(1); while(n) { if(n & 1) ans *= a; n >>= 1; a *= a; } return ans; } Mint inv() { return bpow(phi_minus_one); } Mint operator /(Mint b) { return b.inv() * x; } Mint operator /(int a) { return Mint(a, 1).inv() * x; } friend Mint operator -(int a, Mint b) { Mint res(b - a); res.x = mod - res.x; if(res.x == mod) res.x = 0; return res; } friend Mint operator +(int a, Mint b) { return Mint(b + a); } friend Mint operator *(int a, Mint b) { return Mint(b * a); } friend Mint operator /(int a, Mint b) { return Mint(a, 1) * b.inv(); } Mint operator =(Mint b) { x = b.x; return b; } bool operator ==(int a) { return (x == a); } bool operator !=(int a) { return !(x == a); } friend bool operator ==(int a, Mint b) { return (b.x == a); } friend bool operator !=(int a, Mint b) { return b.x != a; } }; Mint fact[MAX], inv[MAX]; Mint C(int n, int k) { if(n < k || k < 0) return 0; return fact[n] * inv[k] * inv[n - k]; } int zero = 0; int one = 0; int cnt_one = 0; int cnt_zero = 0; int good = 1; int color[MAX]; vector<int> g[MAX]; int used[MAX]; int A[MAX]; void dfs(int v, int p = -1, int col = 0) { color[v] = col; zero += col ^ 1; one += col; if(A[v]) { cnt_zero += col ^ 1; cnt_one += col; } used[v] = 1; for(auto to : g[v]) { if(!used[to]) dfs(to, v, col ^ 1); else good &= color[to] != col; } } Mint get(vector<int> a, vector<pii> edges) { int n = sz(a); if(n == 1) return 1; for(int i = 0; i < n; i++) g[i].clear(); for(int i = 0; i < n; i++) used[i] = 0; for(int i = 0; i < n; i++) A[i] = a[i]; for(auto [u, v] : edges) { g[u].pb(v); g[v].pb(u); } Mint ans = 1; for(int i = 0; i < n; i++) { if(used[i]) continue; zero = one = cnt_zero = cnt_one = 0; good = 1; dfs(i); if(!good) ans *= Mint(2).bpow(one + zero - 1); else { Mint sum = 0; for(int i = 0; i < zero + one; i++) sum += C(zero, max(cnt_zero - cnt_one, 0ll) + i) * C(one, max(cnt_one - cnt_zero, 0ll) + i); ans *= sum; } } return ans; } /* 4 4 0 0 0 1 2 3 4 3 1 4 2 1 */ void solve() { int n, m; cin >> n >> m; vector<int> a(n); for(auto &i : a) { cin >> i; } vector<pii> edges(m); for(auto &[u, v] : edges) { cin >> u >> v; u--, v--; } Mint ans = get(a, edges); cout << ans << '\n'; } signed main() { fact[0] = 1; for(int i = 1; i < MAX; i++) fact[i] = fact[i - 1] * i; inv[MAX - 1] = 1 / fact[MAX - 1]; for(int i = MAX - 1; i; i--) inv[i - 1] = inv[i] * i; assert(inv[0] == 1); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int ttt = 1; // cin >> ttt; while(ttt--) { solve(); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> //#pragma GCC target ("avx2") //#pragma GCC optimize ("Ofast") //#pragma GCC optimize ("unroll-loops") #define f first #define s second #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) ((int) (x).size()) #define pb push_back #define mp make_pair #define int long long using namespace std; using namespace __gnu_pbds; template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template <typename T> inline bool umin(T &a, const T &b) { if(a > b) { a = b; return 1; } return 0; } template <typename T> inline bool umax(T &a, const T &b) { if(a < b) { a = b; return 1; } return 0; } typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const ll mod = 1e9 + 7; const ll base = 1e6 + 9; const ll inf = 1e18; const int MAX = 3e5 + 42; const int LG = 20; //random_device rd; //mt19937 gen(rd()); //uniform_int_distribution<ll> dis(1, inf); const int phi_minus_one = mod - 2; class Mint { public: int x; public: void norm() { x %= mod; if(x < 0) x += mod; } Mint(int a, bool small) { x = a; if(x >= mod) x -= mod; if(x < 0) x += mod; } Mint() { x = 0; } Mint(ll a) { x = a % mod; if(x < 0) x += mod; } friend ostream &operator <<(ostream &out, const Mint &a) { out << a.x; return out; } friend istream &operator >>(istream &in, Mint &a) { in >> a.x; return in; } Mint operator +(const Mint &b) const { return Mint(x + b.x, 1); } Mint operator +(int a) { return Mint(x + a, 1); } Mint operator -(const Mint &b) const { return Mint(x - b.x, 1); } Mint operator -(int a) { return Mint(x - a, 1); } friend Mint operator -(Mint a) { return Mint(mod - a); } Mint operator *(const Mint &b) const { return Mint(1LL * x * b.x); } Mint operator *(int a) { return Mint(1LL * x * a); } Mint& operator +=(const Mint &b) { x += b.x; if(x >= mod) x -= mod; return *this; } Mint& operator +=(int a) { x += a; if(x >= mod) x -= mod; return *this; } Mint& operator -=(Mint b) { x += mod - b.x; if(x >= mod) x -= mod; return *this; } Mint& operator -=(int a) { x += mod - a; if(x >= mod) x -= mod; return *this; } Mint& operator *=(Mint b) { x = (ll) x * b.x % mod; return *this; } Mint& operator *=(int a) { x = (ll) x * a % mod; return *this; } Mint& operator ++() { if(++x == mod) x = 0; return *this; } Mint bpow(ll n) { Mint a(x); Mint ans(1); while(n) { if(n & 1) ans *= a; n >>= 1; a *= a; } return ans; } Mint inv() { return bpow(phi_minus_one); } Mint operator /(Mint b) { return b.inv() * x; } Mint operator /(int a) { return Mint(a, 1).inv() * x; } friend Mint operator -(int a, Mint b) { Mint res(b - a); res.x = mod - res.x; if(res.x == mod) res.x = 0; return res; } friend Mint operator +(int a, Mint b) { return Mint(b + a); } friend Mint operator *(int a, Mint b) { return Mint(b * a); } friend Mint operator /(int a, Mint b) { return Mint(a, 1) * b.inv(); } Mint operator =(Mint b) { x = b.x; return b; } bool operator ==(int a) { return (x == a); } bool operator !=(int a) { return !(x == a); } friend bool operator ==(int a, Mint b) { return (b.x == a); } friend bool operator !=(int a, Mint b) { return b.x != a; } }; Mint fact[MAX], inv[MAX]; Mint C(int n, int k) { if(n < k || k < 0) return 0; return fact[n] * inv[k] * inv[n - k]; } int zero = 0; int one = 0; int cnt_one = 0; int cnt_zero = 0; int good = 1; int color[MAX]; vector<int> g[MAX]; int used[MAX]; int A[MAX]; void dfs(int v, int p = -1, int col = 0) { color[v] = col; zero += col ^ 1; one += col; if(A[v]) { cnt_zero += col ^ 1; cnt_one += col; } used[v] = 1; for(auto to : g[v]) { if(!used[to]) dfs(to, v, col ^ 1); else good &= color[to] != col; } } Mint get(vector<int> a, vector<pii> edges) { int n = sz(a); if(n == 1) return 1; for(int i = 0; i < n; i++) g[i].clear(); for(int i = 0; i < n; i++) used[i] = 0; for(int i = 0; i < n; i++) A[i] = a[i]; for(auto [u, v] : edges) { g[u].pb(v); g[v].pb(u); } Mint ans = 1; for(int i = 0; i < n; i++) { if(used[i]) continue; zero = one = cnt_zero = cnt_one = 0; good = 1; dfs(i); if(!good) ans *= Mint(2).bpow(one + zero - 1); else { Mint sum = 0; for(int i = 0; i < zero + one; i++) sum += C(zero, max(cnt_zero - cnt_one, 0ll) + i) * C(one, max(cnt_one - cnt_zero, 0ll) + i); ans *= sum; } } return ans; } /* 4 4 0 0 0 1 2 3 4 3 1 4 2 1 */ void solve() { int n, m; cin >> n >> m; vector<int> a(n); for(auto &i : a) { cin >> i; } vector<pii> edges(m); for(auto &[u, v] : edges) { cin >> u >> v; u--, v--; } Mint ans = get(a, edges); cout << ans << '\n'; } signed main() { fact[0] = 1; for(int i = 1; i < MAX; i++) fact[i] = fact[i - 1] * i; inv[MAX - 1] = 1 / fact[MAX - 1]; for(int i = MAX - 1; i; i--) inv[i - 1] = inv[i] * i; assert(inv[0] == 1); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int ttt = 1; // cin >> ttt; while(ttt--) { solve(); } } |