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
#include <bits/stdc++.h>
#define ll long long
#define mk std::make_pair
#define st first
#define nd second

const int MAX_N = 1e4;
ll bin[MAX_N+3][MAX_N+3];
const ll MOD = 1e9+7;
int n;

void prepare(){
    bin[0][0] = 1;
    for (int i = 1; i <= n; i++){
        bin[i][0] = 1;
        for (int j = 1; j < i; j++)
            bin[i][j] = (bin[i-1][j-1] + bin[i-1][j]) % MOD;
        bin[i][i] = 1;
    }
    ll S;
    for (int i = 1; i <= n; i++){
        S = 0;
        for (int j = 0; j <= i; j++){
            S = (S + bin[i][j]) % MOD;
            bin[i][j] = S;
        }
    }
}

ll sum(int row, int l, int r){
    if (l > r)
        return 0;
    r = std::min(r, row);
    if (l == 0)
        return bin[row][r];
    return (bin[row][r] - bin[row][l-1] + MOD) % MOD;
}

std::pair<int, std::string> A, B, C;

ll solve1(std::pair<int, std::string> &A){
    return sum(n, 0, A.st);
}

ll solve2(std::pair<int, std::string> &A, std::pair<int, std::string> &B){
    int S = 0;
    for (int i = 0; i < n; i++)
        if (A.nd[i] == B.nd[i])
            S++;
    ll res = 0;
    for (int x1 = 0; x1 <= S; x1++){
        int L = std::max(0, x1 + n - S - B.st);
        int R = std::min(n - S, A.st - x1);
        ll tmp = sum(n - S, L, R);
        tmp = (tmp * sum(S, x1, x1)) % MOD;
        res = (res + tmp) % MOD;
    }
    return res;
}

ll solve3(std::pair<int, std::string> &A, std::pair<int, std::string> &B, std::pair<int, std::string> &C){
    int S_123, S_12, S_13, S_23;
    S_123 = S_12 = S_13 = S_23 = 0;
    for (int i = 0; i < n; i++){
        if (A.nd[i] == B.nd[i] && A.nd[i] == C.nd[i])
            S_123++;
        else if (A.nd[i] == B.nd[i])
            S_12++;
        else if (A.nd[i] == C.nd[i])
            S_13++;
        else
            S_23++;
    }
    ll res = 0;
    for (int x4 = 0; x4 <= S_123; x4++){
        for (int x3 = 0; x3 <= S_12; x3++){
            for (int x2 = 0; x2 <= S_13; x2++){
                int L = std::max(0, x4 + x3 + x2 + S_23 - A.st);
                int R = std::min(S_23, B.st - x4 - x3 - S_13 + x2);
                R = std::min(R, C.st - x4 - x2 - S_12 + x3);
                ll tmp = sum(S_23, L, R);
                tmp = (tmp * sum(S_123, x4, x4)) % MOD;
                tmp = (tmp * sum(S_12, x3, x3)) % MOD;
                tmp = (tmp * sum(S_13, x2, x2)) % MOD;
                res = (res + tmp) % MOD;
            }
        }
    }
    return res;
}

int main(){
    std::ios_base::sync_with_stdio(0); std::cin.tie(NULL);
    std::cin >> n >> A.st >> A.nd >> B.st >> B.nd >> C.st >> C.nd;
    prepare();
    if (A == B && A == C){
        // wszystkie sa takie same
        std::cout << solve1(A) << "\n";
    }
    else if (A == B){
        // dwie kule
        ll X = solve1(A);
        ll Y = solve1(C);
        ll Z = solve2(A, C);
        ll res = (X+Y)%MOD;
        res = (res - Z + MOD)%MOD;
        std::cout << res << "\n";
    }
    else if (B == C){
        // dwie kule
        ll X = solve1(B);
        ll Y = solve1(A);
        ll Z = solve2(B, A);
        ll res = (X+Y)%MOD;
        res = (res - Z + MOD)%MOD;
        std::cout << res << "\n";
    }
    else if (A == C){
        // dwie kule
        ll X = solve1(A);
        ll Y = solve1(B);
        ll Z = solve2(A, B);
        ll res = (X+Y)%MOD;
        res = (res - Z + MOD)%MOD;
        std::cout << res << "\n";
    }
    else{
        // trzy rozne
        ll X1 = solve1(A);
        ll X2 = solve1(B);
        ll X3 = solve1(C);
        ll Y1 = solve2(A, B);
        ll Y2 = solve2(A, C);
        ll Y3 = solve2(B, C);
        ll Z = solve3(A, B, C);
        ll res = (X1+X2)%MOD;
        res = (res + X3)%MOD;
        res = (res - Y1 + MOD)%MOD;
        res = (res - Y2 + MOD)%MOD;
        res = (res - Y3 + MOD)%MOD;
        res = (res + Z)%MOD;
        std::cout << res << "\n";
    }
}