#include <bits/stdc++.h> using namespace std; long long MOD = 1000000007ll; long long n; string k[4]; long long r[4]; long long P1[10001]; long long P2[10001]; long long P3[10001]; long long P3S[10001]; long long PA[10001]; long long PB[10001]; long long PC[10001]; long long PD[10001]; long long PDS[10001]; void binomialCoeff(int nn, int kk, long long C[]) { for (int i = 0; i <= nn; i++)C[i]=0; C[0] = 1; // nC0 is 1 for (int i = 1; i <= nn; i++) { for (int j = min(i, kk); j > 0; j--) { C[j] = C[j] + C[j-1]; C[j]%=MOD; } } // cout<<"\nPIT: "; // for (int i = 0; i <= nn; i++) { cout<<C[i]<<" ";} // cout<<"\n"; } long long F(int r){ long long S = 0 ; for(int i =0;i<=r;i++){ S += P1[i]; S%=MOD; } return S; } long long d[4][4]; long long F_2(int a, int b){ long long S = 0,r1,r2,z = d[a][b]; long long W = 0; //cout<<"z: "<<z<<" n-z: "<<n-z<<" r1: "<<r[a]<<" r2: "<<r[b]<<"\n"; binomialCoeff(z,z,P2); binomialCoeff(n-z,n-z,P3); P3S[0]=P3[0]; for(int i =1;i<=n-z;i++){ P3S[i]=P3[i]+P3S[i-1]; P3S[i]%=MOD; } for(long long i = 0;i<=z;i++){ r1 = i; r2 = z - i; if(r1 > r[a] || r2>r[b])continue; // cout<<"G: "<<min(min(r[a]-r1,r[b]-r2),n-z)<<"\n"; W+=(P2[min(r1,r2)]*P3S[min(min(r[a]-r1,r[b]-r2),n-z)])%MOD; W%=MOD; } //cout<<"W: "<<W<<"\n"; return W; } long long F_3() { long long S = 0; long long d = 0, a = 0, b = 0, c = 0; for(int i =0;i<n;i++){ if(k[1][i]==k[2][i] && k[1][i]==k[3][i])d++; if(k[1][i]==k[2][i] && k[1][i]!=k[3][i])a++; if(k[1][i]==k[3][i] && k[1][i]!=k[2][i])b++; if(k[2][i]==k[3][i] && k[1][i]!=k[2][i])c++; } //cout<<"a: "<<a<<" b: "<<b<<" c: "<<c<<" d: "<<d<<"\n"; binomialCoeff(a,a,PA); binomialCoeff(b,b,PB); binomialCoeff(c,c,PC); binomialCoeff(d,d,PD); PDS[0]=PD[0]; for(int i =1;i<=d;i++){ PDS[i]=PD[i]+PDS[i-1]; PDS[i]%=MOD; } long long z = n - d; long long r1,r2,r3; long long ile = 0,ile2= 0; for(long long i = 0; i <= min(r[1],z);i++) { if(r[1]<i)break; for(long long j = 0; j<= min(i,a); j++) { if((2*j-i+b+c)>r[2])break; for(long long k = min((r[3]+i-a-c)/2ll,min(i-j,b)); k>= 0; k--) { // if((2*k-i+a+c)>r[3])break; ile2++; long long l = i-j-k; if(l>=0 && l<=c ){ r1 = r[1]-i; r2 = r[2]-(j+(b-k)+(c-l)); r3 = r[3]-((a-j)+k+(c-l)); if(r1>=0 && r2>=0 && r3>=0){ // cout<<"i: "<<i<<" j: "<<j<<" k: "<<k<<" l: "<<l<<"\n"; if(min(min(r1,r2),r3) == 0)ile++; S+=((((PA[j]*PB[k])%MOD)*PC[l])%MOD)*PDS[min(min(min(r1,r2),r3),d)]; S%=MOD; } }else{ break; } } } } // cout<<"ILE: "<<ile<<" ile2: "<<ile2<<"\n"; return S; } int main() { cin>>n; binomialCoeff(n,n,P1); cin>>r[1];cin>>k[1]; cin>>r[2];cin>>k[2]; cin>>r[3];cin>>k[3]; if(r[1]>r[2]){ swap(r[1],r[2]); swap(k[1],k[2]); } if(r[1]>r[3]){ swap(r[1],r[3]); swap(k[1],k[3]); } if(r[2]>r[3]){ swap(r[2],r[3]); swap(k[2],k[3]); } long long F1 = F(r[1]); long long F2 = F(r[2]); long long F3 = F(r[3]); //cout<<"F_1: "<<F1<<" "<<F2<<" "<<F3<<"\n"; long long FS = F1; FS += F2; FS%=MOD; FS += F3; FS%=MOD; //cout<<"FS: "<<FS<<"\n"; for(int i =0;i<n;i++){ if(k[1][i]!=k[2][i])d[1][2]++; if(k[1][i]!=k[3][i])d[1][3]++; if(k[2][i]!=k[3][i])d[2][3]++; }; long long F12 = F_2(1,2); long long F13 = F_2(1,3); long long F23 = F_2(2,3); // cout<<"F_2 "<<F12<<" "<<F13<<" "<<F23<<"\n"; long long F2S = F12; F2S += F13; F2S%=MOD; F2S += F23; F2S%=MOD; //cout<<"F2S: "<<F2S<<"\n"; if(r[1]==r[2] && k[1]==k[2]){ long long W = F1+F3; W%=MOD; W-=F13; W+=MOD; W%=MOD; cout<<W<<"\n"; return 0; } if(r[2]==r[3] && k[2]==k[3]){ long long W = F1+F3; W%=MOD; W-=F13; W+=MOD; W%=MOD; cout<<W<<"\n"; return 0; } long long F123 = F_3(); //cout<<"F123: "<<F123<<"\n"; long long W = ((FS+F123)%MOD-F2S)%MOD; W+=MOD; W%=MOD; printf("%lld\n",W); return 0; } //270016253
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | #include <bits/stdc++.h> using namespace std; long long MOD = 1000000007ll; long long n; string k[4]; long long r[4]; long long P1[10001]; long long P2[10001]; long long P3[10001]; long long P3S[10001]; long long PA[10001]; long long PB[10001]; long long PC[10001]; long long PD[10001]; long long PDS[10001]; void binomialCoeff(int nn, int kk, long long C[]) { for (int i = 0; i <= nn; i++)C[i]=0; C[0] = 1; // nC0 is 1 for (int i = 1; i <= nn; i++) { for (int j = min(i, kk); j > 0; j--) { C[j] = C[j] + C[j-1]; C[j]%=MOD; } } // cout<<"\nPIT: "; // for (int i = 0; i <= nn; i++) { cout<<C[i]<<" ";} // cout<<"\n"; } long long F(int r){ long long S = 0 ; for(int i =0;i<=r;i++){ S += P1[i]; S%=MOD; } return S; } long long d[4][4]; long long F_2(int a, int b){ long long S = 0,r1,r2,z = d[a][b]; long long W = 0; //cout<<"z: "<<z<<" n-z: "<<n-z<<" r1: "<<r[a]<<" r2: "<<r[b]<<"\n"; binomialCoeff(z,z,P2); binomialCoeff(n-z,n-z,P3); P3S[0]=P3[0]; for(int i =1;i<=n-z;i++){ P3S[i]=P3[i]+P3S[i-1]; P3S[i]%=MOD; } for(long long i = 0;i<=z;i++){ r1 = i; r2 = z - i; if(r1 > r[a] || r2>r[b])continue; // cout<<"G: "<<min(min(r[a]-r1,r[b]-r2),n-z)<<"\n"; W+=(P2[min(r1,r2)]*P3S[min(min(r[a]-r1,r[b]-r2),n-z)])%MOD; W%=MOD; } //cout<<"W: "<<W<<"\n"; return W; } long long F_3() { long long S = 0; long long d = 0, a = 0, b = 0, c = 0; for(int i =0;i<n;i++){ if(k[1][i]==k[2][i] && k[1][i]==k[3][i])d++; if(k[1][i]==k[2][i] && k[1][i]!=k[3][i])a++; if(k[1][i]==k[3][i] && k[1][i]!=k[2][i])b++; if(k[2][i]==k[3][i] && k[1][i]!=k[2][i])c++; } //cout<<"a: "<<a<<" b: "<<b<<" c: "<<c<<" d: "<<d<<"\n"; binomialCoeff(a,a,PA); binomialCoeff(b,b,PB); binomialCoeff(c,c,PC); binomialCoeff(d,d,PD); PDS[0]=PD[0]; for(int i =1;i<=d;i++){ PDS[i]=PD[i]+PDS[i-1]; PDS[i]%=MOD; } long long z = n - d; long long r1,r2,r3; long long ile = 0,ile2= 0; for(long long i = 0; i <= min(r[1],z);i++) { if(r[1]<i)break; for(long long j = 0; j<= min(i,a); j++) { if((2*j-i+b+c)>r[2])break; for(long long k = min((r[3]+i-a-c)/2ll,min(i-j,b)); k>= 0; k--) { // if((2*k-i+a+c)>r[3])break; ile2++; long long l = i-j-k; if(l>=0 && l<=c ){ r1 = r[1]-i; r2 = r[2]-(j+(b-k)+(c-l)); r3 = r[3]-((a-j)+k+(c-l)); if(r1>=0 && r2>=0 && r3>=0){ // cout<<"i: "<<i<<" j: "<<j<<" k: "<<k<<" l: "<<l<<"\n"; if(min(min(r1,r2),r3) == 0)ile++; S+=((((PA[j]*PB[k])%MOD)*PC[l])%MOD)*PDS[min(min(min(r1,r2),r3),d)]; S%=MOD; } }else{ break; } } } } // cout<<"ILE: "<<ile<<" ile2: "<<ile2<<"\n"; return S; } int main() { cin>>n; binomialCoeff(n,n,P1); cin>>r[1];cin>>k[1]; cin>>r[2];cin>>k[2]; cin>>r[3];cin>>k[3]; if(r[1]>r[2]){ swap(r[1],r[2]); swap(k[1],k[2]); } if(r[1]>r[3]){ swap(r[1],r[3]); swap(k[1],k[3]); } if(r[2]>r[3]){ swap(r[2],r[3]); swap(k[2],k[3]); } long long F1 = F(r[1]); long long F2 = F(r[2]); long long F3 = F(r[3]); //cout<<"F_1: "<<F1<<" "<<F2<<" "<<F3<<"\n"; long long FS = F1; FS += F2; FS%=MOD; FS += F3; FS%=MOD; //cout<<"FS: "<<FS<<"\n"; for(int i =0;i<n;i++){ if(k[1][i]!=k[2][i])d[1][2]++; if(k[1][i]!=k[3][i])d[1][3]++; if(k[2][i]!=k[3][i])d[2][3]++; }; long long F12 = F_2(1,2); long long F13 = F_2(1,3); long long F23 = F_2(2,3); // cout<<"F_2 "<<F12<<" "<<F13<<" "<<F23<<"\n"; long long F2S = F12; F2S += F13; F2S%=MOD; F2S += F23; F2S%=MOD; //cout<<"F2S: "<<F2S<<"\n"; if(r[1]==r[2] && k[1]==k[2]){ long long W = F1+F3; W%=MOD; W-=F13; W+=MOD; W%=MOD; cout<<W<<"\n"; return 0; } if(r[2]==r[3] && k[2]==k[3]){ long long W = F1+F3; W%=MOD; W-=F13; W+=MOD; W%=MOD; cout<<W<<"\n"; return 0; } long long F123 = F_3(); //cout<<"F123: "<<F123<<"\n"; long long W = ((FS+F123)%MOD-F2S)%MOD; W+=MOD; W%=MOD; printf("%lld\n",W); return 0; } //270016253 |