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
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
auto operator<<(auto& o, auto x) -> decltype(x.first, o);
auto operator<<(auto& o, auto x) -> decltype(x.end(), o) {
    o << "{";
    for (int i = 0; auto y : x) o << ", " + !i++ * 2 << y;
    return o << "}"; }
auto operator<<(auto& o, auto x) -> decltype(x.first, o) {
    return o << "(" << x.first << ", " << x.second << ")"; }
void __print(auto... x) { ((cerr << x << " "), ...) << endl; }
#define debug(x...) __print("[" #x "]:", x)
#else
#define debug(...) {}
#endif

#define x first
#define y second
#define ir(x, a, b) ((a) <= (x) && (x) <= (b))
#define vec vector
#define rep(i, a, b) for (ll i = a; i < (b); ++i)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)((x).size()))

using ll = long long;
using pii = pair<ll, ll>;

template <int MOD>
struct Modular {
  int value;
  static const int MOD_value = MOD;

  Modular(long long v = 0) { value = v % MOD; if (value < 0) value += MOD;}
  Modular(long long a, long long b) : value(0){ *this += a; *this /= b;}

  Modular& operator+=(Modular const& b) {value += b.value; if (value >= MOD) value -= MOD; return *this;}
  Modular& operator-=(Modular const& b) {value -= b.value; if (value < 0) value += MOD;return *this;}
  Modular& operator*=(Modular const& b) {value = (long long)value * b.value % MOD;return *this;}

  friend Modular mexp(Modular a, long long e) {
    if (e < 0) return 1;
    Modular res = 1; while (e) { if (e&1) res *= a; a *= a; e >>= 1; }
    return res;
  }
  friend Modular inverse(Modular a) { return mexp(a, MOD - 2); }

  Modular& operator/=(Modular const& b) { return *this *= inverse(b); }
  friend Modular operator+(Modular a, Modular const b) { return a += b; }
  friend Modular operator-(Modular a, Modular const b) { return a -= b; }
  friend Modular operator-(Modular const a) { return 0 - a; }
  friend Modular operator*(Modular a, Modular const b) { return a *= b; }
  friend Modular operator/(Modular a, Modular const b) { return a /= b; }
  friend std::ostream& operator<<(std::ostream& os, Modular const& a) {return os << a.value;}
  friend bool operator==(Modular const& a, Modular const& b) {return a.value == b.value;}
  friend bool operator!=(Modular const& a, Modular const& b) {return a.value != b.value;}
};

using mint = Modular<ll(1e9)+7>;

vec<mint> f(5e6);
vec<mint> inv2(3e6);

void solve() {
  int N; cin >> N;
  vec<int> a(2*N);
  vec<int> tt(3);
  rep (n, 0, 2*N) {
    cin >> a[n];
    tt[a[n]]++;
  }

  if (tt[0] > 0 && tt[2] > 0) { cout << "0\n"; return; }
  int winner = 0;
  if (tt[2]) {
    rep (n, 0, 2*N) a[n] = 2 - a[n];
    winner = 1;
  } else {
    winner = 0;
  }
  bool allodd = tt[2] == 0 && tt[0] == 0;
  rep (n, 0, 2*N) a[n] = 1 - a[n];
  debug(winner, a);

  auto fact = [&](int x) {
    return f[x];
  };

  vec<pii> seqs = {{a[0], 1}};
  rep (n, 1, 2*N) {
    if (a[n] == seqs.back().x) {
      seqs.back().y++;
    } else {
      seqs.push_back({a[n], 1});
    }
  }

  if (seqs[0].x == seqs.back().x && seqs.size() > 1) {
    debug(winner, seqs);
    winner ^= (seqs.back().y % 2);
    seqs[0].y += seqs.back().y;
    seqs.pop_back();
  }


  if (seqs.size() == 1 && seqs[0].x == 1) {
    cout << N * (2*N - 1) * fact(4*N - 2) / mexp(mint(2), 2*N-1) << "\n";
    return;
  } else if (seqs.size() == 2*N) {
    cout << (winner != seqs[0].x ? 0 : N * N * fact(2*N-1)) << "\n";
    return;
  }

  for (auto x : seqs) {
    if (seqs.size() != 1 && x.y % 2 == 0) {
      cout << "0\n";
      return;
    }
  }

  // check all odd
  debug(seqs);

  auto mexp = [&](int x) {
    if (x < 0) return mint(1);
    return inv2[x];
  };
  mint sum = 0;
  int rn = 0;
  rep (i, 0, seqs.size()) {
    for (int ct = 1; ct <= seqs[i].y; ct++) {
      int n = rn + ct - 1;
      // suppose a[n] has max
      int switches, first_len = ct, last_len;
      if (ct == seqs[i].y || seqs.size() == 1) {
        switches = seqs.size(), last_len = seqs[(i+1)%seqs.size()].y;
      } else {
        switches = seqs.size()+1, last_len = seqs[i].y - ct;
      }

      // debug(n, switches, first_len, ct, winner);
      debug(n, winner);
      if (n % 2 != winner) continue;
      // debug("passed");
      if (seqs[i].x == 1) {
        continue;
      }
      // debug("passed");
      if (first_len % 2 == 0 && seqs.size() != 1) continue;
      debug("passed", n);

      // A even, winner
      mint tA = (switches+1)/2;
      mint tB = switches/2;
      mint A = N - tA;
      mint B = N - tB;
      // debug(switches, tA, tB, n);
      // debug(A);
      // debug(4*N - (switches+1));
      // debug(2*N - switches);
      // hold two consecutive

      // amount of cards on the side of the last swithc player to the left of it
      mint last = (last_len-1) / 2;
      mint tlast = switches % 2 == 0 ? tB : tA;
      mint tother = switches % 2 != 0 ? tB : tA;

      debug(4*N - (switches+2));
      debug(2*N - switches);
      debug(2*last, 2*N-tother-1);
      mint doub = (2 * (last_len/2)) * (2 * N - tother - 1) * fact(4*N - (switches+2)) * mexp(2*N - switches);

      // next card is enemy's and majorised
      // debug(last);
      // mint bothleft = last * (last - 1) / 2 * fact(4*N - (switches + 2)) / mexp(mint(2), 2*N - (switches + 2));
      // mint dived = last * (2*(N - last_len/2) - tlast) * fact(4*N - (switches + 2)) / mexp(mint(2), 2*N - (switches + 1));
      debug(switches, first_len, last_len, ct, doub);

      mint x = (2*N - last_len + 1) / 2;
      debug(x, 2*x - tother, 4*N - (switches + 1), switches);
      mint majorised = (2 * x - tother) * fact(4 * N - (switches + 1)) * mexp(2*N - switches);
      debug(majorised);

      sum += doub + majorised;
    }
    rn += seqs[i].y;
  }
  if (seqs.size() == 1) sum *= 2;
  cout << sum << "\n";
}

int main() {
  inv2[sz(inv2)-1] = inverse(mexp(mint(2), sz(inv2)-1));
  for (int i = inv2.size()-2; i >= 0; i--) {
    inv2[i] = inv2[i+1] * 2;
  }
  f[0] = 1;
  rep (i, 1, f.size()) {
    f[i] = f[i-1] * mint(i);
  }
  cin.tie(0)->sync_with_stdio(0);
  int T; cin >> T;
  while (T--) solve();
  return 0;
}