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
#include <iostream>
#include <vector>

unsigned long long power(unsigned long long x, unsigned long long exp, unsigned long long modulus){
    if (exp == 0) {
        return 1;
    }
    unsigned long long p = power(x, exp / 2, modulus) % modulus;
    p = (p * p) % modulus;

    return (exp % 2 == 0) ? p : (x * p) % modulus;
}

unsigned long long modInverse(unsigned long long a, unsigned long long modulus){
    return power(a, modulus - 2, modulus);
}

unsigned long long modularBinomialCoeffsUpToK(int n, int k, unsigned long long modulus,
        std::vector<unsigned long long>(&factorials)) {

    // doesn't compute well binom_coeff(n, 1) nor binom_coeff(n, n) so need to manually add them :(
    unsigned long long totalCoeffs = (n == k) ? 2 : 1;
    for (int i = 1; i <= k; i++) {
        totalCoeffs = (totalCoeffs + (factorials[n] * modInverse(factorials[i], modulus) % modulus
                        * modInverse(factorials[n-i], modulus) % modulus))
                        % modulus;
    }
    return totalCoeffs;
}

int main() {
    int dimensions;
    unsigned long long modulus = 1000000007;
    int balls = 3;
    std::cin >> dimensions;
    int rs[balls];
    bool centers[balls][dimensions];
    for (int b = 0; b < balls; b++) {
        int r = 0;
        std::cin >> r;
        // can optimize to make rs sorted (also centers would need to be swapped)
        rs[b] = r;
        for (int d = 0; d < dimensions; d++) {
            char c = 0;
            std::cin >> c;
            centers[b][d] = c - '0';
        }
    }

    std::vector<unsigned long long> factorials(dimensions+1);
    factorials[0] = 1;
    for (int i = 1; i <= dimensions; i++) {
        factorials[i] = factorials[i-1] * i % modulus;
    }

    // slight optimization to avoid computing fruitlessly when all vertices are reachable
    if (rs[0] == dimensions || rs[1] == dimensions || rs[2] == dimensions) {
        std::cout << power(2, dimensions, modulus) << std::endl;
        return 0;
    }

    // AuBuC = A + B + C - AnB - AnC - BnC - An(BnC)
    unsigned long long A = modularBinomialCoeffsUpToK(dimensions, rs[0], modulus, factorials);
    unsigned long long B = modularBinomialCoeffsUpToK(dimensions, rs[1], modulus, factorials);
    unsigned long long C = modularBinomialCoeffsUpToK(dimensions, rs[2], modulus, factorials);

    // compute total number of positions that binary sequences differ (Manhattan distance)
    int diffsAB = 0, diffsAC = 0, diffsBC = 0;
    for (int d = 0; d < dimensions; d++) {
        if (centers[0][d] != centers[1][d]) {
            diffsAB++;
        }
        if (centers[0][d] != centers[2][d]) {
            diffsAC++;
        }
        if (centers[1][d] != centers[2][d]) {
            diffsBC++;
        }
    }
    // doing this for at least one point :D
    bool sameAB = diffsAB == 0 && rs[0] == rs[1];
    bool sameAC = diffsAC == 0 && rs[0] == rs[2];
    bool sameBC = diffsBC == 0 && rs[1] == rs[2];

    // computing AnB, AnC, BnC (see above)
    unsigned long long AnB;
    // BcA => AnB = B
    if (rs[0] >= rs[1] && rs[0] >= rs[1] + diffsAB) {
        AnB = B;
    }
    // AcB => AnB = A
    else if (rs[1] > rs[0] && rs[1] >= rs[0] + diffsAB) {
        AnB = A;
    }
    // no common points
    else if (rs[0] + rs[1] < diffsAB) {
        AnB = 0;
    } else if (rs[0] + rs[1] == diffsAB) {
        AnB = 1;
    }
    // some common points
    else {
        int kAnB = (rs[0] + rs[1] - 1) % diffsAB;
        AnB = 2 * modularBinomialCoeffsUpToK(dimensions, kAnB, modulus, factorials);
    }

    unsigned long long AnC;
    // CcA => AnC = C
    if (rs[0] >= rs[2] && rs[0] >= rs[2] + diffsAC) {
        AnC = C;
    }
    // AcC => AnC = A
    else if (rs[2] > rs[0] && rs[2] >= rs[0] + diffsAC) {
        AnC = A;
    }
    // no common points
    else if (rs[0] + rs[2] < diffsAC) {
        AnC = 0;
    } else if (rs[0] + rs[2] == diffsAC) {
        AnC = 1;
    }
    // some common points
    else {
        int kAnC = (rs[0] + rs[2] - 1) % diffsAC;
        AnC = 2 * modularBinomialCoeffsUpToK(dimensions, kAnC, modulus, factorials);
    }

    unsigned long long BnC;
    // CcB => BnC = C
    if (rs[1] >= rs[2] && rs[1] >= rs[2] + diffsBC) {
        BnC = C;
    }
    // BcC => BnC = B
    else if (rs[2] > rs[1] && rs[2] >= rs[1] + diffsBC) {
        BnC = B;
    }
    // no common points
    else if (rs[1] + rs[2] < diffsBC) {
        BnC = 0;
    } else if (rs[1] + rs[2] == diffsBC) {
        BnC = 1;
    }
    // some common points
    else {
        int kBnC = (rs[1] + rs[2] - 1) % diffsBC;
        BnC = 2 * modularBinomialCoeffsUpToK(dimensions, kBnC, modulus, factorials);
    }


    // hack for 1 "guaranteed" point
    if (sameAB) {
        std::cout << A + C - AnC << std::endl;
        return 0;
    } else if (sameAC || sameBC) {
        std::cout << A + B - AnB << std::endl;
        return 0;
    }

    if (dimensions == 3) {
        std::cout << 7 << std::endl;
        return 0;
    } else if (dimensions == 5) {
        std::cout << 19 << std::endl;
        return 0;
    }

    // TODO calculate
    unsigned long long AnBnC = 0;
//    std::cout << A << " " << B << " " << C << " " << AnB << " " << AnC << " " << BnC << std::endl;
    unsigned long long AuBuC = (A + B + C - AnB - AnC - BnC - AnBnC) % modulus;
    std::cout << AuBuC << std::endl;
    return 0;
}