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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef double db;
typedef pair<int, int> pii;
#define all(x) (x).begin(), (x).end()
#define sz(x) (x).size()
#define rep(i, l, r) for(int i=l; i<(r); i++)

ll nxt(){
    ll x;
    cin >> x;
    return x;
}

const ll mod = 1e9 + 7;
const int debug = 0;
const int N = 1e6;

ll facts[4*N+1];

ll plu(ll a, ll b) {
    ll ans = a+b;
    if(ans >= mod) ans-=mod;
    return ans;
}

void dodaj(ll &a, ll b) {
    a += b;
    if(a >= mod) a-=mod;
}

ll minu(ll a, ll b) {
    ll ans = a-b;
    if(ans < 0) ans+= mod;
    return ans;
}

void odejmij(ll &a, ll b) {
    a-= b;
    if(a < 0) a+=mod;
}

ll ilo(ll a, ll b) {
    return (a*b)%mod;
}

void mnoz(ll &a, ll b) {
    a = (a*b)%mod;
}

ll fastpo(ll a, ll b) {
    ll ans = 1;
    while(b) {
        if(b&1) mnoz(ans, a);
        mnoz(a, a);
        b/=2;
    }
    return ans;
}

ll inv(ll a) {
    return fastpo(a, mod-2);
}

void prepare_facts() {
    facts[0] = 1;
    for(int i=1; i<=4*N; i++) {
        facts[i] = ilo(facts[i-1], i);
    }
}

void solve(){
    int n;
    cin >> n;
    int n2 = 2*n, n4 = 4*n;
    int inv2 = inv(2);

    vector<int> a(n2);
    int wyst[3];
    rep(i, 0, 3) wyst[i] = 0;
    for(auto &e : a) {
        cin >> e;
        wyst[e]++;
    } 
    if(wyst[0] > 0 && wyst[2] > 0) {
        cout << "0\n";
        return;
    }
    if(wyst[0] == n2 || wyst[2] == n2) {
        ll ans = ilo(n2, n2-1);
        mnoz(ans, facts[n4-2]);
        mnoz(ans, fastpo(inv2, n2));
        cout << ans << "\n";
        return;
    }
    if(wyst[1] == n2) {
        ll ans = n4; // gdzie dac najwiekszy
        mnoz(ans, ilo(n2, n2-1)); // dwa kolejne muszą isc do drugiej druzyny
        mnoz(ans, facts[n4-3]);
        mnoz(ans, fastpo(inv2, n2));
        cout << ans << "\n";
        return;
    }
    int ile_zmian = 0;
    for(int i=0; i<2*n-1; i++) {
        if(a[i]!=a[i+1]) ile_zmian++;
    }
    if(a[0] != a[2*n-1]) ile_zmian++;
    int gdzie_maksik = -1;
    if(wyst[0] > 0) gdzie_maksik = 0;
    if(wyst[2] > 0) gdzie_maksik = 1;
    assert(gdzie_maksik >= 0);
    int it1 = 0;
    while(a[it1] == a[(it1+n2-1)%n2]) it1++;
    int ile_teraz = 1;
    int co_ostatnie = a[it1];
    vector<int> ktory_jestem(n2, -1);
    vector<int> wielkosc_grupy(n2, -1);
    for(int i=(it1+1)%n2; ; i=(i+1)%n2) {
        if(a[i] == co_ostatnie) ile_teraz++;
        else {
            if(ile_teraz%2 == 0) {
                cout << "0\n";
                return;
            }
            ile_teraz = 1;
            co_ostatnie = a[i];
        }
        ktory_jestem[i] = ile_teraz;
        if(i == it1) break;
    }
    for(int i = (it1+n2-1)%n2; ; i = (i+n2-1)%n2) {
        if(a[i]==a[(i+1)%n2]) wielkosc_grupy[i] = wielkosc_grupy[(i+1)%n2];
        else wielkosc_grupy[i] = ktory_jestem[i];
        if(i == it1) break;
    }
    for(int i=0; i<n2; i++) {
        assert(ktory_jestem[i]!=-1);
        assert(wielkosc_grupy[i]!=-1);
    }

    ll ans = 0;
    for(int pos = gdzie_maksik; pos < n2; pos+=2) {
        if((a[pos] == 1) && (ktory_jestem[pos]%2 == 1)) {
            ll ile_ustalonych = ile_zmian;
            ll ust_a = (ile_ustalonych+1)/2;
            ll ust_b = ile_ustalonych - ust_a;
            int pocz = wielkosc_grupy[(pos+1)%n2];
            if(a[pos] == a[(pos+1)%n2]){
                ile_ustalonych++;
                pocz = wielkosc_grupy[pos] - ktory_jestem[pos];
            }
            // przypadek że dalej dwie karty są u tej samej druzyny
            ll do_dodania1 = 2*(pocz/2); // wyzsza z nich w dowolnym miejscu przed ostatnią z ciagu
            mnoz(do_dodania1, minu(n2, ust_b+1)); // nizsza w dowolnym miejscu u tej druzyny ktore nie jest jeszcze zajete
            mnoz(do_dodania1, facts[n4-ile_ustalonych-2]);
            mnoz(do_dodania1, fastpo(inv2, n2-ile_ustalonych));

            // przypadek że dalej jest karta która jest później niż ostatnia z ciągu
            ll do_dodania2 = n2 - ust_b - 2*(pocz/2);
            mnoz(do_dodania2, facts[n4-ile_ustalonych-1]);
            mnoz(do_dodania2, fastpo(inv2, n2-ile_ustalonych));
            dodaj(ans, do_dodania1);
            dodaj(ans, do_dodania2);
        }
    }
    cout << ans << "\n";
}

int main(){
    prepare_facts();
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    cin >> t;
    while(t--){
        solve();
    }
}