#include<set> #include<map> #include<queue> #include<vector> #include<algorithm> #include<bits/stdc++.h> #define pr pair #define f first #define s second #define ll long long #define mp make_pair #define pll pr<ll,ll> #define pii pr<int,int> #define piii pr<int,pii> using namespace std; bool vd[200005]; vector<int> e[200005]; int s[200005]; int cl[200005]; const ll m=1e9+7,mo=m; ll pw(ll a,ll b=m-2) { ll r=1; while(b>0) { if(b&1) r=r*a%m; b>>=1; a=a*a%m; } return r; } ll C(ll a,ll b) { ll r=1; for(int i=1;i<=b;i++) { r=r*(a-i+1)%m; r=r*pw(i)%m; } return r; } ll cal(int x) { queue<int> q; bool bd=0; int cnt=0,tot=0; q.push(x); vd[x]=1; while(q.size()) { int f=q.front(); q.pop(); tot++; // cout<<"Fn "<<f<<endl; if(cl[f]^s[f]) cnt++; for(int t:e[f]) { if(vd[t]) { if(cl[t]==cl[f]) bd=1; } else { cl[t]=cl[f]^1; vd[t]=1; q.push(t); } } } // cout<<"F "<<bd<<' '<<tot<<' '<<cnt<<endl; if(bd) return pw(2,tot-1); return C(tot,cnt); } int main() { ios_base::sync_with_stdio(0); int n,m; cin>>n>>m; int u,v; for(int i=0;i<n;i++) cin>>s[i]; for(int i=0;i<m;i++) { cin>>u>>v; u--; v--; e[u].push_back(v); e[v].push_back(u); } ll ans=1; for(int i=0;i<n;i++) { if(vd[i]) continue; ans=ans*cal(i)%mo; } cout<<ans<<endl; 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 | #include<set> #include<map> #include<queue> #include<vector> #include<algorithm> #include<bits/stdc++.h> #define pr pair #define f first #define s second #define ll long long #define mp make_pair #define pll pr<ll,ll> #define pii pr<int,int> #define piii pr<int,pii> using namespace std; bool vd[200005]; vector<int> e[200005]; int s[200005]; int cl[200005]; const ll m=1e9+7,mo=m; ll pw(ll a,ll b=m-2) { ll r=1; while(b>0) { if(b&1) r=r*a%m; b>>=1; a=a*a%m; } return r; } ll C(ll a,ll b) { ll r=1; for(int i=1;i<=b;i++) { r=r*(a-i+1)%m; r=r*pw(i)%m; } return r; } ll cal(int x) { queue<int> q; bool bd=0; int cnt=0,tot=0; q.push(x); vd[x]=1; while(q.size()) { int f=q.front(); q.pop(); tot++; // cout<<"Fn "<<f<<endl; if(cl[f]^s[f]) cnt++; for(int t:e[f]) { if(vd[t]) { if(cl[t]==cl[f]) bd=1; } else { cl[t]=cl[f]^1; vd[t]=1; q.push(t); } } } // cout<<"F "<<bd<<' '<<tot<<' '<<cnt<<endl; if(bd) return pw(2,tot-1); return C(tot,cnt); } int main() { ios_base::sync_with_stdio(0); int n,m; cin>>n>>m; int u,v; for(int i=0;i<n;i++) cin>>s[i]; for(int i=0;i<m;i++) { cin>>u>>v; u--; v--; e[u].push_back(v); e[v].push_back(u); } ll ans=1; for(int i=0;i<n;i++) { if(vd[i]) continue; ans=ans*cal(i)%mo; } cout<<ans<<endl; return 0; } |