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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <list>
#include <ctime>
#include <sstream>
#include <queue>
#include <stack>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;
typedef short int sint;
#define FOR(x, b, e) for(int x=(b); x<=(e); ++x)
#define FORD(x, b, e) for(int x=((int)(b))-1; x>=(e); --x)
#define REP(x, n) for(int x=0; x<(n); ++x)
#define ALL(c) c.begin(),c.end()
#define SIZE(x) ((int)((x).size()))
#define PB push_back
#define ST first
#define ND second
#define DEBUG 0
#define debug(x) {if (DEBUG)cerr <<#x <<" = " <<x <<endl; }
#define debugv(x) {if (DEBUG) {cerr <<#x <<" = "; FOREACH(it, (x)) cerr <<*it <<", "; cout <<endl; }}
#define REMAX(a,b) (a)=max((a),(b));
#define REMIN(a,b) (a)=min((a),(b));
#define wez(n) int (n); scanf("%d",&(n));
#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m));

const int K = 3;
const int N = 10010;
char a[K][N];
int r[K];
int n;
int s[N][N]; // [N][N] - N^2 * 4B ~400MB
int ros[N + N / 3][N / 3], m[N + N / 3][N / 3]; // N * N * 4/3 * 1/3 * 2 = N^2 * 8/9 ~ 400MB
const int MOD = 1000000000 + 7;
int a012, a01, a12, a02;

int d(int a, int b) {
	if (b > a) {
		return 0;
	}
	if (a == b || b == 0) {
		return 1;
	}
	return (s[a][b] - s[a][b - 1] + MOD) % MOD;
}

ll mn(ll a, ll b) {
	return (a * b) % MOD;
}

ll countRangeGoingUp(int p, int q, int wys, int offset) {
	int os = max(0, q - wys + offset);
	int il = q - p;
	// printf("\t\t offset: %d, il: %d\n", os, il);
	return ros[os][il];
}

ll countRangeGoingDown(int p, int q, int wys, int ogr) {
	if (p > q) {
		return 0;
	}
	int os = min(p + wys, ogr);
	int il = q - p;
	// printf("\t\t  offset: %d, il: %d\n", os, il);
	return m[os][il];
}

ll getRes(int x, int y, int a12) {
	int k2 = (x - y + a12);
	if (k2 < 0) {
		k2 = 0;
	}
	if (k2 > 2 * a12) {
		k2 = 2 * a12;
	}
	int k = k2 / 2;
	int wys = min(x - k, y - (a12 - k));
	int wys2 = min(x - (k + 1), y - (a12 - (k + 1)));
	if(DEBUG) printf("  inNEW: k: %d, wys: %d, wys2: %d\n", k, wys, wys2);
	if (wys < 0) {
		return 0;
	}
	bool drug = false;
	if (k + 1 <= a12 && wys2 == wys) {
		drug = true;
		if (DEBUG) printf("  podwojna gora!\n");
	}
	// podwojna gora
	// x + [0;k], [k+1, a12]
	int q1 = k, p2 = k + 1;
	int p1 = max(q1 - wys, 0), q2 = min(a12, p2 + wys2);
	ll r1 = countRangeGoingUp(p1, q1, wys, a012);
	ll r2 = countRangeGoingDown(p2, q2, wys2, a012 + a12);
	if (DEBUG) {
		printf("counting up: [%d;%d] %d - %lld, counting down: [%d; %d] %d - %lld\n", p1, q1, wys, r1, p2, q2, wys2, r2);
	}
	return (r1 + r2) % MOD;
}

ll allThreeWz() {
	// find correct order
	int order[K];
	REP(i, K) order[i]=i;
	do {
		a012 = a01 = a02=a12 = 0;
		REP(i, n) {
			if (a[order[0]][i] == a[order[1]][i] && a[order[1]][i] == a[order[2]][i]) {
				++a012;
			} else if (a[order[0]][i] == a[order[1]][i]) {
				++a01;
			} else if (a[order[0]][i] == a[order[2]][i]) {
				++a02;
			} else {
				++a12;
			}
		}
		if (a01 >= a02 && a02 >= a12) {
			break;
		}
	} while (next_permutation(order, order + K));
	REP(k, a012 + a12 + 1) {
		int firstLeft = max(0, a012 - k);
		int firstK = max(0, k - a012);
		ros[k][0] = mn(d(a12, firstK), s[a012][firstLeft]);
		FOR(i, 1, a12) {
			++firstLeft;
			++firstK;
			if (firstK > a12) {
				break;
			}
			ros[k][i] = (ros[k][i - 1] + mn(d(a12, firstK), s[a012][min(firstLeft, a012)])) % MOD; 
		}
	}
	REP(q, a012 + a12 + 1) {
		int firstK = min(q, a12);
		int firstLeft = max(0, q - a12);
		m[q][0] = mn(d(a12, firstK), s[a012][firstLeft]);
		// printf("q: %d\n", q);
		// printf("m[%d][%d] = %d\n", q, 0, m[q][0]);
		FOR(i, 1, firstK) {
			int nk = firstK - i;
			++firstLeft;
			m[q][i] = (m[q][i - 1] + mn(d(a12, nk), s[a012][min(firstLeft, a012)])) % MOD;
		}
	}
	if (DEBUG) printf("012: %d,  01: %d, 02: %d, 12: %d\n", a012, a01, a02, a12);
	ll res = 0;
	REP(i, a01 + 1) {
		REP(j, a02 + 1) {
			int x0 = r[order[0]] - (a01 - i) - (a02 - j);
			int x1 = r[order[1]] - (a01 - i) - j;
			int x2 = r[order[2]] - i - (a02 - j);
			int nx = min(x1, x2);
			if (nx < 0 || x0 < 0) {
				continue;
			}
			if (DEBUG) printf("zest: y0: %d, ny: %d, a12: %d\n", x0, nx, a12);
			ll sum2 = getRes(x0, nx, a12);
			ll pom = mn(d(a01, i), mn(d(a02, j), sum2));
			res = (res + pom) % MOD;
		}
	}
	if (DEBUG)printf("ALL THREE = %lld\n", res);
	return res;
}

ll single(int kt) {
	if (DEBUG)printf("SINGLE %d - %d\n", kt, s[n][r[kt]]);
	return s[n][r[kt]];
}

ll two(int x, int y) {
	if (DEBUG) printf("Compute for: %d, %d\n", x, y);
	int same = 0, diff = 0;
	REP(i, n) {
		if (a[x][i] == a[y][i]) {
			++same;
		} else {
			++diff;
		}
	}
	if (DEBUG) printf("Same: %d, Diff: %d\n", same, diff);
	ll res = 0;
	REP(niezgoda, same + 1) {
		// if (DEBUG)printf("\tniezgoda: %d\n", niezgoda);
		if (niezgoda > min(r[x], r[y])) {
			continue;
		}
		int z0 = r[x] - niezgoda, z1 = r[y] - niezgoda;
		if (z0 + z1 < diff) {
			continue;
		}
		ll spos = d(same, niezgoda);
		REP(niezgodaX, z0 + 1) {
			// if(DEBUG) printf("\t\tniezgodaX: %d\n", niezgodaX);
			int niezgodaY = diff - niezgodaX;
			if (niezgodaY > z1) {
				continue;
			}
			ll pom = (spos * d(diff, niezgodaX)) % MOD;
			// if (DEBUG) printf("\t\t\tADD: %lld\n", pom);
			res = (res + pom) % MOD;
		}
	}
	if (DEBUG)printf("DOUBLE %d - %d = %lld\n", x, y, res);
	return res;
}

int main() {
	scanf("%d", &n);
	bool caly = false;
	REP(i, K) {
		scanf("%d %s", &r[i], a[i]);
	}
	s[0][0] = 1;
	FOR(i, 1, n) {
		s[i][0] = d(i, 0);
		FOR(j, 1, i - 1) {
			int pom = (d(i - 1, j) + d(i-1, j-1)) % MOD;
			s[i][j] = (s[i][j - 1] + pom) % MOD;
		}
		s[i][i] = s[i][i - 1] + 1;
	}
	ll res = 0;
	REP(i, K) {
		res = (res + single(i)) % MOD;
	}
	REP(i, K) {
		FOR(j, i+1, K - 1) {
			res = (res - two(i, j) + MOD) % MOD;
		}
	}
	res = (res + allThreeWz()) % MOD;
	printf("%lld\n", res);
	return 0;
}