#include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define eb emplace_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef basic_string<int> BI; typedef long long ll; typedef pair<int,int> PII; typedef double db; mt19937 mrand(1); const ll mod=1000000007; int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head template<int MOD, int RT> struct mint { static const int mod = MOD; static constexpr mint rt() { return RT; } // primitive root for FFT int v; explicit operator int() const { return v; } // explicit -> don't silently convert to int mint():v(0) {} mint(ll _v) { v = int((-MOD < _v && _v < MOD) ? _v : _v % MOD); if (v < 0) v += MOD; } bool operator==(const mint& o) const { return v == o.v; } friend bool operator!=(const mint& a, const mint& b) { return !(a == b); } friend bool operator<(const mint& a, const mint& b) { return a.v < b.v; } mint& operator+=(const mint& o) { if ((v += o.v) >= MOD) v -= MOD; return *this; } mint& operator-=(const mint& o) { if ((v -= o.v) < 0) v += MOD; return *this; } mint& operator*=(const mint& o) { v = int((ll)v*o.v%MOD); return *this; } mint& operator/=(const mint& o) { return (*this) *= inv(o); } friend mint pow(mint a, ll p) { mint ans = 1; assert(p >= 0); for (; p; p /= 2, a *= a) if (p&1) ans *= a; return ans; } friend mint inv(const mint& a) { assert(a.v != 0); return pow(a,MOD-2); } mint operator-() const { return mint(-v); } mint& operator++() { return *this += 1; } mint& operator--() { return *this -= 1; } friend mint operator+(mint a, const mint& b) { return a += b; } friend mint operator-(mint a, const mint& b) { return a -= b; } friend mint operator*(mint a, const mint& b) { return a *= b; } friend mint operator/(mint a, const mint& b) { return a /= b; } }; const int MOD=1000000007; using mi = mint<MOD,5>; // 5 is primitive root for both common mods namespace simp { vector<mi> fac,ifac,invn; void check(int x) { if (fac.empty()) { fac={mi(1),mi(1)}; ifac={mi(1),mi(1)}; invn={mi(0),mi(1)}; } while (SZ(fac)<=x) { int n=SZ(fac),m=SZ(fac)*2; fac.resize(m); ifac.resize(m); invn.resize(m); for (int i=n;i<m;i++) { fac[i]=fac[i-1]*mi(i); invn[i]=mi(MOD-MOD/i)*invn[MOD%i]; ifac[i]=ifac[i-1]*invn[i]; } } } mi gfac(int x) { assert(x>=0); check(x); return fac[x]; } mi ginv(int x) { assert(x>0); check(x); return invn[x]; } mi gifac(int x) { assert(x>=0); check(x); return ifac[x]; } mi binom(int n,int m) { if (m < 0 || m > n) return mi(0); return gfac(n)*gifac(m)*gifac(n - m); } } const int N=601000; int n,m,deg; int c[N],f[N],mark[N]; VI vc[N]; vector<PII> E; int find(int x) { return f[x]==x?x:f[x]=find(f[x]); } int main() { scanf("%d%d",&n,&m); rep(i,1,n+1) scanf("%d",&c[i]),f[i]=i; rep(i,1,n+1) f[i+n]=i+n,f[i+2*n]=i+2*n; rep(i,0,m) { int u,v; scanf("%d%d",&u,&v); f[find(u)]=find(v); f[find(u+n)]=find(v+2*n); f[find(u+2*n)]=find(v+n); E.pb(mp(u,v)); } rep(i,1,n+1) { vc[find(i)].pb(i); } for (auto [u,v]:E) if (c[u]==c[v]) mark[find(u)]=1; mi ans=1; rep(i,1,n+1) if (f[i]==i&&mark[i]) { int sz=SZ(vc[i]); if (find(i+n)==find(i+2*n)) ans=ans*pow(mi(2),sz-1); else { int ls=0,rs=0,lc=0,rc=0; for (auto x:vc[i]) if (find(x+n)==find(i+n)) { ls++; lc+=c[x]; } else { rs++; rc+=c[x]; } mi w=0; rep(i,0,ls+1) { int j=i-lc+rc; if (j>=0&&j<=rs) w+=simp::binom(ls,i)*simp::binom(rs,j); } ans=ans*w; } } printf("%d\n",(int)ans); }
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 146 147 148 | #include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define eb emplace_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef basic_string<int> BI; typedef long long ll; typedef pair<int,int> PII; typedef double db; mt19937 mrand(1); const ll mod=1000000007; int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head template<int MOD, int RT> struct mint { static const int mod = MOD; static constexpr mint rt() { return RT; } // primitive root for FFT int v; explicit operator int() const { return v; } // explicit -> don't silently convert to int mint():v(0) {} mint(ll _v) { v = int((-MOD < _v && _v < MOD) ? _v : _v % MOD); if (v < 0) v += MOD; } bool operator==(const mint& o) const { return v == o.v; } friend bool operator!=(const mint& a, const mint& b) { return !(a == b); } friend bool operator<(const mint& a, const mint& b) { return a.v < b.v; } mint& operator+=(const mint& o) { if ((v += o.v) >= MOD) v -= MOD; return *this; } mint& operator-=(const mint& o) { if ((v -= o.v) < 0) v += MOD; return *this; } mint& operator*=(const mint& o) { v = int((ll)v*o.v%MOD); return *this; } mint& operator/=(const mint& o) { return (*this) *= inv(o); } friend mint pow(mint a, ll p) { mint ans = 1; assert(p >= 0); for (; p; p /= 2, a *= a) if (p&1) ans *= a; return ans; } friend mint inv(const mint& a) { assert(a.v != 0); return pow(a,MOD-2); } mint operator-() const { return mint(-v); } mint& operator++() { return *this += 1; } mint& operator--() { return *this -= 1; } friend mint operator+(mint a, const mint& b) { return a += b; } friend mint operator-(mint a, const mint& b) { return a -= b; } friend mint operator*(mint a, const mint& b) { return a *= b; } friend mint operator/(mint a, const mint& b) { return a /= b; } }; const int MOD=1000000007; using mi = mint<MOD,5>; // 5 is primitive root for both common mods namespace simp { vector<mi> fac,ifac,invn; void check(int x) { if (fac.empty()) { fac={mi(1),mi(1)}; ifac={mi(1),mi(1)}; invn={mi(0),mi(1)}; } while (SZ(fac)<=x) { int n=SZ(fac),m=SZ(fac)*2; fac.resize(m); ifac.resize(m); invn.resize(m); for (int i=n;i<m;i++) { fac[i]=fac[i-1]*mi(i); invn[i]=mi(MOD-MOD/i)*invn[MOD%i]; ifac[i]=ifac[i-1]*invn[i]; } } } mi gfac(int x) { assert(x>=0); check(x); return fac[x]; } mi ginv(int x) { assert(x>0); check(x); return invn[x]; } mi gifac(int x) { assert(x>=0); check(x); return ifac[x]; } mi binom(int n,int m) { if (m < 0 || m > n) return mi(0); return gfac(n)*gifac(m)*gifac(n - m); } } const int N=601000; int n,m,deg; int c[N],f[N],mark[N]; VI vc[N]; vector<PII> E; int find(int x) { return f[x]==x?x:f[x]=find(f[x]); } int main() { scanf("%d%d",&n,&m); rep(i,1,n+1) scanf("%d",&c[i]),f[i]=i; rep(i,1,n+1) f[i+n]=i+n,f[i+2*n]=i+2*n; rep(i,0,m) { int u,v; scanf("%d%d",&u,&v); f[find(u)]=find(v); f[find(u+n)]=find(v+2*n); f[find(u+2*n)]=find(v+n); E.pb(mp(u,v)); } rep(i,1,n+1) { vc[find(i)].pb(i); } for (auto [u,v]:E) if (c[u]==c[v]) mark[find(u)]=1; mi ans=1; rep(i,1,n+1) if (f[i]==i&&mark[i]) { int sz=SZ(vc[i]); if (find(i+n)==find(i+2*n)) ans=ans*pow(mi(2),sz-1); else { int ls=0,rs=0,lc=0,rc=0; for (auto x:vc[i]) if (find(x+n)==find(i+n)) { ls++; lc+=c[x]; } else { rs++; rc+=c[x]; } mi w=0; rep(i,0,ls+1) { int j=i-lc+rc; if (j>=0&&j<=rs) w+=simp::binom(ls,i)*simp::binom(rs,j); } ans=ans*w; } } printf("%d\n",(int)ans); } |