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

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) begin(x), end(x)
#define sz(x) int((x).size())
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

#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(...) 2137
#endif

template<int M, int R>
struct Mod {
  static const int mod = M, rt = R;
  int x;
  Mod(ll y = 0) : x(y % M) { x += (x < 0) * M; }
  Mod& operator+=(Mod o) {
    if ((x += o.x) >= M) x -= M;
    return *this; }
  Mod& operator-=(Mod o) {
    if ((x -= o.x) < 0) x += M;
    return *this; }
  Mod& operator*=(Mod o) {
    x = 1ll * x * o.x % M;
    return *this; }
  Mod& operator/=(Mod o) { return *this *= o.inv(); }
  friend Mod operator+(Mod a, Mod b) { return a += b; }
  friend Mod operator-(Mod a, Mod b) { return a -= b; }
  friend Mod operator*(Mod a, Mod b) { return a *= b; }
  friend Mod operator/(Mod a, Mod b) { return a /= b; }
  auto operator<=>(const Mod&) const = default;
  Mod pow(ll n) const {
    Mod a = x, b = 1;
    for (; n; n /= 2, a *= a) if (n % 2) b *= a;
    return b; }
  Mod inv() const { assert(x); return pow(M - 2); }
  friend ostream& operator<<(ostream& os, Mod x) {
    return os << x.x; }
};
using mint = Mod<1000000007, 5>;

vector<mint> _f = {1}, _fi = {1};
void _ext(int n) {
  int k = sz(_f);
  if (n < k) return;
  n *= 2, _f.resize(n), _fi.resize(n);
  rep(i, k, n) _f[i] = _f[i - 1] * i;
  _fi[n - 1] = _f[n - 1].inv();
  for (int i = n - 2; i >= k; i--) _fi[i] = _fi[i + 1] * (i + 1);
}
mint F(int n) {
  if (n < 0) return 0;
  return _ext(n), _f[n];
}
mint Fi(int n) {
  if (n < 0) return 0;
  return _ext(n), _fi[n];
}
mint C(int n, int k) {
  if (n < k || k < 0) return 0;
  return _ext(n), _f[n] * _fi[k] * _fi[n - k];
}

const int N = 1000100;
int n;
mint odwr[4 * N];
int a[4 * N];
int suma[4 * N];
int nast[4 * N];

mint policz(int przed, int po, int juz, int ile) {
  mint odp = 0;
  // jeden
  odp += (2 * po - juz) * F(4 * n - ile - 1) * odwr[2 * n - ile];
  // dwa
  odp += (2 * przed) * (2 * (przed + po) - juz - 1) * F(4 * n - ile - 2) * odwr[2 * n - ile];
  return odp;
}

mint solve() {
  cin >> n;
  rep(i, 0, 2 * n) cin >> a[i];
  int mx = 0, mn = 2;
  rep(i, 0, 2 * n) mx = max(mx, a[i]), mn = min(mn, a[i]);
  if (mx - mn == 2) return 0;
  if (mn == 0) {
    rep(i, 0, 2 * n) if (a[i] == 0) a[i] = 2;
    rotate(a, a + 1, a + 2 * n);
  }
  if (count(a, a + 2 * n, 1) == 2 * n) {
    return 2 * n * policz(n, 0, 0, 1);
  }
  if (count(a, a + 2 * n, 2) == 2 * n) {
    return mint(2 * n) * mint(2 * n - 1) * F(4 * n - 2) * odwr[2 * n];
  }
  rep(i, 0, 2 * n) {
    if (a[i] != a[(i + 1) % (2 * n)]) {
      if (a[i] == 2 && i % 2) return 0;
      if (a[i] == 1 && i % 2 == 0) return 0;
    }
  }
  rep(i, 0, 2 * n) a[2 * n + i] = a[i];
  nast[4 * n] = nast[4 * n - 1] = 4 * n;
  for (int i = 4 * n - 1; i >= 0; i--) {
    nast[i] = nast[i + 1];
    if (a[i] != a[i + 1]) nast[i] = i;
  }
  rep(i, 0, 4 * n - 1) suma[i + 1] = suma[i] + (a[i] != a[i + 1]);
  mint odp = 0;
  rep(i, 0, 2 * n) {
    if (i % 2 == 0 && a[i + 2 * n - 1] != 2) {
      int ost = min(nast[i], i + 2 * n) - i;
      int ile = 1 + suma[i + 2 * n - 1] - suma[i];
      int przed, po, juz;
      if (ost % 2 == 1) { // ost A
        przed = (ost + 1) / 2;
        juz = ile / 2;
      } else { // ost B
        przed = ost / 2;
        juz = (ile + 1) / 2;
      }
      po = n - przed;
      odp += policz(przed, po, juz, ile);
    }
  }
  return odp;
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  odwr[0] = 1;
  odwr[1] = mint(2).inv();
  rep(i, 2, 4 * N) odwr[i] = odwr[1] * odwr[i - 1];
  int t;
  cin >> t;
  while (t--) cout << solve() << '\n';
}