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
235
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fi first
#define se second
#define pii pair <int, int>
#define pli pair <ll, int>
#define pll pair <ll, ll>
#define ppi pair <pii, pii>
#define ppii pair <pii, pii>
#define ppli pair <pli, pli>
#define ppll pair <pli, pll>
#define umap unordered_map
using namespace std;

const ll MIN_INF = -(1ll<<61);

template <typename T>
ostream & operator << (ostream &out, const vector <T> &V);

template <typename T>
ostream & operator << (ostream &out, const set <T> &S);

template <typename T1, typename T2>
ostream & operator << (ostream &out, const pair <T1, T2> &P);

template <typename T1, typename T2>
ostream & operator << (ostream &out, const map <T1, T2> &M);

template <typename T1, typename T2>
ostream & operator << (ostream &out, const umap <T1, T2> &M);


const int MAX = 200000, q = 1000000007;
const ll Q = 1ll * q * q;

int n;


ll fast_power(ll a, ll k) {
    if(k == 0) {
        return 1;
    }
    return (fast_power((a*a) % q, k/2) * ((k % 2 == 0) ? 1 : a)) % q;
}

ll inverse_modulo(ll a) {        //a^(-1) mod q = a^(q-2) mod q (Small Fermat Theorem)
    return fast_power(a, q-2);
}


ll Sil[MAX], Rev_sil[MAX];
void init_factorion(int n) {
    Sil[0] = Sil[1] = Rev_sil[0] = Rev_sil[1] = 1;
    for (int i = 2; i < n; i++) {
        Sil[i] = (Sil[i-1] * i) % q;
        Rev_sil[i] = fast_power(Sil[i], q-2);
    }
}

ll newton(int n, int k) {
    return (n >= k) ? (((Sil[n] * Rev_sil[k]) % q) * Rev_sil[n-k]) % q : 0;
}


int R[3]; string Cntr[3];
int G[8] = {0, 0, 0, 0, 0, 0, 0, 0};
int Suf[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
int 
const ll MAX_1 = (1<<14), MAX_2 = MAX_1 * MAX_1, MAX_3 = MAX_2 * MAX_1;

umap <ll,ll> M;


ll solve(int i, int r0, int r1, int r2) {
    if (i >= 8) {
        return 1;
    }
    ll ind = (i + 1) * MAX_3 + (r0 + 1) * MAX_2 + (r1 + 1) * MAX + (r2 + 1);
    if (M.find(ind) != M.end()) {
        return M[ind];
    }
    ll ans = 0; int g = G[i]; //, min_r = (r0 <= r1) ? (r0 <= r2 ? r0 : r2) : (r1 <= r2 ? r1 : r2);
    
    for (int k = 0; k <= g; k++) {
        int gk = g - k, p[3];
        for (int b = 0; b < 3; b++) {
            p[b] = (i & (1<<b)) ? gk : k;
        }
        if (p[0] <= r0 && p[1] <= r1 && p[2] <= r2) {
            ans += newton(g, k) * solve(i+1, r0 - p[0], r1 - p[1], r2 - p[2]);
            if (ans >= Q) {
                ans %= q;
            }
        }
    }
    if (ans >= q) {
        ans %= q;
    }
    M[ind] = ans;
    cout << i << " " << r0 << " " << r1 << " " << r2 << " | " << ans <<endl;
    return ans;
}

ll solve_bad(int i, int r0, int r1, int r2) {
    if (r0 > R[0] && r1 > R[1] && r2 > R[2]) {
        ll ans = 1;
        for (int j = i; j < 8; j++) {
            ans *= fast_power(2, G[j]);
            if (ans >= q) {
                ans %= q;
            }
        }
        return ans;
    }
    if (i >= 8) {
        return 0;
    }
    int max_lac = max(R[0] - r0, max(R[1] - r1, R[2] - r2));
    if (max_lac > Suf[i]) {
        return 0;
    }
    ll ind = (i + 1) * MAX_3 + (r0 + 1) * MAX_2 + (r1 + 1) * MAX + (r2 + 1);
    if (M.find(ind) != M.end()) {
        return M[ind];
    }
    ll ans = 0; int g = G[i];
    
    for (int k = 0; k <= g; k++) {
        int gk = g - k, p[3];
        for (int b = 0; b < 3; b++) {
            p[b] = (i & (1<<b)) ? gk : k;
        }
        ans += newton(g, k) * solve_bad(i+1, r0 + p[0], r1 + p[1], r2 + p[2]);
        if (ans >= Q) {
            ans %= q;
        }
    }
    if (ans >= q) {
        ans %= q;
    }
    M[ind] = ans;
    //cout << i << " " << r0 << " " << r1 << " " << r2 << " | " << ans <<endl;
    return ans;
}


void testcase() {
    cin >> n;
    for (int i = 0; i < 3; i++) {
        cin >> R[i] >> Cntr[i];
    }
    init_factorion(MAX);
    for (int i = 0; i < n; i++) {
        G[(Cntr[0][i]-'0') + 2 * (Cntr[1][i]-'0') + 4 * (Cntr[2][i]-'0')]++;
    }
    for (int i = 7; i >= 0; i--) {
        Suf[i] = Suf[i+1] + G[i];
    }
    cout << (fast_power(2, n) - solve_bad(0, 0, 0, 0) + q) % q <<endl;
    //cout << M <<endl;
}

int main()
{
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int T = 1;
    //cin >> T;
    for (int t = 0; t < T; t++) {
        testcase();
    }
    return 0;
}

template <typename T>
ostream & operator << (ostream &out, const vector <T> &V) {
	if (!V.empty()) {
		out << "{";
		for (auto v : V)
			out << v << ", ";
		out << "\b\b}";			// \b is backspace
	}
	else {
		out << "{}";
	}
	return out;
}

template <typename T>
ostream & operator << (ostream &out, const set <T> &S) {
	if (!S.empty()) {
		out << "set{";
		for (auto v : S)
			out << v << ", ";
		out << "\b\b}";			// \b is backspace
	}
	else {
		out << "{}";
	}
	return out;
}

template <typename T1, typename T2>
ostream & operator << (ostream &out, const map <T1,T2> &M) {
	if (!M.empty()) {
		out << "map{";
		for (auto &kv : M)
			out << "(" << kv.fi << ": " << kv.se << ")" << ", ";
		out << "\b\b}";			// \b is backspace
	}
	else {
		out << "{}";
	}
	return out;
}

template <typename T1, typename T2>
ostream & operator << (ostream &out, const umap <T1,T2> &M) {
	if (!M.empty()) {
		out << "umap{";
		for (auto &kv : M)
			out << "(" << kv.fi << ": " << kv.se << ")" << ", ";
		out << "\b\b}";			// \b is backspace
	}
	else {
		out << "{}";
	}
	return out;
}

template <typename T1, typename T2>
ostream & operator << (ostream &out, const pair <T1, T2> &P) {
	out << "{" << P.first << ", " << P.second << "}";
	return out;
}