#pragma GCC optimize("O3,unroll-loops") #pragma GCC target("bmi,bmi2,lzcnt,popcnt") #include <bits/stdc++.h> using namespace std; #define endl '\n' #define L long long #define MP make_pair #define REP(i, n) for(int i = 0; i < n; ++i) #define REPR(i, n) for(int i = n - 1; i >= 0; --i) #define FOR(i, a, b) for(int i = a; i < b; ++i) #define FORR(i, a, b) for(int i = b - 1; i >= a; --i) #define EB emplace_back #define ST first #define ND second #define S size #define RS resize template<class T> using P = pair<T, T>; template<class T> using V = vector<T>; const L M = (int)1e9 + 7; V<V<int>> g; V<int> c, col; bool bip; array<int, 2> sizes, zeros, ones; void dfs(int v) { for (int u : g[v]) { if (col[u] == -1) { { int tu = 1 - (col[v] & 1); sizes[tu]++; if (c[u] == 0) { zeros[tu]++; } else { ones[tu]++; } col[u] = col[v] + (tu == 1 ? 1 : -1); } dfs(u); } else if (col[u] == col[v]) { bip = false; } } } L power(L a, L b) { if (b == 0) { return 1LL; } if ((b & 1) == 0) { L s = power(a, b >> 1LL); L res = s * s; return res >= M ? res % M : res; } else { L s = power(a, b - 1LL); L res = s * a; return res >= M ? res % M : res; } } L binom(int n, int k) { L res = 1; FOR(i, n - k + 1, n + 1) { res = (res * (L)i) % M; } FOR(i, 2, k + 1) { res = (res * power(i, M - 2)) % M; } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; g.RS(n); c.RS(n); col.RS(n, -1); REP(i, n) { cin >> c[i]; } REP(i, m) { int a, b; cin >> a >> b; a--, b--; g[a].EB(b), g[b].EB(a); } V<L> pow2(n); pow2[0] = 1; FOR(i, 1, n) { pow2[i] = (2LL * pow2[i - 1]) % M; } L res = 1; REP(v, n) { if (col[v] == -1) { col[v] = 2 * v; bip = true; sizes[0] = 1; sizes[1] = 0; zeros[0] = (int)(c[v] == 0); zeros[1] = 0; ones[0] = (int)(c[v] == 1); ones[1] = 0; dfs(v); /*cout << bip << ' ' << sizes[0] << ' ' << sizes[1] << ' ' << zeros[0] << ' ' << zeros[1] << ' ' << ones[0] << ' ' << ones[1] << '\n'; REP(i, n) { cout << col[i] << ' '; } cout << '\n'*/; if (bip) { int l = sizes[0], p = sizes[1]; int a = ones[0], b = ones[1]; int u = min(l - a, p - b); int d = min(a, b); L bl = binom(l, a - d); L bp = binom(p, b - d); L r = (bl * bp) % M; //cout << bl << ' ' << bp << '\n'; FOR(i, 1, u + d + 1) { bl = (((bl * (L)(l - a + d - i + 1)) % M) * power(a - d + i, M - 2)) % M; bp = (((bp * (L)(p - b + d - i + 1)) % M) * power(b - d + i, M - 2)) % M; //cout << bl << ' ' << bp << '\n'; r = (r + bl * bp) % M; } res = (res * r) % M; } else { res = (res * pow2[sizes[0] + sizes[1] - 1]) % M; } } } cout << res << '\n'; }
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 | #pragma GCC optimize("O3,unroll-loops") #pragma GCC target("bmi,bmi2,lzcnt,popcnt") #include <bits/stdc++.h> using namespace std; #define endl '\n' #define L long long #define MP make_pair #define REP(i, n) for(int i = 0; i < n; ++i) #define REPR(i, n) for(int i = n - 1; i >= 0; --i) #define FOR(i, a, b) for(int i = a; i < b; ++i) #define FORR(i, a, b) for(int i = b - 1; i >= a; --i) #define EB emplace_back #define ST first #define ND second #define S size #define RS resize template<class T> using P = pair<T, T>; template<class T> using V = vector<T>; const L M = (int)1e9 + 7; V<V<int>> g; V<int> c, col; bool bip; array<int, 2> sizes, zeros, ones; void dfs(int v) { for (int u : g[v]) { if (col[u] == -1) { { int tu = 1 - (col[v] & 1); sizes[tu]++; if (c[u] == 0) { zeros[tu]++; } else { ones[tu]++; } col[u] = col[v] + (tu == 1 ? 1 : -1); } dfs(u); } else if (col[u] == col[v]) { bip = false; } } } L power(L a, L b) { if (b == 0) { return 1LL; } if ((b & 1) == 0) { L s = power(a, b >> 1LL); L res = s * s; return res >= M ? res % M : res; } else { L s = power(a, b - 1LL); L res = s * a; return res >= M ? res % M : res; } } L binom(int n, int k) { L res = 1; FOR(i, n - k + 1, n + 1) { res = (res * (L)i) % M; } FOR(i, 2, k + 1) { res = (res * power(i, M - 2)) % M; } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; g.RS(n); c.RS(n); col.RS(n, -1); REP(i, n) { cin >> c[i]; } REP(i, m) { int a, b; cin >> a >> b; a--, b--; g[a].EB(b), g[b].EB(a); } V<L> pow2(n); pow2[0] = 1; FOR(i, 1, n) { pow2[i] = (2LL * pow2[i - 1]) % M; } L res = 1; REP(v, n) { if (col[v] == -1) { col[v] = 2 * v; bip = true; sizes[0] = 1; sizes[1] = 0; zeros[0] = (int)(c[v] == 0); zeros[1] = 0; ones[0] = (int)(c[v] == 1); ones[1] = 0; dfs(v); /*cout << bip << ' ' << sizes[0] << ' ' << sizes[1] << ' ' << zeros[0] << ' ' << zeros[1] << ' ' << ones[0] << ' ' << ones[1] << '\n'; REP(i, n) { cout << col[i] << ' '; } cout << '\n'*/; if (bip) { int l = sizes[0], p = sizes[1]; int a = ones[0], b = ones[1]; int u = min(l - a, p - b); int d = min(a, b); L bl = binom(l, a - d); L bp = binom(p, b - d); L r = (bl * bp) % M; //cout << bl << ' ' << bp << '\n'; FOR(i, 1, u + d + 1) { bl = (((bl * (L)(l - a + d - i + 1)) % M) * power(a - d + i, M - 2)) % M; bp = (((bp * (L)(p - b + d - i + 1)) % M) * power(b - d + i, M - 2)) % M; //cout << bl << ' ' << bp << '\n'; r = (r + bl * bp) % M; } res = (res * r) % M; } else { res = (res * pow2[sizes[0] + sizes[1] - 1]) % M; } } } cout << res << '\n'; } |