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
/*
Runda 4 - 
*/

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cmath>
#include<queue>
#include<queue>
#include<stack>
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include <chrono>

using namespace std;
using namespace __gnu_pbds;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<LL> VLL;
typedef vector<LD> VLD;
typedef vector<VLL > VVLL;
typedef vector<VLD > VVLD;
typedef vector<PII > VPII;
typedef vector<PLL > VPLL;
template<class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;

#define FOR(x, b, e) for(int x=b; x<=(e); ++x)
#define FORD(x, b, e) for(int x=b; x>=(e); --x)
#define REP(x, n) for(int x=0; x<(n); ++x)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int)(x).size())
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define PB push_back
#define ST first
#define ND second
#define THIS (*this)
#define LSB(x) (x & -x)
#define SQR(x) ((x)*(x))

LL MOD = 1e9 + 7;

LL gcdw(LL a, LL b, LL &l, LL &k) {
    if(!a) {
        l = 0;
        k = 1;
        return b;
    }
    LL d = gcdw(b % a, a, k, l);
    l -= (b / a) * k;
    return d;
}

LL mod(LL a, LL m) {
    return (a % m + m) % m;
}

LL modInv(LL a, LL m) {
    LL x, y;
    if(gcdw(a, m, x, y) != 1) return -1;
    return (x % m + m) % m;
}

LL powMod(LL a, LL p, LL m) {
    LL res = 1; a %= m;
    while (p > 0) {
        if (p & 1) res = (res * a) % m;
        p >>= 1;
        a = (a * a) % m;
    }
    return res;
}

int main() {
	ios_base::sync_with_stdio(0);
    cin.tie(NULL); cout.tie(NULL);
    int MAX = 4000010;
    VLL fact(MAX);
    fact[0] = 1;
    FOR(i, 1, MAX) fact[i] = fact[i - 1] * i % MOD;
    int t; cin >> t;
    while (t--) {
        int n; cin >> n;
        VI a(2 * n);
        bool ones = false, zeros = false, twos = false;
        REP(i, 2 * n) {
            cin >> a[i];
            if (a[i] == 0) zeros = true; 
            else if (a[i] == 1) ones = true; 
            else if (a[i] == 2) twos = true;
        }
        if (zeros && twos) {
            cout << "0\n"; continue;
        }
        bool same = true;
        int v = a[0];
        REP(i, a.size()) if (a[i] != v) same = false;
        if (same) {
            LL ans = 0;
            if (n == 1) ans = 4;
            else if (n == 2) ans = (v == 1 ? 256 : 16);
            else {
                if (v == 1) {
                    LL p = powMod(2, 2LL * n - 2, MOD);
                    LL invp = modInv(p, MOD);
                    ans = 1LL * n * n % MOD * fact[4 * n - 2] % MOD * invp % MOD;
                } else {
                    LL p = powMod(2, 2LL * n - 1, MOD);
                    LL invp = modInv(p, MOD);
                    ans = 1LL * n * (2LL * n - 1) % MOD * fact[4 * n - 2] % MOD * invp % MOD;
                }
            }
            cout << ans << '\n';
            continue;
        }
        if (twos) {
            REP(i, a.size()) if (a[i] == 2) a[i] = 0;
            rotate(a.begin(), a.begin() + 1, a.end());
        }
        bool possible = true;
        for (int i = 0; i < 2 * n; i += 2) {
            int odd = a[i];
            int even = a[i + 1];
            int nextOdd = a[(i + 2) % (2 * n)];
            if (!(odd >= even && even <= nextOdd)) { possible = false; break; }
        }
        if (!possible) {
            cout << "0\n";
            continue;
        }
        bool simple = true;
        REP(i, 2 * n) if (a[i] != (i % 2 == 0 ? 1 : 0)) { simple = false; break; }
        if (simple) cout << 1LL * n * fact[2 * n] % MOD * modInv(2, MOD) % MOD << '\n';
        else cout << "-1\n";
    }

    return 0;
}