#include <bits/stdc++.h> using namespace std; #define int long long #define ll long long #define ld long double #define pb push_back #define nd second #define st first #define sz size #define forr(i, n) for(int i=1;i<=n;i++) const ll infl=1e18+90; const int inf=1e9+90; const int roz=2e5+93; const int mod=1e9+7; int odw[roz], ile[2][2], xd, zap=0, tab[roz], sil[roz], odwrot[roz], kol[roz]; vector<int> graf[roz]; void dfs(int u, int o, bool x) { //cerr<<"wchodze dla u = "<<u<<" x = "<<x<<"\n"; ile[x][tab[u]]++; odw[u]=1; kol[u]=x; for(auto v:graf[u]) { if(v==o) continue; if(odw[v]==0) dfs(v, u, !x); else if(x==kol[v]) zap=1; } } long long binpow(long long a, long long b, long long mod) { a %= mod; long long res = 1; while (b > 0) { if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; } void silnie() { sil[0]=1; odwrot[0]=1; for(int i=1;i<=roz-60;i++) { sil[i]=sil[i-1]*i; sil[i]%=mod; odwrot[i]=binpow(sil[i], mod-2, mod); } } int fun(int a, int b) { //cerr<<"wchodze dla a = "<<a<<" b= "<<b<<"\n"; //cerr<<"zwracam "<<((sil[a]*odwrot[b])%mod*odwrot[a-b])%mod<<"\n"; return ((sil[a]*odwrot[b])%mod*odwrot[a-b])%mod; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, mas=0; cin>>n>>m; silnie(); for(int i=1;i<=n;i++) { int a; cin>>a; tab[i]=a; } for(int i=1;i<=m;i++) { int a, b; cin>>a>>b; graf[a].pb(b); graf[b].pb(a); } int wynik=1; for(int i=1;i<=n;i++) { if(odw[i]==0) { int wyn=0; zap=0; ile[0][0]=0; ile[1][0]=0; ile[0][1]=0; ile[1][1]=0; dfs(i, 0, 0); if(zap==1) { //cerr<<"wlazlem\n"; int x=ile[0][0]+ile[0][1]+ile[1][0]+ile[1][1]; int y=(ile[0][1]+ile[1][1])%2; for(int i=y;i<=x;i+=2) { wyn+=fun(x, i); wyn%=mod; } } else { //cerr<<"xd\n"; int x=ile[0][0]+ile[0][1], y=ile[1][0]+ile[1][1]; int z=ile[0][1], c=ile[1][1]; //cerr<<"x = "<<x<<" y = "<<y<<" z = "<<z<<" c = "<<c<<"\n"; while(z>0&&c>0) { z--; c--; } while(true) { wyn+=fun(x, z)*fun(y, c); wyn%=mod; z++; c++; if(z>x) break; if(c>y) break; } } wynik*=wyn; wynik%=mod; } } cout<<wynik<<"\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 | #include <bits/stdc++.h> using namespace std; #define int long long #define ll long long #define ld long double #define pb push_back #define nd second #define st first #define sz size #define forr(i, n) for(int i=1;i<=n;i++) const ll infl=1e18+90; const int inf=1e9+90; const int roz=2e5+93; const int mod=1e9+7; int odw[roz], ile[2][2], xd, zap=0, tab[roz], sil[roz], odwrot[roz], kol[roz]; vector<int> graf[roz]; void dfs(int u, int o, bool x) { //cerr<<"wchodze dla u = "<<u<<" x = "<<x<<"\n"; ile[x][tab[u]]++; odw[u]=1; kol[u]=x; for(auto v:graf[u]) { if(v==o) continue; if(odw[v]==0) dfs(v, u, !x); else if(x==kol[v]) zap=1; } } long long binpow(long long a, long long b, long long mod) { a %= mod; long long res = 1; while (b > 0) { if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; } void silnie() { sil[0]=1; odwrot[0]=1; for(int i=1;i<=roz-60;i++) { sil[i]=sil[i-1]*i; sil[i]%=mod; odwrot[i]=binpow(sil[i], mod-2, mod); } } int fun(int a, int b) { //cerr<<"wchodze dla a = "<<a<<" b= "<<b<<"\n"; //cerr<<"zwracam "<<((sil[a]*odwrot[b])%mod*odwrot[a-b])%mod<<"\n"; return ((sil[a]*odwrot[b])%mod*odwrot[a-b])%mod; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, mas=0; cin>>n>>m; silnie(); for(int i=1;i<=n;i++) { int a; cin>>a; tab[i]=a; } for(int i=1;i<=m;i++) { int a, b; cin>>a>>b; graf[a].pb(b); graf[b].pb(a); } int wynik=1; for(int i=1;i<=n;i++) { if(odw[i]==0) { int wyn=0; zap=0; ile[0][0]=0; ile[1][0]=0; ile[0][1]=0; ile[1][1]=0; dfs(i, 0, 0); if(zap==1) { //cerr<<"wlazlem\n"; int x=ile[0][0]+ile[0][1]+ile[1][0]+ile[1][1]; int y=(ile[0][1]+ile[1][1])%2; for(int i=y;i<=x;i+=2) { wyn+=fun(x, i); wyn%=mod; } } else { //cerr<<"xd\n"; int x=ile[0][0]+ile[0][1], y=ile[1][0]+ile[1][1]; int z=ile[0][1], c=ile[1][1]; //cerr<<"x = "<<x<<" y = "<<y<<" z = "<<z<<" c = "<<c<<"\n"; while(z>0&&c>0) { z--; c--; } while(true) { wyn+=fun(x, z)*fun(y, c); wyn%=mod; z++; c++; if(z>x) break; if(c>y) break; } } wynik*=wyn; wynik%=mod; } } cout<<wynik<<"\n"; } |