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
#include<iostream>
#include<assert.h>
#include<cstdio>
#include<string>
#include<cmath>
#include<bitset>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<utility>
using namespace std;

const int MAX_DIMENSION = 10001;
const int MOD = 1e9 + 7;

struct Ball {
	int dimension;
	std::string center;
	int radius;
};


int cache[MAX_DIMENSION][MAX_DIMENSION];
int rows[MAX_DIMENSION][MAX_DIMENSION];

void Init(int N) {
	for (int n = 0; n <= N; n++) {
		for (int k = 0; k <= N; k++) {
			cache[n][k] = 0;
			rows[n][k] = 0;
		}
	}
}

int BinomialCoefficient(int n, int k) {
	if (n == k || k == 0) {
		return 1;
	}
	if(cache[n][k] > 0) {
		return cache[n][k];
	}
	int v = BinomialCoefficient(n - 1, k - 1) + BinomialCoefficient(n - 1, k);
	v %= MOD;
	cache[n][k] = v;
	return v;
}

void ComputeRowSums(int N) {
	for (int n = 0; n <= N; n++) {
		rows[n][0] = 1;
		for (int k = 1; k <= n; k++) {
			rows[n][k] = rows[n][k - 1] + BinomialCoefficient(n, k);
			rows[n][k] %= MOD;
		}
	}
}

int GetSegmentSum(int n, int k1, int k2) {
	return (rows[n][k2] - (k1 == 0 ? 0 : rows[n][k1 - 1]) + MOD) % MOD;
}

int CountPointsInBall(const Ball& b) {
	long long points = 0;
	for (int r = 0; r <= b.radius; ++r) {
		points += BinomialCoefficient(b.dimension, r);
	}
	points %= MOD;
	return static_cast<int>(points);
}

int CountPointsInTwoBallsIntersection(const Ball& b1, const Ball& b2) {
	int equal = 0;
	int different = 0;
	for (int i = 0; i < b1.dimension; i++) {
		if (b1.center[i] == b2.center[i]) {
			++equal;
		} else {
			++different;
		}
	}

	long long points = 0;
	for (int e = 0; e <= std::min(equal, std::min(b1.radius, b2.radius)); ++e) {
		int left = std::max(e + different - b2.radius, 0);
		int right = std::min(b1.radius - e, different);
		if (left > right) {
			continue;
		}
		long long v = (long long)BinomialCoefficient(equal, e) * GetSegmentSum(different, left, right);
		v %= MOD;
		points += v;
	}
	points %= MOD;
	return static_cast<int>(points);
}

int CountPointsInThreeBallsIntersection(const Ball& b1,
	const Ball& b2, const Ball& b3) {
	int A = 0;
	int B = 0;
	int C = 0;
	int D = 0;
	for (int i = 0; i < b1.dimension; i++) {
		if (b1.center[i] == b2.center[i] && b2.center[i] == b3.center[i]) {
			A++;
		} else if (b1.center[i] == b3.center[i]) {
			B++;
		} else if (b2.center[i] == b3.center[i]) {
			C++;
		} else {
			D++;
		}
	}

	if (A + B == b1.dimension && b1.radius == b3.radius) {
		return CountPointsInTwoBallsIntersection(b1, b2);
	}
	if (A + C == b1.dimension && b2.radius == b3.radius) {
		return CountPointsInTwoBallsIntersection(b1, b2);
	}
	if (A + D == b1.dimension && b1.radius == b2.radius) {
		return CountPointsInTwoBallsIntersection(b1, b3);
	}

	int r = std::min(std::min(b1.radius, b2.radius), b3.radius);

	long long points = 0;
	for (int a = 0; a <= std::min(A, r); a++) {
		long long v = 0;
		for(int b = 0; b <= B && a + b <= b1.radius && a + b <= b3.radius && a + (B - b) <= b2.radius; b++) {
			long long w = 0;
			int X = a + b + C + D - b3.radius;
			int Y = b1.radius - a - b;
			int Z = b2.radius - a - B + b - C;
			for (int c = std::max(std::max((X - Z) / 2, X - D), 0); c <= std::min(Y, C); c++) {
				int left = std::max(X - c, 0);
				int right = std::min(std::min(Y - c, Z + c), D);
				if (left > right) {
					continue;
				}
				long long z = (long long)BinomialCoefficient(C, c) * GetSegmentSum(D, left, right);
				z %= MOD;
				w += z;
			}
			w %= MOD;
			w *= BinomialCoefficient(B, b);
			w %= MOD;
			v += w;
		}
		v %= MOD;
		v *= BinomialCoefficient(A, a);
		v %= MOD;
		points += v;
	}
	points %= MOD;
	return static_cast<int>(points);
}

int main() {
	std::ios::sync_with_stdio(false);
	// Read input
	int n;
	std::cin >> n;
	Ball b1{.dimension = n};
	std::cin >> b1.radius >> b1.center;
	Ball b2{.dimension = n};
	std::cin >> b2.radius >> b2.center;
	Ball b3{.dimension = n};
	std::cin >> b3.radius >> b3.center;

	// Preprocessing
	Init(n);
	ComputeRowSums(n);

	long long result =
		CountPointsInBall(b1)
		+ CountPointsInBall(b2)
		+ CountPointsInBall(b3)
		- CountPointsInTwoBallsIntersection(b1, b2)
		- CountPointsInTwoBallsIntersection(b2, b3)
		- CountPointsInTwoBallsIntersection(b1, b3)
		+ CountPointsInThreeBallsIntersection(b1, b2, b3);
	std::cout << (((result % MOD) + MOD) % MOD) << '\n';
	return 0;
}