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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
//#pragma GCC optimize("O3")
#include<bits/stdc++.h>

using namespace std;

#ifdef DEBUG

#include "lib/debug.h"

#else
#define debug(...) 228
#endif


typedef long long ll;
typedef long double ld;

#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)

vector<int> read_vec(int n) {
    vector<int> v(n);
    for (int &t: v) cin >> t;
    return v;
}

template<typename T>
inline void upd_max(T &a, T b) {
    if (a < b) {
        a = b;
    }
}

template<typename T>
inline void upd_min(T &a, T b) {
    if (a > b) {
        a = b;
    }
}

template<int mod>
struct MInt {
    int x;

    constexpr MInt() : x{} {}

    constexpr MInt(ll x) : x{norm(x % getMod())} {}

    static int Mod;

    constexpr static int getMod() {
        if (mod > 0) {
            return mod;
        } else {
            return Mod;
        }
    }

    constexpr static void setMod(int Mod_) {
        Mod = Mod_;
    }

    constexpr int norm(int x) const {
        if (x < 0) {
            x += getMod();
        }
        if (x >= getMod()) {
            x -= getMod();
        }
        return x;
    }

    constexpr int val() const {
        return x;
    }

    explicit constexpr operator int() const {
        return x;
    }

    constexpr MInt operator-() const {
        MInt res;
        res.x = norm(getMod() - x);
        return res;
    }

    constexpr MInt inv() const {
        assert(x != 0);
        return power(*this, getMod() - 2);
    }

    constexpr MInt &operator*=(MInt rhs) &{
        x = 1LL * x * rhs.x % getMod();
        return *this;
    }

    constexpr MInt &operator+=(MInt rhs) &{
        x = norm(x + rhs.x);
        return *this;
    }

    constexpr MInt &operator-=(MInt rhs) &{
        x = norm(x - rhs.x);
        return *this;
    }

    constexpr MInt &operator/=(MInt rhs) &{
        return *this *= rhs.inv();
    }

    friend constexpr MInt power(MInt a, ll b) {
        MInt res = 1;
        for (; b; b /= 2, a *= a) {
            if (b % 2) {
                res *= a;
            }
        }
        return res;
    }

    friend constexpr MInt operator*(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res *= rhs;
        return res;
    }

    friend constexpr MInt operator+(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res += rhs;
        return res;
    }

    friend constexpr MInt operator-(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res -= rhs;
        return res;
    }

    friend constexpr MInt operator/(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res /= rhs;
        return res;
    }

    friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
        ll v;
        is >> v;
        a = MInt(v);
        return is;
    }

    friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {
        return os << a.val();
    }

    friend constexpr bool operator==(MInt lhs, MInt rhs) {
        return lhs.val() == rhs.val();
    }

    friend constexpr bool operator!=(MInt lhs, MInt rhs) {
        return lhs.val() != rhs.val();
    }
};

template<int mod>
string to_string(const MInt<mod> &number) {
    return to_string(number.val());
}

template<int V, int mod>
constexpr MInt<mod> CInv = MInt<mod>(V).inv();

template<>
int MInt<0>::Mod = 0;

constexpr int mod = (int) 1e9 + 7;
using Mint = MInt<mod>;


struct Comb {
    int n;
    vector<Mint> _fac;
    vector<Mint> _invfac;
    vector<Mint> _inv;

    Comb() : n{0}, _fac{1}, _invfac{1}, _inv{0} {}

    Comb(int n) : Comb() {
        init(n);
    }

    void init(int m) {
        if (m <= n) return;
        _fac.resize(m + 1);
        _invfac.resize(m + 1);
        _inv.resize(m + 1);

        for (int i = n + 1; i <= m; i++) {
            _fac[i] = _fac[i - 1] * i;
        }
        _invfac[m] = _fac[m].inv();
        for (int i = m; i > n; i--) {
            _invfac[i - 1] = _invfac[i] * i;
            _inv[i] = _invfac[i] * _fac[i - 1];
        }
        n = m;
    }

    Mint fac(int m) {
        if (m > n) init(2 * m);
        return _fac[m];
    }

    Mint invfac(int m) {
        if (m > n) init(2 * m);
        return _invfac[m];
    }

    Mint inv(int m) {
        if (m > n) init(2 * m);
        return _inv[m];
    }

    Mint cnk(int n, int m) {
        if (n < m || m < 0) return 0;
        return fac(n) * invfac(m) * invfac(n - m);
    }
} comb;

const int maxN = 2e6 + 10;
Mint inv2[maxN];
void solve() {
    int n;
    cin >> n;
    vector<int> a(2 * n);
    FOR(i, 0, 2 * n) cin >> a[i];
    inv2[0] = 1;
    Mint val = (mod + 1) / 2;
    FOR(i, 1, 2 * n + 1) inv2[i] = val * inv2[i - 1];
    if (*min_element(a.begin(), a.end()) == 0) {
        for (auto &it: a) it = 2 - it;
        rotate(a.begin(), a.begin() + 1, a.end());
    }
    if (*min_element(a.begin(), a.end()) == 0) {
        cout << 0 << '\n';
        return;
    }
    Mint ans = 0;
    int cnt[2] = {0, 0};
    cnt[a[0] - 1]++;
    bool ch = false;
    FOR(i, 1, 2 * n) {
        if (a[i] != a[i - 1]) {
            cnt[a[i] - 1]++;
            ch = true;
        }
    }
    if (a[0] == a[2 * n - 1] && ch) {
        cnt[a[0] - 1]--;
    }
    if (cnt[0] && cnt[1]) assert(cnt[0] == cnt[1]);
    vector<int> lens[2];
    FOR(i, 0, 2 * n) {
        if (a[i] != a[(i + 1) % (2 * n)]) {
            int s = (i + 1) % (2 * n);
            for (int x = s;; x++) {
                if (a[x % (2 * n)] != a[s]) {
                    lens[a[s] - 1].emplace_back(x - s);
                    break;
                }
            }
        }
    }
    if (lens[0].empty()) {
        assert(lens[1].empty());
        lens[a[0] - 1] = {2 * n};
    }


    bool ok = true;
    FOR(i, 0, 2 * n) {
        if (a[i] != a[(i + 1) % (2 * n)]) {
            if ((a[i] == 1) != (i % 2 == 1)) {
                ok = false;
                break;
            }
        }
        //cand for 4n - k
    }
    if (!ok) {
        cout << 0 << '\n';
        return;
    }

    auto f = [&](int a, int b, int tot_x, int tot_y) {
        //a of first parity, b of second are occupied
        if (a > tot_x || b > tot_y || a < 0 || b < 0) return Mint(0);
        return comb.fac(2 * tot_x + 2 * tot_y - a - b) * inv2[tot_x - a + tot_y - b];
    };

    auto finish = [&](int a, int b) {
        //I have a cards of even, b cards of odd (on distinct) poses
        //I want to put one more guy on even ones
        return a * f(a - 1, b, n - 1, n) + (n - a) * f(a + 1, b, n, n);
    };
    FOR(k, 1, 2 * n + 1) {
        int cnt1 = 0;
        int cnt2 = 0;
        if (k % 2 == 1) {
            if (k == 1) {
                cnt1 = 0;
                cnt2 = 1;
            } else {
                cnt1 = k / 2;
                cnt2 = k / 2;
            }
        } else {
            if (k == 2) {
                cnt1 = 1;
                cnt2 = 0;
            } else {
                cnt1 = k / 2 - 1;
                cnt2 = k / 2 - 1;
            }
        }

        if (cnt1 == cnt[0] && cnt2 == cnt[1]) {
            //k >= 3
            if (k >= 3) {
                if (k % 2 == 1) {
                    assert(cnt1 >= 1 && cnt2 >= 1);
                    int S = 0;
                    FOR(i, 0, 2 * n) {
                        if (a[i] == 2 && i % 2 == 1) S++;
                    }
                    ans += finish(cnt1 + 1, cnt1) * S;
                } else {
                    assert(cnt1 >= 1 && cnt2 >= 1);
                    for (int z: lens[0]) {
                        assert(z % 2 == 1);
                        Mint ways = 0;
                        FOR(x, 0, z) {
                            if (x % 2 == 1) {
                                ways += x / 2 + 1;
                            }
                        }
                        ans += ways * finish(cnt1 + 1, cnt1 + 1);
                    }
                }
            } else {
                if (k == 1) {
                    ans += n * finish(1, 0);
                } else {
                    assert(k == 2);
                    ans += n * Mint(n) * finish(1, 1);
                }
            }
        }


        //color[4n - k + 1] != color[4n - k] and fits
        if (k % 2 == 1) {
            if (k == 1) {
                //ne bywaet
                cnt1 = -1;
                cnt2 = -1;
            } else {
                cnt1 = k / 2;
                cnt2 = k / 2;
            }
        } else {
            cnt1 = k / 2;
            cnt2 = k / 2;
        }

        if (cnt1 == cnt[0] && cnt2 == cnt[1]) {
            assert(k > 1);
            if (k % 2 == 1) {
                //put max
                for (int z: lens[0]) {
                    assert(z % 2 == 1);
                    Mint ways = (z - 1) / 2;
                    int lft = (2 * n - z);
                    int P = (lft + 1) / 2;
                    assert(P >= cnt1);
                    ans += ways * (P - cnt1) * f(cnt1 + 1, cnt1 + 1, n, n);
                    ans += ways * cnt1 * f(cnt1 - 1, cnt1 + 1, n - 1, n);

                    Mint ways2 = 0;
                    FOR(i, 0, z - 1) {
                        if (i % 2 == 0) {
                            ways2 += i / 2;
                        }
                    }
                    ans += ways2 * f(cnt1 + 1, cnt1 + 1, n, n);
                }
            } else {
                for (int z: lens[1]) {
                    assert(z % 2 == 1);
                    int lft = (2 * n - z + 1);
                    int P = (lft) / 2;
                    assert(P >= cnt1);
                    ans += (P - cnt1) * f(cnt1 + 1, cnt1, n, n);
                    ans += cnt1 * f(cnt1 - 1, cnt1, n - 1, n);
                }
            }
        }
    }
    if (*max_element(a.begin(), a.end()) == 1) {
        ans *= 2;
    }
    cout << ans << '\n';

}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#ifdef DEBUG
    freopen("input.txt", "r", stdin);
#endif
    int tst;
    cin >> tst;
    while (tst--) solve();
    return 0;
}