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

using namespace std;

long long P = 1e9 + 7;

long long modPow(long long a, long long b) {
    long long res = 1;
    while (b) {
        if (b & 1) {
            res = res * a % P;
        }
        a = a * a % P;
        b >>= 1;
    }
    return res;
}

long long fact[4'000'001];
long long half = (P + 1) / 2;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    fact[0] = 1;
    for (int i = 1; i <= 4'000'000; ++i) {
        fact[i] = (fact[i - 1] * i) % P;
    }
    while (t--) {
        int n;
        cin >> n;
        bool has[3]{};
        vector<int> a(2 * n);
        for (int i = 0; i < 2 * n; ++i) {
            cin >> a[i];
            has[a[i]] = true;
        }
        if (has[0] && has[2]) {
            cout << 0 << "\n";
            continue;
        }
        if ((has[0] && !has[1] && !has[2]) || (!has[0] && !has[1] && has[2])) {
            long long tmp = modPow(half, 2 * n - 1);
            long long res = fact[4 * n - 2] * n % P;
            res = res * tmp % P;
            res = res * ((n - 1) * 2 + 1) % P;
            cout << res << "\n";
            continue;
        }
        if (has[1] && !has[0] && !has[2]) {
            long long tmp = modPow(half, 2 * n - 2);
            long long res = fact[4 * n - 3] * n % P;
            res = res * 2 % P;
            res = res * n % P;
            res = res * tmp % P;
            res = res * ((n - 1) * 2 + 1) % P;
            cout << res << "\n";
            continue;
        }
        // oddness
        int length = 0;
        bool valid = true;
        int changesBase = 0;
        int firstLength{}, lastLength{};
        for (int i = 2 * n - 1; i > 0; --i) {
            ++length;
            if (a[i] != a[i - 1]) {
                firstLength = length;
                if (length % 2 == 0) {
                    if (changesBase != 0) {
                        valid = false;
                        break;
                    }
                }
                length = 0;
                ++changesBase;
            }
        }
        lastLength = length + 1;
        if (a[0] != a[2 * n - 1]) {
            if (firstLength % 2 == 0 || lastLength % 2 == 0) {
                valid = false;
            }
        } else {
            if (firstLength + lastLength % 2 == 0) {
                valid = false;
            }
        }
        if (!valid) {
            cout << 0 << "\n";
            continue;
        }
        if (a[0] == a[2 * n - 1]) {
            --changesBase;
        }
        int acePos = -2 + !has[0];
        int jackPos = 0;
        bool jackWrapped = false;
        long long res{};
        bool zero = false;
        while (true) {
            do {
                acePos += 2;
            } while (acePos < 2 * n && a[acePos] != 1);
            if (a[(acePos - 1 + 2 * n) % (2 * n)] == 1 && a[(acePos - 2 + 2 * n) % (2 * n)] != 1) {
                zero = true;
                break;
            }

            if (acePos >= 2 * n || jackPos >= 2 * n) {
                break;
            }
            int changesFix = 0;
            if (a[acePos] == a[(acePos + 1) % (2 * n)]) {
                changesFix = 1;
            }
            int changes = changesBase + changesFix;
            if (jackPos <= acePos && !jackWrapped) {
                jackPos = acePos + 1;
                if (jackPos == 2 * n) {
                    jackPos = 0;
                    jackWrapped = true;
                }
            }
            int nextPos = jackPos == 2 * n - 1 ? 0 : jackPos + 1;
            while (a[jackPos] == a[nextPos]) {
                jackPos = nextPos;
                nextPos = jackPos == 2 * n - 1 ? 0 : jackPos + 1;
                if (jackPos == 0) {
                    jackWrapped = true;
                }
            };
            long long ace2jack = (jackPos - acePos + 2 * n - 1) % (2 * n); // jack excluded
            long long jack2ace = 2 * n - 1 - ace2jack; // ace included
            long long oppositeCardsJack2Ace = (changes + 1) / 2;
            long long oppositePosJack2Ace = (jack2ace + 1) / 2;
            long long oppositePosAce2Jack = (ace2jack + 1) / 2;
            long long fct = fact[4 * n - (changes + 3)];
            long long power = modPow(half, 2 * n - (changes + 1));
            long long tmpRes = fct * power % P;
            long long paren = oppositePosAce2Jack * (1 + oppositeCardsJack2Ace) * 2 + oppositePosAce2Jack * (n - 1 - oppositeCardsJack2Ace) * 4;
            paren += (2 * oppositePosJack2Ace - oppositeCardsJack2Ace) * (4 * n - (changes + 2));
            paren %= P;

            tmpRes = tmpRes * paren % P;
            res = (res + tmpRes) % P;
            if (ace2jack < 0 || jack2ace < 0 || oppositeCardsJack2Ace < 0 || oppositePosJack2Ace < 0 || oppositePosAce2Jack < 0 || fct < 0 || power < 0 || paren < 0 || fct < 0 || tmpRes < 0 || res < 0) {
                cout << "bug\n";
            }
        }

        cout << (zero ? 0 : res) << "\n";
    }
}