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
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod = 1e9+7;
const int N = 1e4+1;
int n, r[3], X, Y, Z, T;
int bc[N][N], dp[N][N];
ll ans;
string s[3];
int main() {
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n;
	bc[0][0]=1;
	for(int i=1; i<=n; ++i) {
		bc[i][0]=1;
		bc[i][i]=1;
		for(int j=1; j<i; ++j) {
			bc[i][j]=(bc[i-1][j]+bc[i-1][j-1])%mod;
		}
	}
	cin>>r[0]>>s[0]>>r[1]>>s[1]>>r[2]>>s[2];
	for(int i=0; i<n; ++i) {
		if(s[0][i]==s[1][i] && s[0][i]==s[2][i]) {
			X++;
		}
		else if(s[1][i]==s[2][i]) {
			Y++;
		}
		else if(s[0][i]==s[2][i]) {
			Z++;
		}
		else {
			T++;
		}
	}
	r[1]=r[1]-Y-Z;
	r[2]=r[2]-Y-T;
	for(int i=0; i<=Z+T; ++i) {
		for(int z=max(i-T, 0); z<=min(i, Z); ++z) {
			int t=i-z;
			ll tmp = (ll)bc[Z][z]*(ll)bc[T][t]%mod;
			dp[i][z-t+T]+=tmp;
			dp[i][z-t+T]%=mod;
		}
		for(int j=1; j<=Z+T; ++j) {
			dp[i][j]+=dp[i][j-1];
			dp[i][j]%=mod;
		}
		if(i>0) {
			for(int j=0; j<=Z+T; ++j) {
				dp[i][j]+=dp[i-1][j];
				dp[i][j]%=mod;
			}
		}
	}
	for(int x=0; x<=X; ++x) {
		for(int y=0; y<=Y; ++y) {
			int sum=r[0]-x-y;
			ll c = (ll)bc[X][x]*(ll)bc[Y][y]%mod;
			sum=min(sum, Z+T);
			if(sum>=0) {
				ans+=c*(ll)dp[sum][Z+T]%mod;
				ans%=mod;
			}
			int val=x-y-r[1]-1;
			val=min(val, Z);
			if(val+T<0) {
				if(sum>=0) {
					ans+=c*((ll)dp[Z+T][Z+T]-(ll)dp[sum][Z+T]+mod)%mod;
				}
				else {
					ans+=c*(ll)dp[Z+T][Z+T]%mod;
				}
				ans%=mod;
			}
			else {
				if(sum>=0) {
					ans+=c*((ll)dp[Z+T][Z+T]-(ll)dp[sum][Z+T]-(ll)dp[Z+T][val+T]+(ll)dp[sum][val+T]+2LL*mod)%mod;
				}
				else {
					ans+=c*((ll)dp[Z+T][Z+T]-(ll)dp[Z+T][val+T]+mod)%mod;
				}
				ans%=mod;
			}
			val=min(val, r[2]+y-x);
			if(val+T>=0) {
				if(sum>=0) ans+=c*((ll)dp[Z+T][val+T]-(ll)dp[sum][val+T]+mod)%mod;
				else ans+=c*(ll)dp[Z+T][val+T]%mod;
				ans%=mod;
			}
		}
	}
	cout<<ans;
}