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
// Template generated by Clank
#include<bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define all(x) x.begin(), x.end()
#define BOOST cin.tie(NULL); ios_base::sync_with_stdio(false);
// #define int ll
typedef long long ll;

int lim = 1e6 * 5;
ll mod = 1e9 + 7;
ll rev2;
vector<ll> fact;
vector<ll> r2k;
vector<int> b[2];

ll fastpow(ll a, ll k){
    ll res = 1;
    while(k > 0){
        if(k & 1) res = (res * a) % mod;
        a = (a * a) % mod;
        k >>= 1;
    }
    return res;
}
ll rev(ll x){
    return fastpow(x, mod - 2);
}

ll po2(ll x){
    return (((x * (x - 1)) % mod) * rev2) % mod;
}

ll poss(ll space, ll dbl){
    if(space < 0 || dbl < 0) return 0;
    return (fact[space] * r2k[dbl]) % mod;
}

ll all_zero(ll n){
    // ll ans = 0, tans;
    // tans = n/2;
    // for(int i = 0; i < n - 1; i++){
    //     tans = (tans * po2(2 * (n - 1 - i))) % mod;
    // }
    // ans = tans;
    // tans = (4 * po2(n/2)) % mod;
    // for(int i = 0; i < n - 2; i++){
    //     tans = (tans * po2(2 * (n - 1 - i))) % mod;
    // }
    // ans = (ans + tans) % mod;
    // return ans;

    ll ans1 = ((n/2) * poss(n * 2 - 2, n - 1)) % mod;
    ll ans2 = (((2 * po2(n/2)) % mod) * poss(n * 2 - 2, n - 2)) % mod;

    return (ans1 + ans2) % mod;
}

ll all_one(ll n){
    // ll ans1 = ((n/2) * poss(n * 2 - 2, n - 1)) % mod;
    ll ans1 = ((n/2) * (n/2)) % mod;
    ans1 = (ans1 * poss(n * 2 - 3, n - 2)) % mod;
    ll ans2 = (((2 * po2(n/2)) % mod) * (n/2)) % mod;
    ans2 = (ans2 * poss(n * 2 - 3, n - 3)) % mod;
    // ll ans2 = (((2 * po2(n/2)) % mod) * poss(n * 2 - 2, n - 2)) % mod;
    // return 1LL;
    return ((ans1 + ans2) * 2) % mod;
}

ll compute(ll d1, ll d0, ll bn, ll n){
    // cout << "Compute " << d1 << " " << d0 << " " << bn << " " << n << "\n";
    ll ans = 0;
    ll ans1 = 0;
    ll ans2 = 0;
    ll ans3 = 0;
    ll ans4 = 0;
    ll options = d0 / 2;
    
    // 10 nie jest pomiedzy 12 a 11
    // 10 tworzy z kims pare
    ans1 = ((bn/2) * poss(2 * n - (bn + 1), n - bn)) % mod;
    // 10 stoi sama
    ans2 = (((n/2) - (bn/2) - options) * poss(2 * n - (bn + 1), n - bn - 1)) % mod; 
    // 10 jest pomiedzy 12 a 11
    // 9 jest sama na polu
    ans3 = (((n/2) - (bn/2 + 1)) * poss(2 * n - (bn + 1) - 1, n - (bn + 1) - 1)) % mod;
    // 9 tworzy z kims pare
    ans4 = ((bn/2 + 1) * poss(2 * n - (bn + 1) - 1, n - (bn + 1))) % mod;
    
    ans = (ans + ans1 + ans2 + (((ans3 + ans4) * options) % mod)) % mod;


    // ll ans = ((bn/2) * poss(2 * n - (bn + 1), n - bn)) % mod;
    // ans = (ans + ((n - bn)/2) * poss(2 * n - (bn + 1), n - bn - 1)) % mod;
    // cout << ans << "\n";

    options = d1/2;
    for(int i = 1; i <= d1 - 2; i += 2){
        //7 nie jest pomiedzy 12 a 8
        // 7 tworzy z kims pare
        ans1 = ((bn/2) * poss(2 * n - (bn + 2), n - bn - 1)) % mod;
        // 7 stoi sama
        ans2 = (((n/2) - (bn/2) - options) * poss(2 * n - (bn + 2), n - bn - 2)) % mod;

        //7 jest pomiedzy 12 a 8
        // 6 jest sama na polu
        ans3 = (((n/2) - (bn/2 + 1)) * poss(2 * n - (bn + 2) - 1, n - (bn + 2) - 1)) % mod;
        // 6 tworzy z kims pare
        ans4 = ((bn/2 + 1) * poss(2 * n - (bn + 2) - 1, n - (bn + 2))) % mod;
        ans = (ans + ans1 + ans2 + (((ans3 + ans4) * options) % mod)) % mod;
        // cout << "o: " << options << " " << ans << " " << ans1 << " " << ans2 << " " << ans3 << " " << ans4 << "\n";
        options--;
    }
    return ans;
}

void solve(){
    int n; cin >> n;
    n *= 2;
    vector<int> s(n);
    int flags[3]={0,0,0};
    for(int i = 0; i < n; i++){
        cin >> s[i];
        flags[s[i]] = 1;    
    }
    if(flags[0] && flags[2]){
        cout << "0\n";
        return;
    }
    if(!flags[1]){
        cout << all_zero(n) << "\n";
        return;
    }
    if(!flags[0] && !flags[2]){
        cout << all_one(n) << "\n";
        return;
    }
    if(flags[2]){
        int tmp = s[0];
        if(tmp == 2) tmp = 0;
        for(int i = 1; i < n; i++){
            if(s[i] == 2) s[i] = 0;
            s[i - 1] = s[i];
        }
        s[n - 1] = tmp;
    }
    int sidx = 0;
    while(s[sidx] == s[0]) sidx++;
    int first_sym = s[sidx];
    int d = 1;
    int last = s[sidx];
    if((s[sidx] ^ (sidx&1)) == 0){
        cout << "0\n";
        return;
    }
    // int tmp = (sidx - 1 + n) % n;
    // if()
    b[0].clear();
    b[1].clear();
    for(int i = (sidx + 1) % n; i != sidx; i = (i + 1) % n){
        if(s[i] != last){
            if(d % 2 == 0 || ((s[i] ^ (i&1)) == 0)){
                cout << "0\n";
                return;
            }
            b[last].push_back(d);
            last = s[i];
            d = 0;
        }
        d++;
    }
    if(d % 2 == 0){
        cout << "0\n";
        return;
    }
    b[last].push_back(d);

    // cout << "    0: ";
    // for(int i : b[0]){
    //     cout << i << " ";
    // }
    // cout << "\n";
    // cout << "    1: ";
    // for(int i : b[1]){
    //     cout << i << " ";
    // }
    // cout << "\n";
    ll ans = 0;
    // for(int i : b[1]){
    for(int i = 0; i < (int)b[1].size(); i++){
        // cout << "    " << ans << "\n";
        ans = (ans + compute(b[1][i], b[0][(i + (first_sym ^ 1)) % b[0].size()], b[0].size() + b[1].size(), n)) % mod;
        
    }
    cout << ans << "\n";
}
 
int32_t main(){
    BOOST;
    rev2 = rev(2);
    fact.resize(lim);
    r2k.resize(lim);
    fact[0] = 1;
    r2k[0] = 1;
    for(ll i = 1; i < lim; i++){
        fact[i] = (fact[i - 1] * i) % mod;
        r2k[i] = (r2k[i - 1] * rev2) % mod;
    }
    int t; cin >> t;
    while(t--){
        solve();
    }
    return 0;
}