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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// TEMPLATE START

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ull> vull;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<int,int> pii;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;


const int MOD = 1000000007;
const int INF = 2147483647;
const ll LINF = 1e18;
const ld PI = 4*atan((ld)1);

#define rep(i,a,b) for (int i=(a); i<(b); ++i)
#define repd(i,a,b) for (int i=((int)(a)); i>=(b); --i)
#define trav(a,x)  for (auto& a : x)
#define all(u) begin(u), end(u)
#define sz(x) (int)x.size()
#define xsort(v, m) rep(i, 0, v.size()) m[i] = i; sort(all(m), [=](int i, int j) {return v[i] < v[j];}); sort(v);

#define pb push_back
#define X first
#define Y second

#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define PREC(n) cout<<fixed<<setprecision(n);

#define db(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl;
#define dbv(v) cerr << __LINE__ << ": " << #v << "\n" << v.size() << "\n" << v << "\n";


template <class T, class S> ostream& operator << (ostream& os, const pair<T, S>& p) {
    os << p.X << " " << p.Y;
    return os;
}

template <class T, class S> istream& operator >> (istream& is,  pair<T, S>& p) {
    is >> p.X >> p.Y;
    return is;
}

template <class T> ostream& operator << (ostream& os, const vector<T>& v) {
    rep(i, 0, v.size())
        os << v[i] << " \n" [i == v.size() - 1];
    return os;
}

template <class T> istream& operator >> (istream& is,  vector<T>& v) {
    rep(i, 0, v.size())
        is >> v[i];
    return is;
}


// TEMPLATE END


// SOLUTION START

vvi bins;

bool areEqual(int r1, string& k1, int r2, string& k2) {
    rep(i, 0, k1.size())
        if(k1[i] != k2[i])
            return false;
    return true;
}

int madd(int x, int y) {
    return (x + y) % MOD;
}

int msub(int x, int y) {
    return (x + MOD - y) % MOD;
}

int mmul(int x, int y) {
    return ((long long)x * (long long)y) % (long long)MOD;
}

void swap(int& x, int& y) {
    int tmp = x;
    x = y;
    y = tmp;
}


int binomial(int n, int m) {
    return (n < m) ? 0 : bins[n + 1][m + 1];
}

int in1(int r1, string& k1) {
    int r = 0, n = k1.size();
    rep(i, 0, r1 + 1) {
        r = madd(r, binomial(n, i));
    }
    return r;
}

int hamming2(string& k1, string& k2) {
    int d = 0;
    rep(i, 0, k1.size()) {
        if(k1[i] != k2[i])
            d++;
    }
    return d;
}

int hamming3(string& k1, string& k2, string& k3, int& d1, int& d2, int& d3) {
    int d = 0;
    d1 = d2 = d3 = 0;
    rep(i, 0, k1.size()) {
        if(k1[i] == k2[i] && k1[i] != k3[i]) {d3++; d++;}
        else if(k1[i] == k3[i] && k2[i] != k1[i]) {d2++; d++;}
        else if(k2[i] == k3[i] && k2[i] != k1[i]) {d1++; d++;}
    }
    return d;
}

int in2(int r1, string& k1, int r2, string& k2) {
    int r = 0, tmp, n = k1.size(), d = hamming2(k1, k2);
    rep(i, max(d - r2, 0), min(r1, d) + 1) {
        tmp = 0;
        rep(j, 0, min(r1 - i, r2 - d + i) + 1) {
            tmp = madd(tmp, binomial(n - d, j));
        }
        r = madd(r, mmul(binomial(d, i), tmp));
    }
    return r;
}

void pref(vi& v, int n) {
    v[0] = bins[n + 1][1];
    for (int i = 1; i <= n; i++)
        v[i] = madd(v[i - 1], bins[n + 1][i + 1]);
}

int in3(int r1, string& k1, int r2, string& k2, int r3, string& k3) {
    int d1, d2, d3, x1, x2, x3, m;
    int r = 0, tmp, ttmp, tttmp, n = sz(k1), d = hamming3(k1, k2, k3, d1, d2, d3);
    vi prefix(n - d + 1);
    pref(prefix, n - d);
    rep(i, max(0, d1 - min(r2, r3)), min(d1, r1) + 1) {
        x1 = d1 - i; // x1 <= r2 && x1 <= r3
        tmp = binomial(d1, i);
        rep(j, max(0, d2 - min(r1 - i, r3 - x1)), min(d2, r2 - x1) + 1) {
            x2 = d2 - j; // x2 <= r1 - i && x2 <= r3 - x1
            ttmp = binomial(d2, j);
            rep(k, max(max(x1 + d3 + j - r2, x2 + d3 + i - r1), 0), min(d3, r3 - x1 - x2) + 1) {
                x3 = d3 - k;
                m = min(min(min(r1 - (x2 + x3 + i), r2 - (x1 + x3 + j)), r3 - (x1 + x2 + k)), n - d);
                tttmp = binomial(d3, k);
                r = madd(r, mmul(tmp, mmul(ttmp, mmul(tttmp, prefix[m]))));
            }
        }
    }
    return r;
}

void init(vvi& bins, int n) {
    bins.pb(vi(1));
    bins.pb(vi(2, 1));
    rep(i, 2, n + 1) {
        bins.pb(vi(i + 1));
        bins[i][1] = bins[i - 1][1];
        rep(j, 2, i) {
            bins[i][j] = madd(bins[i - 1][j], bins[i - 1][j - 1]);
        }
        bins[i][i] = bins[i - 1][i - 1];
    }
}

int main() {
    FAST_IO
    int n, r1, r2, r3, r = 0;
    cin >> n;
    init(bins, n + 1);
    string k1, k2, k3;
    cin >> r1 >> k1 >> r2 >> k2 >> r3 >> k3;
    bool eq = false;
    if(areEqual(r1, k1, r3, k3)) {
        if(r1 < r3) {
            swap(r1, r3);
            swap(k1, k3);
        }
        eq = true;
    }
    if(areEqual(r2, k2, r3, k3)) {
        if(r2 < r3) {
            swap(r2, r3);
            swap(k2, k3);
        }
        eq = true;
    }
    if(areEqual(r1, k1, r2, k2)) {
        if(r1 < r2) {
            swap(r1, r2);
            swap(k1, k2);
        }
        swap(r2, r3);
        swap(k2, k3);
        eq = true;
    }
    if(eq) {
        r = msub(madd(in1(r1, k1), in1(r1, k1)), in2(r1, k1, r2, k2));
    }
    else {
        r = madd(r, madd(madd(in1(r1, k1), in1(r2, k2)), in1(r3, k3)));
        r = msub(msub(msub(r, in2(r1, k1, r2, k2)), in2(r1, k1, r3, k3)), in2(r2, k2, r3, k3));
        r = madd(r, in3(r1, k1, r2, k2, r3, k3));
    }
    cout << r << "\n";
    return 0;
}

// SOLUTION END

/*
6
3 000000
3 000001
2 000011
*/