#include <bits/stdc++.h> #define ll long long #define ld long double #define endl '\n' #define st first #define nd second #define pb push_back #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() using namespace std; int inf=1000000007; ll infl=1000000000000000007; ll mod=1000000007; ll mod1=998244353; const int N=200007; ll fac[N]; ll inv[N]; ll pot(ll x,ll p) {int res=1;for(;p;p>>=1) {if(p&1) res=res*x%mod; x=x*x%mod;} return res;} ll npok(ll n,ll k) { if(min(k,n)<0||k>n) return 0; return fac[n]*inv[k]%mod*inv[n-k]%mod; } bool w[N]; vector<int>G[N]; bool ok; int C1,C2,S1,S2; int col[N]; void dfs(int v) { if(col[v]==1) { C1++; if(w[v]) S1++; } else { C2++; if(w[v]) S2++; } for(auto u:G[v]) { if(!col[u]) { col[u]=3-col[v]; dfs(u); } else if(col[u]==col[v]) ok=0; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,m,a,b; cin>>n>>m; fac[0]=1; for(int i=1;i<=n;i++) fac[i]=fac[i-1]*i%mod; inv[n]=pot(fac[n],mod-2); for(int i=n-1;i>=0;i--) inv[i]=inv[i+1]*(i+1)%mod; for(int i=1;i<=n;i++) cin>>w[i]; while(m--) { cin>>a>>b; G[a].pb(b); G[b].pb(a); } ll ans=1; for(int i=1;i<=n;i++) { if(col[i]) continue; ok=1; col[i]=1; C1=0,C2=0,S1=0,S2=0; dfs(i); int t=C1+C2; if(!ok) { for(int j=1;j<t;j++) ans=ans*2%mod; } else { //cout<<C1<<" "<<S1<<" "<<C2<<" "<<S2<<endl; ll ile=0; for(int k=-t;k<=t;k++) ile=(ile+npok(C1,S1+k)*npok(C2,S2+k))%mod; ans=ans*ile%mod; } } cout<<ans; 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 | #include <bits/stdc++.h> #define ll long long #define ld long double #define endl '\n' #define st first #define nd second #define pb push_back #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() using namespace std; int inf=1000000007; ll infl=1000000000000000007; ll mod=1000000007; ll mod1=998244353; const int N=200007; ll fac[N]; ll inv[N]; ll pot(ll x,ll p) {int res=1;for(;p;p>>=1) {if(p&1) res=res*x%mod; x=x*x%mod;} return res;} ll npok(ll n,ll k) { if(min(k,n)<0||k>n) return 0; return fac[n]*inv[k]%mod*inv[n-k]%mod; } bool w[N]; vector<int>G[N]; bool ok; int C1,C2,S1,S2; int col[N]; void dfs(int v) { if(col[v]==1) { C1++; if(w[v]) S1++; } else { C2++; if(w[v]) S2++; } for(auto u:G[v]) { if(!col[u]) { col[u]=3-col[v]; dfs(u); } else if(col[u]==col[v]) ok=0; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,m,a,b; cin>>n>>m; fac[0]=1; for(int i=1;i<=n;i++) fac[i]=fac[i-1]*i%mod; inv[n]=pot(fac[n],mod-2); for(int i=n-1;i>=0;i--) inv[i]=inv[i+1]*(i+1)%mod; for(int i=1;i<=n;i++) cin>>w[i]; while(m--) { cin>>a>>b; G[a].pb(b); G[b].pb(a); } ll ans=1; for(int i=1;i<=n;i++) { if(col[i]) continue; ok=1; col[i]=1; C1=0,C2=0,S1=0,S2=0; dfs(i); int t=C1+C2; if(!ok) { for(int j=1;j<t;j++) ans=ans*2%mod; } else { //cout<<C1<<" "<<S1<<" "<<C2<<" "<<S2<<endl; ll ile=0; for(int k=-t;k<=t;k++) ile=(ile+npok(C1,S1+k)*npok(C2,S2+k))%mod; ans=ans*ile%mod; } } cout<<ans; return 0; } |