#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; const int N = 5e5+10; const int MOD = 1e9+7; ll c[N], n, m, f[N]; vector<int> G[N], tmp; int col[N], vx[N]; bool bip(int v, int d) { col[v]=d; tmp.push_back(v); vx[v]=true; for (auto u : G[v]) { if (!vx[u]) { if (!bip(u, d^1)) return false; } else if (col[u] == col[v]) return false; } return true; } bool v2[N]; int dfs(int v) { v2[v]=true; int res=1; for (auto u : G[v]) { if (v2[u]) continue; res+=dfs(u); } return res; } ll bpw(ll x, ll n) { if (n == 0) return 1; ll c=bpw(x, n/2); c=(c*c)%MOD; if (n&1) return (c*x)%MOD; return c; } ll inv(ll x) { return bpw(x, MOD-2); } ll nck(ll n, ll k) { if (k > n) return 0; return (f[n]*inv((f[k]*f[n-k])%MOD))%MOD; } ll calc(ll l, ll x, ll r, ll y) { ll res=0, mn=min(x, y); x-=mn, y-=mn; while (x <= l && y <= r) { (res+=(nck(l, x)*nck(r, y))%MOD)%=MOD; ++x, ++y; } return res; } void solve() { int n, m; cin>>n>>m; for (int i=1; i<=n; ++i) cin>>c[i]; for (int i=1; i<=m; ++i) { int a, b; cin>>a>>b; G[a].push_back(b); G[b].push_back(a); } f[0]=1; for (ll i=1; i<=n; ++i) (f[i]=f[i-1]*i)%=MOD; ll ans=1; for (int i=1; i<=n; ++i) { if (v2[i]) continue; int sz=dfs(i); tmp.clear(); if (bip(i, 0)) { ll l=0, r=0, x=0, y=0; for (auto u : tmp) { assert(vx[u]); if (col[u] == 0) { ++l, x+=c[u]; } else { ++r, y+=c[u]; } } assert(l+r == sz); (ans*=calc(l, x, r, y))%=MOD; } else { (ans*=bpw(2, sz-1))%=MOD; } } cout<<ans<<"\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t=1; //cin>>t; while (t--) 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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; const int N = 5e5+10; const int MOD = 1e9+7; ll c[N], n, m, f[N]; vector<int> G[N], tmp; int col[N], vx[N]; bool bip(int v, int d) { col[v]=d; tmp.push_back(v); vx[v]=true; for (auto u : G[v]) { if (!vx[u]) { if (!bip(u, d^1)) return false; } else if (col[u] == col[v]) return false; } return true; } bool v2[N]; int dfs(int v) { v2[v]=true; int res=1; for (auto u : G[v]) { if (v2[u]) continue; res+=dfs(u); } return res; } ll bpw(ll x, ll n) { if (n == 0) return 1; ll c=bpw(x, n/2); c=(c*c)%MOD; if (n&1) return (c*x)%MOD; return c; } ll inv(ll x) { return bpw(x, MOD-2); } ll nck(ll n, ll k) { if (k > n) return 0; return (f[n]*inv((f[k]*f[n-k])%MOD))%MOD; } ll calc(ll l, ll x, ll r, ll y) { ll res=0, mn=min(x, y); x-=mn, y-=mn; while (x <= l && y <= r) { (res+=(nck(l, x)*nck(r, y))%MOD)%=MOD; ++x, ++y; } return res; } void solve() { int n, m; cin>>n>>m; for (int i=1; i<=n; ++i) cin>>c[i]; for (int i=1; i<=m; ++i) { int a, b; cin>>a>>b; G[a].push_back(b); G[b].push_back(a); } f[0]=1; for (ll i=1; i<=n; ++i) (f[i]=f[i-1]*i)%=MOD; ll ans=1; for (int i=1; i<=n; ++i) { if (v2[i]) continue; int sz=dfs(i); tmp.clear(); if (bip(i, 0)) { ll l=0, r=0, x=0, y=0; for (auto u : tmp) { assert(vx[u]); if (col[u] == 0) { ++l, x+=c[u]; } else { ++r, y+=c[u]; } } assert(l+r == sz); (ans*=calc(l, x, r, y))%=MOD; } else { (ans*=bpw(2, sz-1))%=MOD; } } cout<<ans<<"\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t=1; //cin>>t; while (t--) solve(); } |