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
#include <bits/stdc++.h>
// #define MULTIPLE_TESTS
// #define ENDLESS_TESTS
#define MEMORY_LIMIT 312
#define TIME_LIMIT 2.0
// #define ADDITIONAL_LOGS // enable blocks used only for cerr

using namespace std;

constexpr int MOD = 1'000'000'000+7;

inline int fast_pow(int a, int power)
{
    int result = 1;
    while (power > 0)
    {
        if (power%2 == 1)
            result = 1LL * result * a % MOD;

        power /= 2;
        a = 1LL * a * a % MOD;
    }

    return result;
}

// Małe Twierdzenie Fermata
inline int mod_inverse(int a)
{
    return fast_pow(a, MOD-2);
}

vector<int> inverses(int n)
{
    vector<int> ret(n+1);

    for (int i = 0; i <= n; ++i)
        ret[i] = mod_inverse(i);

    return ret;
}

vector<int> pascal_row(int n, const vector<int> &inverses)
{
    vector<int> row(n+1);
    row[0] = 1;

    for (int k = 0; k < n; ++k)
    {
        row[k+1] = 1LL * row[k] * (n-k) % MOD * inverses[k+1] % MOD;
    }

    return row;
}

vector<int> sufix_sums(const vector<int> &row)
{
    vector<int> sums(row.size());

    int current = 0;

    for (int i = row.size()-1; i >= 0; --i)
    {
        current = (current + row[i]) % MOD;
        sums[i] = current;
    }

    return sums;
}

struct ComputationRequest 
{

};

void solve_optimize_123(int r1, int r2, int r3, int c12, int c13, int c23, int c123)
{
    const int n = c12+c13+c23+c123;
    const int max_n = max({c23, c13, c12, c123});

    const vector<int> inverses = ::inverses(max_n);

    const auto syms23 = pascal_row(c23, inverses);
    const auto syms13 = pascal_row(c13, inverses);
    const auto syms12 = pascal_row(c12, inverses);
    const auto syms123 = pascal_row(c123, inverses);
    const auto sufixs123 = sufix_sums(syms123);

    int answer = fast_pow(2, n);

    const int base1 = r1 + 1;
    const int base2 = r2 - c23 - c13 + 1;
    const int base3 = r3 - c23 - c12 + 1;

    unordered_map<int, vector<long long>> req_method1;
    unordered_map<int, vector<long long>> req_method2;

    for (int i23 = 0; i23 <= c23; ++i23)
    {
        const int sym23 = syms23[i23];

        for (int i13 = 0; i13 <= c13; ++i13)
        {
            const int prefix = 1LL * sym23 * syms13[i13] % MOD;
            const int req1 = max({base1-i23-i13, base2+i23+i13});
            const int req2 = base3+i23-i13;

            const int min12 = max(0, req1-c123);
            const int max12 = min(c12, c123-req2);
            const int swap = (req1-req2+1)/2;

            const int range_end1 = min(max12, swap-1);
            const int range_begin2 = max(min12, swap);

            if (min12 <= range_end1)
            {
                auto &vec = req_method1[req1];
                vec.resize(c12+2);
                vec[min12] += prefix;
                vec[range_end1+1] -= prefix;
            }

            if (range_begin2 <= max12)
            {
                auto &vec = req_method2[req2];
                vec.resize(c12+2);
                vec[range_begin2] += prefix;
                vec[max12+1] -= prefix;
            }
#if 0
            long long sufix_sum = 0;

            for (int i12 = min12; i12 <= range_end1; ++i12)
            {
                assert(req1-i12 >= req2+i12);
                const int req = max(0, req1-i12);

                assert(req <= c123);

                const int sufix = 1LL * syms12[i12] * sufixs123[req] % MOD;
                sufix_sum += sufix;
            }

            for (int i12 = range_begin2; i12 <= max12; ++i12)
            {
                assert(req1-i12 <= req2+i12);

                const int req = max(0, req2+i12);

                assert(req <= c123);

                const int sufix = 1LL * syms12[i12] * sufixs123[req] % MOD;
                sufix_sum += sufix;
            }

            const int diff = sufix_sum % MOD * prefix % MOD;
            answer = (answer - diff + MOD) % MOD;
#endif
        }
    }
#if 1
    for (const auto &elem : req_method1)
    {
        const int req1 = elem.first;
        const auto &vec = elem.second;

        long long prefix = 0;

        for (int i12 = 0; i12 <= c12; ++i12)
        {
            prefix += vec[i12];
            const int req = max(0, req1-i12);

            if(req > c123)
                continue;

            const int sufix = 1LL * syms12[i12] * sufixs123[req] % MOD;
            const int diff = prefix % MOD * sufix % MOD;
            answer = (answer - diff + MOD) % MOD;
        }
    }

    for (const auto &elem : req_method2)
    {
        const int req2 = elem.first;
        const auto &vec = elem.second;

        long long prefix = 0;

        for (int i12 = 0; i12 <= c12; ++i12)
        {
            prefix += vec[i12];
            const int req = max(0, req2+i12);

            if(req > c123)
                continue;

            const int sufix = 1LL * syms12[i12] * sufixs123[req] % MOD;
            const int diff = prefix % MOD * sufix % MOD;
            answer = (answer - diff + MOD) % MOD;
        }
    }
#endif
    cout << answer << '\n';
}

void test()
{
    int n;
    cin >> n;

    int r1, r2, r3;
    string bits1, bits2, bits3;

    cin >> r1 >> bits1;
    cin >> r2 >> bits2;
    cin >> r3 >> bits3;

    int c12=0, c13=0, c23=0, c123=0;

    for (int i = 0; i < n; ++i)
    {
        const bool b12 = (bits1[i] == bits2[i]);
        const bool b13 = (bits1[i] == bits3[i]);

        if (b12 && b13) ++c123;
        else if (b12) ++c12;
        else if (b13) ++c13;
        else ++c23;
    }

    // cerr << c23 << ' ' << c13 << ' ' << c12 << ' ' << c123 << endl;

    if (c13 > c12)
    {
        swap(c13, c12);
        swap(r3, r2);
    }

    if (c23 > c12)
    {
        swap(c23, c12);
        swap(r3, r1);
    }

    solve_optimize_123(r1, r2, r3, c12, c13, c23, c123);

    // const int max_n = max({c23, c13, c12, c123});

    // if (max2_n == c23)
    //            
}

/******************************************************************************/

int main()
{
#ifdef CONTEST_WORKSPACE
    #ifdef MEMORY_LIMIT
        void limit_ram(float);
        limit_ram(MEMORY_LIMIT);
    #endif
    #ifdef TIME_LIMIT
        void limit_cpu(float);
        limit_cpu(TIME_LIMIT);
    #endif
#endif

#if !defined(CONTEST_WORKSPACE)
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
#endif

#if defined(MULTIPLE_TESTS)
    int T = 0;
    cin >> T;

    while (T --> 0)
        test();

#elif defined(ENDLESS_TESTS)
    while(!(cin >> std::ws).eof())
        test();

#else
    test();
#endif

    return EXIT_SUCCESS;
}