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
#include <bits/stdc++.h>

using namespace std;

const long long mod = 1000000007;
const long long maxFact = 10010;
long long factorials[maxFact+1];
long long invFactorials[maxFact+1];

long long moduloExp(long long base, long long exp) {
    if(exp == 0) return 1;
    long long pow = moduloExp(base*base % mod, exp>>1);
    if(exp & 1) pow = pow*base % mod;
    return pow;
}

long long moduloInverse(long long x) {
    return moduloExp(x, mod-2);
}

long long choose(int objects, int amount) {
    if(amount > objects || objects < 0 || amount < 0) return 0;
    return factorials[objects] * invFactorials[amount] % mod * invFactorials[objects-amount] % mod;
}

struct hmsphere {
    vector<bool> p;
    int r;

    hmsphere(vector<bool> pos, int radius) {
        p = pos;
        r = radius;
    }

    hmsphere(string pos, int radius) {
        for(int i = 0; i < pos.size(); i++) {
            p.push_back(pos[i] != '0');
        }
        r = radius;
    }

    hmsphere invert() {
        vector<bool> inverted;
        for(int i = 0; i < p.size(); i++) {
            inverted.push_back(!p[i]);
        }
        return hmsphere(inverted, p.size()-r-1);
    }

    bool operator > (hmsphere& o) {
        return r > o.r;
    }

    bool operator < (hmsphere& o) {
        return r < o.r;
    }
};

long long i1(int d, hmsphere& s1) {
    long long w = 0;
    for(int i = 0; i <= s1.r; i++) {
        w = (w+choose(d, i)) % mod;
    }
    return w;
}

long long i2(int d, hmsphere& s1, hmsphere& s2) {
    int w = 0;
    for(int i = 0; i < s1.p.size(); i++) {
        if(s1.p[i] == s2.p[i]) w++;
    }
    int a = d-w;
    long long pref[a+2];
    pref[0] = 0;
    for(int i = 0; i <= a; i++) {
        pref[i+1] = (pref[i] + choose(a, i)) % mod;
    }
    long long ret = 0;
    int maxi = min(s1.r, s2.r), maxx, minx;
    for(int i = 0; i <= maxi; i++) {
        maxx = min(s2.r-i, a);
        minx = max(0, a+i-s1.r);
        if(maxx >= minx) {
            ret = (ret + choose(w, i) * ((pref[maxx+1] - pref[minx] + mod) % mod) % mod) % mod;
        }
    }
    return ret;
}

long long i3(int d, hmsphere& s1, hmsphere& s2, hmsphere& s3) {
    int w = 0, a = 0, b = 0, c = 0;
    for(int i = 0; i < s1.p.size(); i++) {
        if(s1.p[i] == s2.p[i] && s1.p[i] == s3.p[i]) w++;
        if(!s1.p[i] == s2.p[i] && !s1.p[i] == s3.p[i]) a++;
        if(!s2.p[i] == s1.p[i] && !s2.p[i] == s3.p[i]) b++;
        if(!s3.p[i] == s1.p[i] && !s3.p[i] == s2.p[i]) c++;
    }
    long long ret = 0, sumx, sumy;
    int maxi = min(min(s1.r, min(s2.r, s3.r)), w), maxx, maxy, maxz, minz;
    long long pref[c+2];
    pref[0] = 0;
    for(int i = 0; i <= c; i++) {
        pref[i+1] = (pref[i] + choose(c, i)) % mod;
    }
    for(int i = 0; i <= maxi; i++) {
        maxx = min(min(s3.r-i, s2.r-i), a);
        sumx = 0;
        for(int x = max(0, a+i-s1.r); x <= maxx; x++) {
            maxy = min(min(s3.r-i-x, s1.r-i-a+x), b);
            sumy = 0;
            for(int y = max(0, x-s2.r+i+b); y <= maxy; y++) {
                maxz = min(min(s1.r-i-a-y+x, s2.r-i-b-x+y), c);
                minz = max(0, x+y-s3.r+i+c);
                if(maxz >= minz) {
                    sumy = (sumy + (pref[maxz+1] - pref[minz] + mod) % mod * choose(b, y) % mod) % mod;
                }
            }
            sumx = (sumx + sumy * choose(a, x) % mod) % mod;
        }
        ret = (ret + sumx * choose(w, i) % mod) % mod;
    }
    return ret;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    factorials[0] = 1;
    for(int i = 1; i <= maxFact; i++) factorials[i] = factorials[i-1]*i % mod;
    invFactorials[maxFact] = moduloInverse(factorials[maxFact]);
    for(int i = maxFact-1; i >= 0; i--) invFactorials[i] = invFactorials[i+1]*(i+1) % mod;
    int n, r1, r2, r3;
    cin >> n;
    string s1, s2, s3;
    cin >> r1 >> s1 >> r2 >> s2 >> r3 >> s3;
    hmsphere s[3] = {hmsphere(s1, r1), hmsphere(s2, r2), hmsphere(s3, r3)};
    sort(s, s+3);
    long long res = 0;
    if(s[2].r*2 >= n) {
        s[2] = s[2].invert();
        if(s[1].r*2 >= n) {
            s[1] = s[1].invert();
            if(s[0].r*2 >= n) {
                s[0] = s[0].invert();
                res = (res + moduloExp(2, n)) % mod;
                res = (res - i3(n, s[0], s[1], s[2]) + mod) % mod;
            }
            else {
                res = (res + moduloExp(2, n)) % mod;
                res = (res - i2(n, s[1], s[2]) + mod) % mod;
                res = (res + i3(n, s[0], s[1], s[2])) % mod;
            }
        }
        else {
            res = (res + moduloExp(2, n)) % mod;
            res = (res - i1(n, s[2]) + mod) % mod;
            res = (res + i2(n, s[0], s[2])) % mod;
            res = (res + i2(n, s[1], s[2])) % mod;
            res = (res - i3(n, s[0], s[1], s[2]) + mod) % mod;
        }
    }
    else {
        res = (res + i1(n, s[0])) % mod;
        res = (res + i1(n, s[1])) % mod;
        res = (res + i1(n, s[2])) % mod;
        res = (res - i2(n, s[0], s[1]) + mod) % mod;
        res = (res - i2(n, s[0], s[2]) + mod) % mod;
        res = (res - i2(n, s[1], s[2]) + mod) % mod;
        res = (res + i3(n, s[0], s[1], s[2])) % mod;
    }
    cout << res;
    return 0;
}