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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Karol Kosinski 2019
#include <cstdio>
#include <algorithm>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FOD(i,b,a) for(int i=(b-1);i>=(a);--i)
//#define DEBUG(x...) printf(x)
#define DEBUG(x...)
using namespace std;

typedef long long LL;
const int NX = 10009;
const LL MOD = 1'000'000'007;
const int DISJOINT = -1;
const int INTERSECT = -2;

struct Vers
{
    void read(char *c, int n) { FOR(i,0,n) v[i] = c[i] - '0'; }
    void clear(int n) { FOR(i,0,n) v[i] = 0; }
    int v[NX];
};

struct Ball
{
    int r;
    Vers coords;
};

struct gcd { LL x, y; };

char Aux[NX];
Ball B[3];
LL NewtonD[NX], NewtonN_D[NX];

gcd gcd_fun(LL a, LL b)
{
    if (b == 0) return (gcd){1, 0};
    gcd t = gcd_fun(b, a % b);
    return (gcd){t.y, t.x - (a / b) * t.y};
}

int dist(const Vers &a, const Vers &b, int n)
{
    int res = 0;
    FOR(i,0,n) if(a.v[i] ^ b.v[i]) ++res;
    return res;
}

bool is_point_in_ball(const Vers &p, const Ball &b, int n)
{
    return dist(p, b.coords, n) <= b.r;
}

LL algo_max_brut(int n)
{
    DEBUG("algo_max_brut\n");
    LL res = 0;
    Vers w;
    w.clear(n);
    FOR(i,0,3)
    {
        if (is_point_in_ball(w, B[i], n))
        {
            ++res;
            break;
        }
    }
    FOR(i,0,n)
    {
        w.v[i] = 1;
        do {
            FOR(i,0,3)
            {
                if (is_point_in_ball(w, B[i], n))
                {
                    ++res;
                    break;
                }
            }
        }
        while (prev_permutation(w.v, w.v + n));
    }
    return res % MOD;
}

LL algo_brut_inter_3(int n)
{
    DEBUG("++ algo_brut_inter_3\n");
    LL res = 0;
    Vers w;
    w.clear(n);
    int r_min = NX, ir = -1;
    FOR(i,0,3)
    {
        if (B[i].r >= r_min) continue;
        r_min = B[i].r;
        ir = i;
    }
    Ball *j1, *j2;
    switch(ir)
    {
        case 0:
            if (B[1].r < B[2].r)
                j1 = &B[1], j2 = &B[2];
            else
                j1 = &B[2], j2 = &B[1];
        break;
        case 1:
            if (B[0].r < B[2].r)
                j1 = &B[0], j2 = &B[2];
            else
                j1 = &B[2], j2 = &B[0];
        break;
        case 2:
            if (B[0].r < B[1].r)
                j1 = &B[0], j2 = &B[1];
            else
                j1 = &B[1], j2 = &B[0];
        break;
    }
    auto &coords = B[ir].coords;
    int *cv = coords.v, *wv = w.v;
    if (is_point_in_ball(coords, *j1, n) and
        is_point_in_ball(coords, *j2, n))
            res = 1;
    FOR(i,0,r_min)
    {
        wv[i] = 1;
        do {
            FOR(j,0,n) if (wv[j]) cv[j] ^= 1;
            if (is_point_in_ball(coords, *j1, n) and
                is_point_in_ball(coords, *j2, n))
                    ++res;
            FOR(j,0,n) if (wv[j]) cv[j] ^= 1;
        }
        while (prev_permutation(wv, wv + n));
    }
    DEBUG("-- algo_brut_inter_3 -- res = %lld\n", res % MOD);
    return res % MOD;
}

int inside(int ai, int bi, int n)
{
    //  a --> b in a
    //  b --> a in b
    // INTERSECT --> a and b != 0
    // DISJOINT  --> a and b == 0
    const auto &a = B[ai], &b = B[bi];
    int d = dist(a.coords, b.coords, n);
    if (a.r + b.r < d) return DISJOINT;
    if (d + b.r <= a.r) return ai;
    if (d + a.r <= b.r) return bi;
    return INTERSECT;
}

void fill_newton(LL *Newton, int lim)
{
    LL num = 1, den = 1;
    Newton[0] = 1;
    FOR(i,0,lim)
    {
        num = (num * (lim - i)) % MOD;
        den = (den * (i + 1)) % MOD;
        LL den_1 = (gcd_fun(MOD, den).y + MOD) % MOD;
        Newton[i + 1] = (num * den_1) % MOD;
    }
}

LL inter(int a, int b, int n)
{
    DEBUG("++ inter(%d, %d)\n", a, b);
    const auto &X = B[a], &Y = B[b];
    int d = dist(X.coords, Y.coords, n);
    int bigger = max(X.r, Y.r);
    int smaller = min(X.r, Y.r);
    int i = min(bigger, d);
    
    fill_newton(NewtonD, d);
    fill_newton(NewtonN_D, n - d);
    FOR(i,0,n-d) NewtonN_D[i + 1] = (NewtonN_D[i] + NewtonN_D[i + 1]) % MOD;
    
    LL res = 0;
    while(i >= d - smaller)
    {
        int delta = min(min(bigger - i, smaller - (d - i)), n - d);
        DEBUG("*** i, d-i = %d, %d     delta = %d\n", i, d - i, delta);
        res = (res + NewtonD[i] * NewtonN_D[delta]) % MOD;
        --i;
    }
    DEBUG("-- inter(%d, %d) -- res = %lld\n", a, b, res);
    return res;
}

LL vol(int a, int n)
{
    DEBUG("++ vol(%d)\n", a);
    const auto &X = B[a];
    LL res = 1, num = 1, den = 1;
    
    FOR(i,0,X.r)
    {
        num = (num * (n - i)) % MOD;
        den = (den * (i + 1)) % MOD;
        LL den_1 = (gcd_fun(MOD, den).y + MOD) % MOD;
        res = (res + num * den_1) % MOD;
    }
    DEBUG("-- vol(%d) -- res = %lld\n", a, res);
    return res;
}

LL sumvol(int a, int b, int n)
{
    DEBUG("sumvol(%d, %d)\n", a, b);
    return (vol(a, n) + vol(b, n) + MOD - inter(a, b, n)) % MOD;
}

LL sumvol_3(int a, int b, int c, int n)
{
    DEBUG("sumvol_3(%d, %d, %d)\n", a, b, c);
    return (vol(a, n) + vol(b, n) + vol(c, n)
        + 2 * MOD - inter(a, b, n) - inter(b, c, n)) % MOD;
}

LL algo_brut(int n)
{
    DEBUG("algo_brut\n");
    return (vol(0, n) + vol(1, n) + vol(2, n)
        + 3 * MOD - inter(0, 1, n) - inter(1, 2, n) - inter(2, 0, n)
        + algo_brut_inter_3(n)) % MOD;
}

LL algo(int n)
{
    int d_01 = inside(0, 1, n);
    int d_12 = inside(1, 2, n);
    int d_20 = inside(2, 0, n);
    DEBUG("(0,1): %d    (1,2): %d    (2,0): %d\n", d_01, d_12, d_20);
    
    if (d_01 == INTERSECT and d_12 == INTERSECT and d_20 == INTERSECT)
        return algo_brut(n);
    
    if (d_01 == INTERSECT and d_12 == INTERSECT and d_20 >= 0)
        return sumvol(d_20, 1, n);
    if (d_01 == INTERSECT and d_12 >= 0 and d_20 == INTERSECT)
        return sumvol(d_12, 0, n);
    if (d_01 >= 0 and d_12 == INTERSECT and d_20 == INTERSECT)
        return sumvol(d_01, 2, n);
    
    if (d_01 == INTERSECT and d_12 == INTERSECT and d_20 == DISJOINT)
        return sumvol_3(0, 1, 2, n);
    if (d_01 == INTERSECT and d_12 == DISJOINT and d_20 == INTERSECT)
        return sumvol_3(2, 0, 1, n);
    if (d_01 == DISJOINT and d_12 == INTERSECT and d_20 == INTERSECT)
        return sumvol_3(1, 2, 0, n);
    
    if (d_01 == DISJOINT and d_12 == DISJOINT and d_20 == DISJOINT)
        return (vol(0, n) + vol(1, n) + vol(2, n)) % MOD;
    
    if (d_01 == DISJOINT and d_12 == DISJOINT and d_20 >= 0)
        return (vol(1, n) + vol(d_20, n)) % MOD;
    if (d_01 == DISJOINT and d_12 >= 0 and d_20 == DISJOINT)
        return (vol(0, n) + vol(d_12, n)) % MOD;
    if (d_01 >= 0 and d_12 == DISJOINT and d_20 == DISJOINT)
        return (vol(2, n) + vol(d_01, n)) % MOD;
    
    if (d_01 == DISJOINT and d_12 == DISJOINT and d_20 == INTERSECT)
        return (vol(1, n) + sumvol(2, 0, n)) % MOD;
    if (d_01 == DISJOINT and d_12 == INTERSECT and d_20 == DISJOINT)
        return (vol(0, n) + sumvol(1, 2, n)) % MOD;
    if (d_01 == INTERSECT and d_12 == DISJOINT and d_20 == DISJOINT)
        return (vol(2, n) + sumvol(0, 1, n)) % MOD;
    
    if (d_01 == 1 and d_12 == 1)
        return vol(1, n);
    if (d_12 == 2 and d_20 == 2)
        return vol(2, n);
    if (d_20 == 0 and d_01 == 0)
        return vol(0, n);
    
    if (d_01 == 0 and d_12 == 2 and d_20 == INTERSECT)
        return sumvol(2, 0, n);
    if (d_01 == 1 and d_12 == INTERSECT and d_20 == 2)
        return sumvol(1, 2, n);
    if (d_01 == INTERSECT and d_12 == 1 and d_20 == 0)
        return sumvol(0, 1, n);
    
    if (d_01 >= 0 and d_12 == DISJOINT and d_20 == INTERSECT)
        return sumvol(2, 0, n);
    if (d_01 == DISJOINT and d_12 == INTERSECT and d_20 >= 0)
        return sumvol(1, 2, n);
    if (d_01 == INTERSECT and d_12 >= 0 and d_20 == DISJOINT)
        return sumvol(0, 1, n);
    
    if (d_01 >= 0 and d_12 == INTERSECT and d_20 == DISJOINT)
        return sumvol(1, 2, n);
    if (d_01 == INTERSECT and d_12 == DISJOINT and d_20 >= 0)
        return sumvol(0, 1, n);
    if (d_01 == DISJOINT and d_12 >= 0 and d_20 == INTERSECT)
        return sumvol(2, 0, n);

    return algo_brut(n);
}

int main()
{
    int n;
    LL res;
    scanf("%d", &n);
    FOR(i,0,3)
    {
        scanf("%d%s", &B[i].r, Aux);
        B[i].coords.read(Aux, n);
    }
    //DEBUG("%lld\n", algo_max_brut(n));
    //DEBUG("%lld\n", algo_brut(n));
    printf("%lld\n", algo(n));
    return 0;
}