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
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;

const ll MOD = 1000000007;

vector<ll> sil(10001), inv(10001);

int dp[2][401][401][401];
int n;
vector<int> r(3);
vector<string> s(3);

ll Pot(ll p, ll w)
{
    if (w == 0)
        return 1;
    if (w % 2 == 0)
    {
        ll pol = Pot(p, w / 2);
        return pol * pol % MOD;
    }
    return Pot(p, w - 1) * p % MOD;
}

ll C(ll n, ll k)
{
    return ((sil[n] * inv[k]) % MOD * inv[n - k]) % MOD;
}

ll Jedna(int n, int r)
{
    ll wyn = 0;
    for (ll i = 0; i <= r; ++i)
        wyn = (wyn + C(n, i)) % MOD;
    return wyn;
}

ll Dwie(string x, string y, int rx, int ry)
{
    ll odp = 0;
    int s = 0, d = 0;
    for (int i = 0; i < n; ++i)
    {
        if (x[i] == y[i])
            ++s;
        else
            ++d;
    }
    for (ll i = 0; i <= min(s, min(rx, ry)); ++i)
        for (ll j = 0; j + i <= rx && j <= d; ++j)
            if (i + (d - j) <= ry)
                odp += (C(s, i) * C(d, j)) % MOD;
    return odp;
}

int main()
{
    ios_base::sync_with_stdio(0);
    sil[0] = 1;
    inv[0] = 1;
    for (ll i = 1; i < 10001; ++i)
    {
        sil[i] = (sil[i - 1] * i) % MOD;
        inv[i] = Pot(sil[i], MOD - 2);
    }
    cin >> n;
    for (int i = 0; i < 3; ++i)
        cin >> r[i] >> s[i];
    //cout << n << " " << max(r[0], max(r[1], r[2])) << endl;
    if (s[0] == s[1] && r[0] == r[1] && s[0] == s[2] && r[1] == r[2])
    {
        cout << Jedna(n, r[0]);
        return 0;
    }
    if (s[0] == s[1] || s[0] == s[2] || s[1] == s[2])
    {
        string x = s[0], y;
        int rx = s[0].size(), ry;
        if (s[0] == s[1])
        {
            y = s[2];
            ry = s[2].size();
        }
        else
        {
            y = s[1];
            ry = s[1].size();
        }
        ll odp = (Jedna(n, rx) + Jedna(n, ry)) % MOD;
        odp -= Dwie(x, y, rx, ry);
        if (odp < 0)
            odp += MOD;
        cout << odp;
        return 0;
    }
    ll odp = Jedna(n, r[0]) + Jedna(n, r[1]) + Jedna(n, r[2]) -
            Dwie(s[0], s[1], r[0], r[1]) - Dwie(s[0], s[2], r[0], r[2]) - Dwie(s[1], s[2], r[1], r[2]);
    while (odp < 0)
        odp += MOD;
    odp %= MOD;
    if (s[0][0] == s[1][0] && s[1][0] == s[2][0])
    {
        dp[0][0][0][0] = 1;
        if (r[0] >= 1 && r[1] >= 1 && r[2] >= 1)
            dp[0][1][1][1] = 1;
    }
    if (s[0][0] == s[1][0] && s[0][0] != s[2][0])
    {
        if (r[0] >= 1 && r[1] >= 1)
            dp[0][1][1][0] = 1;
        if (r[2] >= 1)
            dp[0][0][0][1] = 1;
    }
    if (s[0][0] == s[2][0] && s[0][0] != s[1][0])
    {
        if (r[0] >= 1 && r[2] >= 1)
            dp[0][1][0][1] = 1;
        if (r[1] >= 1)
            dp[0][0][1][0] = 1;
    }
    if (s[1][0] == s[2][0] && s[0][0] != s[1][0])
    {
        if (r[1] >= 1 && r[2] >= 1)
            dp[0][0][1][1] = 1;
        if (r[0] >= 1)
            dp[0][1][0][0] = 1;
    }
    for (int i = 0; i < n - 1; ++i)
    {
        int p = i % 2;
        for (int j = 0; j <= r[0]; ++j)
        {
            for (int k = 0; k <= r[1]; ++k)
            {
                for (int l = 0; l <= r[2]; ++l)
                {
                    if (s[0][i + 1] == s[1][i + 1] && s[1][i + 1] == s[2][i + 1])
                    {
                        dp[p ^ 1][j][k][l] = (dp[p ^ 1][j][k][l] + dp[p][j][k][l]) % MOD;
                        dp[p ^ 1][j + 1][k + 1][l + 1] = (dp[p ^ 1][j + 1][k + 1][l + 1] + dp[p][j][k][l]) % MOD;
                    }
                    if (s[0][i + 1] == s[1][i + 1] && s[0][i + 1] != s[2][i + 1])
                    {
                        dp[p ^ 1][j + 1][k + 1][l] = (dp[p ^ 1][j + 1][k + 1][l] + dp[p][j][k][l]) % MOD;
                        dp[p ^ 1][j][k][l + 1] = (dp[p ^ 1][j][k][l + 1] + dp[p][j][k][l]) % MOD;
                    }
                    if (s[0][i + 1] == s[2][i + 1] && s[0][i + 1] != s[1][i + 1])
                    {
                        dp[p ^ 1][j + 1][k][l + 1] = (dp[p ^ 1][j + 1][k][l + 1] + dp[p][j][k][l]) % MOD;
                        dp[p ^ 1][j][k + 1][l] = (dp[p ^ 1][j][k + 1][l] + dp[p][j][k][l]) % MOD;
                    }
                    if (s[1][i + 1] == s[2][i + 1] && s[0][i + 1] != s[1][i + 1])
                    {
                        dp[p ^ 1][j][k + 1][l + 1] = (dp[p ^ 1][j][k + 1][l + 1] + dp[p][j][k][l]) % MOD;
                        dp[p ^ 1][j + 1][k][l] = (dp[p ^ 1][j + 1][k][l] + dp[p][j][k][l]) % MOD;
                    }
                }
            }
        }
        for (int j = 0; j <= r[0]; ++j)
            for (int k = 0; k <= r[1]; ++k)
                for (int l = 0; l <= r[2]; ++l)
                    dp[p][j][k][l] = 0;
    }
    for (int i = 0; i <= r[0]; ++i)
        for (int j = 0; j <= r[1]; ++j)
            for (int k = 0; k <= r[2]; ++k)
                odp = (odp + dp[(n - 1) % 2][i][j][k]) % MOD;
    cout << odp;
    return 0;
}