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
#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

LL MOD = LL(1e9) + 9;

LL single(LL **binCoeff, int n, int r) {
	LL result = 0;
	for (int i = 0; i <= n && i <= r; i++)
		result = (result + binCoeff[n][i]) % MOD;
	return result;
}

LL interOf2(LL **binCoeff, int n, pair<int, string> ball1, pair<int, string> ball2) { //intersection of 2 hyperballs
	int r1 = ball1.first;
	int r2 = ball2.first;
	string coor1 = ball1.second;
	string coor2 = ball2.second;

	int diff = 0;
	for (int i = 0; i < n; i++)
		if (coor1[i] != coor2[i])
			diff++;
	
	if (r1 > r2) { // assume first hyperball has smaller radius
		swap(r1, r2);
		swap(coor1, coor2);
	}

	LL result = 0;

	for (int i = 0; i <= r1 && i <= diff; i++)
		for (int j = 0; j <= r1 - i; j++) {
			//cout << i << " " << j << " " << diff << " " << diff-i+j << " " << r2 << endl;
			if (diff - i + j > r2 )
				break;
			result = (result + (binCoeff[diff][i] * binCoeff[n - diff][min(j, n-diff)]) % MOD) % MOD;
		}
	return result;
}

LL interOf3(LL **binCoeff, int n, pair<int, string> hyperballs[]) {
	sort(hyperballs, hyperballs + 3);

	int r[] = {hyperballs[0].first, hyperballs[1].first, hyperballs[1].first};
	string coor[] = {hyperballs[0].second, hyperballs[1].second, hyperballs[2].second};
	
	if (coor[0] == coor[1] && r[0] == r[1])
		return interOf2(binCoeff, n, hyperballs[1], hyperballs[2]);
	else if (coor[1] == coor[2] && r[1] == r[2])
		return interOf2(binCoeff, n, hyperballs[0], hyperballs[1]);
	else
		return 0;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	
	int maxn;
	cin >> maxn;

	LL** C = new LL*[maxn + 1]; //Binomial Coefficient
	for(int i = 0; i <= maxn; ++i)
    		C[i] = new LL[maxn + 1];

	C[0][0] = 1;
	for (int n = 1; n <= maxn; ++n) {
    		C[n][0] = C[n][n] = 1;
    		for (int k = 1; k < n; ++k)
        		C[n][k] = C[n - 1][k - 1] + C[n - 1][k];
	}
	pair<int, string> hyperballs[3];

	LL result = 0;
	for (int i = 0; i < 3; i++) {
		int r;
		string coor;
		cin >> r;
		cin >> coor;
		hyperballs[i] = {r, coor};
		result += single(C, maxn, r);
	}
	
	for (int i = 0; i < 3; i++) {
		for (int j = i + 1;j < 3; j++) {
			result -= interOf2(C, maxn, hyperballs[i], hyperballs[j]);
			result = (result + MOD) % MOD;
		}
	}
	
	result = (result + interOf3(C, maxn, hyperballs)) % MOD;
	
	for(int i = 0; i <= maxn; ++i)
    		delete[] C[i];
		
	delete[] C;

	cout << result;
}