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
//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
using ll=long long;
using ull=unsigned long long;
using pii=pair<int,int>;
using i128=__int128_t;
void die(string S){puts(S.c_str());exit(0);}
const ll mod=1e9+7;
int n;
ll a[2002000];
ll fact[4004000],inv[4004000],rfact[4004000];
ll pwi2[4004000];
ll Binom(int n,int k)
{
	if(k<0||k>n) return 0;
	return fact[n]*rfact[k]%mod*rfact[n-k]%mod;
}
ll calc()
{
	for(int i=1;i<=n;i++)
		if(!a[i])
			return 0;
	ll ret=0;
	int ok1=1;
	for(int i=1;i<=n;i++)
		ok1&=(a[i]==2);
	ll dis=fact[n];
	for(int i=1;i<=n/2;i++) dis=dis*(mod+1)/2%mod;
	dis=dis*dis%mod;
	if(ok1) ret=(ret+Binom(n*2-2,n-2)*dis)%mod;
	int ok2=1;
	for(int i=1;i<=n;i++)
		ok2&=(a[i]==1);
	if(ok2) ret=(ret+Binom(n*2-3,n-1)*dis)%mod;
	vector<int> vc;
	for(int i=1;i<=n;i++)
	{
		int j=i;
		while(j+1<=n&&a[j+1]==a[i]) j++;
		vc.pb(j-i+1);
		i=j;
	}
	if(sz(vc)==1) return ret;
	if(sz(vc)&1)
	{
		vc[0]+=vc.back();
		vc.pop_back();
	}
	for(auto x:vc)
		if(x%2==0)
			return ret;
	for(int i=1;i<=n;i++)
		if(a[i]==1&&a[i%n+1]==2)
			if(i%2!=0)
				return ret;
	if(a[1]==1)
	{
		vc.pb(vc[0]);
		vc.erase(vc.begin());
	}
	for(int i=1;i<sz(vc);i+=2)
	{
		int ln=vc[(i+1)%sz(vc)];
		int tot=(n-ln+1)/2;
		int rem=2*n-sz(vc)-1;
		ret=(ret+sz(vc)/2*fact[rem]%mod*pwi2[n-sz(vc)])%mod;
		if(sz(vc)<n)
			ret=(ret+(tot-sz(vc)/2)*fact[rem]%mod*pwi2[n-sz(vc)-1])%mod;
	}
	for(int i=1;i<sz(vc);i+=2)
	{
		int ln=vc[(i+1)%sz(vc)];
		if(ln<=1) continue;
		int rem=2*n-sz(vc)-2;
		ret=(ret+1ll*(ln/2)*(sz(vc)/2+1)%mod*fact[rem]%mod*pwi2[n-sz(vc)-1])%mod;
		if(sz(vc)+2<=n)
			ret=(ret+1ll*(ln/2)*(n/2-sz(vc)/2-1)%mod*fact[rem]%mod*pwi2[n-sz(vc)-2])%mod;
	}
	vc.pb(vc[0]);
	vc.erase(vc.begin());
	for(int i=1;i<sz(vc);i+=2)
	{
		int ln=vc[(i+1)%sz(vc)];
		if(ln<=1) continue;
		ll w=1ll*(ln/2)*(ln/2+1)/2%mod;
		int rem=2*n-sz(vc)-3;
		if(sz(vc)+2<=n)
			ret=(ret+w*(sz(vc)/2+1)%mod*fact[rem]%mod*pwi2[n-sz(vc)-2])%mod;
		if(sz(vc)+3<=n)
			ret=(ret+w*(n/2-sz(vc)/2-1)%mod*fact[rem]%mod*pwi2[n-sz(vc)-3])%mod;
	}
	for(int i=1;i<sz(vc);i+=2)
	{
		int ln=vc[(i+1)%sz(vc)];
		if(ln<=1) continue;
		int rem=2*n-sz(vc)-2;
		ret=(ret+1ll*(ln/2)*(sz(vc)/2)%mod*fact[rem]%mod*pwi2[n-sz(vc)-1])%mod;
		if(sz(vc)+2<=n)
			for(int j=1;j<ln;j+=2)
				ret=(ret+((n-ln+j)/2-(sz(vc)/2))*fact[rem]%mod*pwi2[n-sz(vc)-2])%mod;
	}
	return ret;
}
void solve()
{
	cin>>n;
	n*=2;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	fact[0]=rfact[0]=inv[1]=pwi2[0]=1;
	for(int i=2;i<=n*2;i++)
		inv[i]=(mod-mod/i)*inv[mod%i]%mod;
	for(int i=1;i<=n*2;i++)
	{
		pwi2[i]=pwi2[i-1]*(mod+1)/2%mod;
		fact[i]=fact[i-1]*i%mod;
		rfact[i]=rfact[i-1]*inv[i]%mod;
	}
	ll ans=calc();
	for(int i=1;i<=n;i++)
		a[i-1]=2-a[i];
	a[n]=a[0];
	ans=(ans+calc())%mod;
	cout<<ans<<'\n';
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin>>t;
	while(t--)
		solve();
	return 0;
}