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
#include <cstdio>
#include <iostream>
#include <vector>
#include <string>
#define LL long long
using namespace std;

const LL MAX = 10005;
const LL MOD = 1000000007;

LL silnia[MAX];

void oblicz_silnie() {
	silnia[0] = 1;
	for (int i = 1; i < MAX; ++i)
		silnia[i] = (silnia[i - 1] * i) % MOD;
}

LL potega(LL podstawa, LL wykladnik) {
	if (wykladnik == 0)
		return 1;
	LL wyn = potega(podstawa, wykladnik / 2);
	wyn = (wyn * wyn) % MOD;
	if (wykladnik % 2 == 1)
		return (wyn * podstawa) % MOD;
	return wyn;
}

LL a_nad_b_mod_p_pierwsze(LL a, LL b) {
	if (a == 0) {
		return 1;
	}
	return (((silnia[a] * potega(silnia[b], MOD - 2)) % MOD) * potega(silnia[a - b], MOD - 2)) % MOD;
}

int typ[4] = {0, 0, 0, 0};
int num[4];
int r[3];

LL sum = 0;
int part[3] = {0, 0, 0};
int x;
LL a_nad_b[4][MAX];

void solve(int p) {
	if (p < 4) {
		for (int i = 0; i <= typ[p]; ++i) {
			num[p] = i;
			for (int j = 0; j <= 2; ++j)
				part[j] += ((p == (j + 1)) ? (typ[p] - num[p]) : num[p]);
			if (part[0] <= r[2] || part[1] <= r[1] || part[2] <= r[0])
				solve(p + 1);
			for (int j = 0; j <= 2; ++j)
				part[j] -= ((p == (j + 1)) ? (typ[p] - num[p]) : num[p]);
		}
	} else {
		LL w = 1;
		for (int i = 0; i < 4; ++i)
			w = (w * a_nad_b[i][num[i]]) % MOD;
		sum = (sum + w) % MOD;
	}
}

int main() {
	oblicz_silnie();
	int n, i, j;
	cin >> n;
	string s[3];
	for (i = 0; i < 3; ++i) {
		cin >> r[i] >> s[i];
	}
	for (i = 0; i < n; ++i) {
		if (s[0][i] == s[1][i] && s[1][i] == s[2][i])
			typ[0]++;
		else if (s[0][i] == s[1][i])
			typ[1]++;
		else if (s[0][i] == s[2][i])
			typ[2]++;
		else
			typ[3]++;
	}
	
	for (i = 0; i < 4; ++i)
		for (j = 0; j <= typ[i]; ++j)
			a_nad_b[i][j] = a_nad_b_mod_p_pierwsze(typ[i], j);
	
	solve(0);
	cout << sum;
	return 0;
}