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
#include <cstdio>
#include <limits>
#include <vector>
#include <cstring>
#include <cassert>

#define MODULO 1000000007

class Modular {
	unsigned v;

public:
	Modular(void) { }

	Modular(unsigned v) : v(v) {}

	Modular(const Modular &other) : v(other.v) {}

	unsigned value() const {
		return v;
	}

	Modular operator+(const Modular &m) const {
		return Modular((v + m.v) % MODULO);
	}

	Modular operator-(const Modular &m) const {
		return Modular((MODULO + v - m.v) % MODULO);
	}

	Modular &operator+=(const Modular &m) {
		(v += m.v) %= MODULO;
		return *this;
	}

	Modular &operator-=(const Modular &m) {
		((v += MODULO) -= m.v) %= MODULO;
		return *this;
	}

	Modular operator*(const Modular &m) const {
		return Modular((static_cast<unsigned long long>(v) * static_cast<unsigned long long>(m.v)) % static_cast<unsigned long long>(MODULO));
	}
};

class Matrix
{
	Modular data[10001 * 10002 / 2];

public:
	Modular* operator[](size_t n) {
		size_t index = n * (n + 1) / 2;
		return data + index;
	}
};

//typedef Modular Matrix[10001][10001];

Matrix newton, balls;

void fill_newton(int N) {
	newton[0][0] = 1;
	for (int n = 1; n <= N; ++n) {
		newton[n][0] = 1;
		newton[n][n] = 1;
		for (int k = 1; k < n; ++k) {
			newton[n][k] = newton[n - 1][k - 1] + newton[n - 1][k];
		}
	}
}

void fill_balls(int N) {
	for (int n = 0; n <= N; ++n) {
		Modular value = 0;
		for (int r = 0; r <= n; ++r) {
			value += newton[n][r];
			balls[n][r] = value;
		}
	}
}

// N - wymiar, r - promienie, d - ilość różniących się bitów
Modular volume2(int N, int r1, int r2, int d) {
	Modular result = 0;
	const Modular *ball = balls[N - d];
	const int a_min = std::max(0, d - r2);
	const int a_max = std::min(r1, d);
	for (int a = a_min; a <= a_max; ++a) {
		const int R1 = r1 - a;
		const int R2 = r2 - (d - a);
		result += newton[d][a] * ball[std::min(N-d, std::min(R1, R2))];
	}
	return result;
}

Modular volume2(int N, const char *s1, int r1, const char *s2, int r2) {
	int d = 0;
	for (int i = 0; i < N; ++i) {
		d += (s1[i] != s2[i]);
	}
	return volume2(N, r1, r2, d);
}

Modular temp[10001][10001];

// N - wymiar, r - promienie, d1 - 100|011, d2 - 010|101, d3 - 001|110
Modular volume3(int N, int r1, int r2, int r3, int d1, int d2, int d3) {
	Modular result = 0;
	int iball = N - (d1 + d2 + d3);
	const Modular *ball = balls[iball];

	int Qmin = std::min(r1, std::min(r2, r3)) - (d1 + d2 + d3);
	for (int Q = 0; Q <= N; ++Q) {
		Modular x = 0;
		for (int a3 = 0; a3 <= N; ++a3) {
			if (a3 <= d3 && Qmin + Q + a3 >= 0) {
				x += newton[d3][a3] * ball[std::min(Qmin + Q + a3, iball)];
			}
			temp[Q][a3] = x;
		}
	}

	const int a1_min = std::max(0, std::max(d1 - r2, d1 - r3));
	const int a1_max = std::min(r1, d1);
	for (int a1 = a1_min; a1 <= a1_max; ++a1) {
		const int a23 = d1 - a1;
		const int R1 = r1 - a1;
		const int R2 = r2 - a23;
		const int R3 = r3 - a23;
		const int a2_min = std::max(0, std::max(d2 - R1, d2 - R3));
		const int a2_max = std::min(R2, d2);
		for (int a2 = a2_min; a2 <= a2_max; ++a2) {
			const int a13 = d2 - a2;
			const int RR1 = R1 - a13;
			const int RR2 = R2 - a2;
			const int RR3 = R3 - a13;
			const int a3_min = std::max(0, std::max(d3 - RR1, d3 - RR2));
			const int a3_max = std::min(RR3, d3);

			const Modular n12 = newton[d1][a1] * newton[d2][a2];
			const int RR12 = std::min(RR1, RR2) - d3;

			Modular z = 0;
			int a3_border = (RR3 < RR12) ? -1 : (RR3 - RR12) / 2;
			if (a3_border < a3_min) {
				z += temp[RR3 - d3 - Qmin][d3 - a3_min];
				if (a3_max < d3) {
					z -= temp[RR3 - d3 - Qmin][d3 - a3_max - 1];
				}
			} else if (a3_border >= a3_max) {
				z += temp[RR12 - Qmin][a3_max];
				if (a3_min > 0) {
					z -= temp[RR12 - Qmin][a3_min - 1];
				}
			} else {
				z += temp[RR12 - Qmin][a3_border];
				if (a3_min > 0) {
					z -= temp[RR12 - Qmin][a3_min - 1];
				}
				z += temp[RR3 - d3 - Qmin][d3 - (a3_border + 1)];
				if (a3_max < d3) {
					z -= temp[RR3 - d3 - Qmin][d3 - a3_max - 1];
				}
			}
			result += n12 * z;
		}
	}
	return result;
}

Modular volume3(int N, const char *s1, int r1, const char *s2, int r2, const char *s3, int r3) {
	int d1 = 0, d2 = 0, d3 = 0;
	for (int i = 0; i < N; ++i) {
		if (s1[i] != s2[i] || s2[i] != s3[i]) {
			d1 += (s2[i] == s3[i]);
			d2 += (s1[i] == s3[i]);
			d3 += (s1[i] == s2[i]);
		}
	}
	return volume3(N, r1, r2, r3, d1, d2, d3);
}

char s1[10003], s2[10003], s3[10003];

int main() {
	int N;
	scanf("%d", &N);
	fill_newton(N);
	fill_balls(N);

	int r1, r2, r3;
	scanf("%d %s", &r1, s1);
	scanf("%d %s", &r2, s2);
	scanf("%d %s", &r3, s3);

	Modular v1 = balls[N][r1], v2 = balls[N][r2], v3 = balls[N][r3];
	Modular v12 = volume2(N, s1, r1, s2, r2);
	Modular v13 = volume2(N, s1, r1, s3, r3);
	Modular v23 = volume2(N, s2, r2, s3, r3);
	Modular v123 = volume3(N, s1, r1, s2, r2, s3, r3);

	Modular result = (v1 + v2 + v3 + v123) - (v12 + v13 + v23);
	printf("%u\n", result.value());
}