#pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = (a); i <= (b); i++) #define per(i, a, b) for (int i = (b); i >= (a); i--) #define SZ(x) ((int)x.size()) #define all(x) x.begin(), x.end() #define pb push_back #define mp make_pair #define mt make_tuple #define st first #define nd second using ll = long long; using vi = vector<int>; using pii = pair<int, int>; using pll = pair<ll, ll>; auto &operator<<(auto &o, pair<auto, auto> p) { return o << "(" << p.st << ", " << p.nd << ")"; } auto operator<<(auto &o, auto x)->decltype(end(x), o) { o << "{"; int i=0; for(auto e: x) o << ", " + 2*!i++ << e; return o << "}"; } #define deb(x...) cerr << "[" #x "]: ", [](auto...$) { ((cerr<<$<<"; "),...) << endl; }(x) const int nax = 1e6 + 5; const int mod = 1e9 + 7; vector<int> adj[nax]; int n, m; int a[nax]; int color[nax]; bool vis[nax]; bool bipartite; bool nei; int cnt[2][2]; ll pp(ll a, ll b){ ll ans = 1; while(b){ if(b & 1){ ans *= a; ans %= mod; } a *= a; a %= mod; b /= 2; } return ans; } ll inv(ll a){ return pp(a, mod - 2); } int f[nax]; int iw[nax]; void prep(){ f[0] = iw[0] = 1; rep(i, 1, nax - 1){ f[i] = (1LL * f[i - 1] * i % mod); iw[i] = inv(f[i]); } } ll binom(ll n, ll k){ ll ans = f[n]; ans *= iw[k]; ans %= mod; ans *= iw[n - k]; ans %= mod; return ans; } vector<int> now; void dfs(int v, int p){ now.pb(v); vis[v] = true; cnt[p][a[v]] += 1; color[v] = p; for(int x : adj[v]){ if(color[x] == color[v]) bipartite = false; if(a[x] == a[v]) nei = true; } for(int x : adj[v]){ if(!vis[x]) dfs(x, p ^ 1); } } void solve(){ cin >> n >> m; rep(i, 1, n) cin >> a[i]; rep(i, 1, m){ int x, y; cin >> x >> y; adj[x].pb(y); adj[y].pb(x); } rep(i, 1, n) color[i] = 2; ll ans = 1; rep(i, 1, n){ if(vis[i]) continue; bipartite = true; nei = false; cnt[0][0] = cnt[0][1] = cnt[1][0] = cnt[1][1] = 0; now.clear(); dfs(i, 0); if(!bipartite){ assert(nei); int tot = cnt[0][0] + cnt[0][1] + cnt[1][0] + cnt[1][1]; int ones = cnt[0][1] + cnt[1][1]; int zeros = tot - ones; ll akt = 0; for(int c=ones%2;c<=tot;c+=2){ akt += binom(tot, c); akt %= mod; } ans *= akt; ans %= mod; continue; } if(!nei) continue; ll akt = 0; int dif = cnt[0][1] - cnt[1][1]; rep(c, 0, cnt[0][1] + cnt[0][0]){ int npOnes = c - dif; if(npOnes < 0 || npOnes > cnt[1][1] + cnt[1][0]) continue; akt += 1LL * binom(cnt[0][1] + cnt[0][0], c) * binom(cnt[1][1] + cnt[1][0], npOnes); akt %= mod; } ans *= akt; ans %= mod; } cout << ans << "\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); prep(); int tt = 1; //cin >> tt; rep(te, 1, tt) solve(); return 0; }
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 | #pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = (a); i <= (b); i++) #define per(i, a, b) for (int i = (b); i >= (a); i--) #define SZ(x) ((int)x.size()) #define all(x) x.begin(), x.end() #define pb push_back #define mp make_pair #define mt make_tuple #define st first #define nd second using ll = long long; using vi = vector<int>; using pii = pair<int, int>; using pll = pair<ll, ll>; auto &operator<<(auto &o, pair<auto, auto> p) { return o << "(" << p.st << ", " << p.nd << ")"; } auto operator<<(auto &o, auto x)->decltype(end(x), o) { o << "{"; int i=0; for(auto e: x) o << ", " + 2*!i++ << e; return o << "}"; } #define deb(x...) cerr << "[" #x "]: ", [](auto...$) { ((cerr<<$<<"; "),...) << endl; }(x) const int nax = 1e6 + 5; const int mod = 1e9 + 7; vector<int> adj[nax]; int n, m; int a[nax]; int color[nax]; bool vis[nax]; bool bipartite; bool nei; int cnt[2][2]; ll pp(ll a, ll b){ ll ans = 1; while(b){ if(b & 1){ ans *= a; ans %= mod; } a *= a; a %= mod; b /= 2; } return ans; } ll inv(ll a){ return pp(a, mod - 2); } int f[nax]; int iw[nax]; void prep(){ f[0] = iw[0] = 1; rep(i, 1, nax - 1){ f[i] = (1LL * f[i - 1] * i % mod); iw[i] = inv(f[i]); } } ll binom(ll n, ll k){ ll ans = f[n]; ans *= iw[k]; ans %= mod; ans *= iw[n - k]; ans %= mod; return ans; } vector<int> now; void dfs(int v, int p){ now.pb(v); vis[v] = true; cnt[p][a[v]] += 1; color[v] = p; for(int x : adj[v]){ if(color[x] == color[v]) bipartite = false; if(a[x] == a[v]) nei = true; } for(int x : adj[v]){ if(!vis[x]) dfs(x, p ^ 1); } } void solve(){ cin >> n >> m; rep(i, 1, n) cin >> a[i]; rep(i, 1, m){ int x, y; cin >> x >> y; adj[x].pb(y); adj[y].pb(x); } rep(i, 1, n) color[i] = 2; ll ans = 1; rep(i, 1, n){ if(vis[i]) continue; bipartite = true; nei = false; cnt[0][0] = cnt[0][1] = cnt[1][0] = cnt[1][1] = 0; now.clear(); dfs(i, 0); if(!bipartite){ assert(nei); int tot = cnt[0][0] + cnt[0][1] + cnt[1][0] + cnt[1][1]; int ones = cnt[0][1] + cnt[1][1]; int zeros = tot - ones; ll akt = 0; for(int c=ones%2;c<=tot;c+=2){ akt += binom(tot, c); akt %= mod; } ans *= akt; ans %= mod; continue; } if(!nei) continue; ll akt = 0; int dif = cnt[0][1] - cnt[1][1]; rep(c, 0, cnt[0][1] + cnt[0][0]){ int npOnes = c - dif; if(npOnes < 0 || npOnes > cnt[1][1] + cnt[1][0]) continue; akt += 1LL * binom(cnt[0][1] + cnt[0][0], c) * binom(cnt[1][1] + cnt[1][0], npOnes); akt %= mod; } ans *= akt; ans %= mod; } cout << ans << "\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); prep(); int tt = 1; //cin >> tt; rep(te, 1, tt) solve(); return 0; } |