#include <bits/stdc++.h>
using namespace std;
using ind = long long;
using cind = const ind;
#define FOR(i,l,r) for(ind i = (l); i <= (r); i++)
#define FORD(i,l,r) for(ind i = (l); i >= (r); i--)
#define DEBUG_ON false
#define debug if constexpr(DEBUG_ON)
#define err debug cerr
ind MOD=1'000'000'007;
ind pow2(ind a, ind p){
ind w=1;
while(p > 0){
if(p%2==1) w = (w * a) % MOD;
a = (a*a)%MOD;
p/=2;
}
return w;
}
ind inversed(ind a){
return pow2(a,MOD-2);
}
ind v[2'000'001];
ind factorial[4'000'001];
ind inverse_factorial[4'000'001];
ind filling[2'000'001];
struct mod_ind{
ind v;
void operator=(ind a){v=a%MOD;}
mod_ind operator*(ind a){return mod_ind({(v*a)%MOD});}
};
void solve(){
ind N;
cin >> N;
FOR(i,1,2*N) cin >> v[i];
bool a=true;
FOR(i,1,2*N) if(v[i] != 1) a=false;
if(a){
ind wyn = 0;
if(N>=2) wyn += (mod_ind({2*N})*(N)*(N-1)*(4*N-3)*(4*N-4)*(4*N-5)*filling[2*N-3]).v;
wyn += (mod_ind({2*N})*(N)*(4*N-3)*filling[2*N-2]).v;
cout << wyn%MOD << endl;
return;
}
bool a0=false;
bool a2=false;
FOR(i,1,2*N) if(v[i]==0) a0=true;
FOR(i,1,2*N) if(v[i]==2) a2=true;
if(a0 and a2){
cout << "0\n";
return;
}
if(a2){
FOR(i,1,2*N) if(v[i]==2) v[i]=0;
FOR(i,2,2*N) swap(v[i-1],v[i]);
a2=false;
a0=true;
}
a=true;
FOR(i,1,2*N) if(v[i] != 0) a=false;
if(a){
ind wyn = 0;
if(N >= 2) wyn += (mod_ind({2*N})*(N-1)*(4*N-2)*(4*N-3)*filling[2*N-2]).v;
wyn += (mod_ind({2*N})*filling[2*N-1]).v;
cout << wyn%MOD << endl;
return;
}
FOR(i,2,2*N){
if(v[i]==1 and v[i-1]==0) if(i%2 == 0){cout << "0\n"; return;}
if(v[i]==0 and v[i-1]==1) if(i%2 == 1){cout << "0\n"; return;}
}
if(v[1]==0 and v[2*N]==1) {cout << "0\n"; return;}
ind e = 2*N;
ind add = 0;
vector<pair<ind,ind>> groups;
while(v[1] == v[e]){
e--;
add++;
}
ind len = 1;
FOR(i,2,e){
if(v[i] == v[i-1]){
len++;
continue;
}
groups.push_back({v[i-1],len+add});
len=1;
add=0;
}
groups.push_back({v[e], len+add});
len=1;
add=0;
ind w1=0;
// w1 += (mod_ind({ind(groups.size())/2})
// * factorial[4*N-(groups.size()/2)]
// * inverse_factorial[4*N-groups.size()]
// * filling[2*N-groups.size()]).v;
ind w2=0;
// FOR(j,0,ind(groups.size())-1){
// if(groups[j].first==0) continue;
// len = groups[j].second;
// w1 += (mod_ind({factorial[4*N-(groups.size()/2)]})
// * inverse_factorial[4*N-groups.size()]
// * filling[2*N-groups.size()]).v;
// if(len > 1)
// w1 -= (mod_ind({factorial[4*N-(groups.size()/2)-1]})
// * (len-1)
// * inverse_factorial[4*N-groups.size()-2]
// * filling[2*N-groups.size()-1]).v;
// }
// cerr << "groups " << groups.size() << endl;
FOR(j,0,ind(groups.size())-1){
if(groups[j].first==0) continue;
len = groups[j].second;
w1 += (mod_ind({factorial[4*N-groups.size()]})
* inverse_factorial[4*N-2*groups.size()]
* filling[2*N-groups.size()]).v;
if(len > 1)
w1 -= (mod_ind({factorial[4*N-groups.size()-1]})
* (len-1)
* inverse_factorial[4*N-2*groups.size()-2]
* filling[2*N-groups.size()-1]).v;
// cerr << w1 << endl;
}
cout << (w1 + w2)%MOD << endl;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cerr << "hm" << endl;
factorial[0]=1;
FOR(i,1,4'000'00) factorial[i] = (factorial[i-1]*i)%MOD;
filling[0]=1;
FOR(i,1,2'000'00) filling[i] = (((filling[i-1] * (i))%MOD) * (2*i-1)) % MOD;//filling[i] = (filling[i-1] * (2*i) * (2*i-1) / 2) % MOD;
inverse_factorial[0]=1;
FOR(i,1,4'000'00) inverse_factorial[i] = inversed(factorial[i]);
cerr << "nah" << endl;
ind T;
cin >> T;
FOR(ti,1,T) solve();
}
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 | #include <bits/stdc++.h> using namespace std; using ind = long long; using cind = const ind; #define FOR(i,l,r) for(ind i = (l); i <= (r); i++) #define FORD(i,l,r) for(ind i = (l); i >= (r); i--) #define DEBUG_ON false #define debug if constexpr(DEBUG_ON) #define err debug cerr ind MOD=1'000'000'007; ind pow2(ind a, ind p){ ind w=1; while(p > 0){ if(p%2==1) w = (w * a) % MOD; a = (a*a)%MOD; p/=2; } return w; } ind inversed(ind a){ return pow2(a,MOD-2); } ind v[2'000'001]; ind factorial[4'000'001]; ind inverse_factorial[4'000'001]; ind filling[2'000'001]; struct mod_ind{ ind v; void operator=(ind a){v=a%MOD;} mod_ind operator*(ind a){return mod_ind({(v*a)%MOD});} }; void solve(){ ind N; cin >> N; FOR(i,1,2*N) cin >> v[i]; bool a=true; FOR(i,1,2*N) if(v[i] != 1) a=false; if(a){ ind wyn = 0; if(N>=2) wyn += (mod_ind({2*N})*(N)*(N-1)*(4*N-3)*(4*N-4)*(4*N-5)*filling[2*N-3]).v; wyn += (mod_ind({2*N})*(N)*(4*N-3)*filling[2*N-2]).v; cout << wyn%MOD << endl; return; } bool a0=false; bool a2=false; FOR(i,1,2*N) if(v[i]==0) a0=true; FOR(i,1,2*N) if(v[i]==2) a2=true; if(a0 and a2){ cout << "0\n"; return; } if(a2){ FOR(i,1,2*N) if(v[i]==2) v[i]=0; FOR(i,2,2*N) swap(v[i-1],v[i]); a2=false; a0=true; } a=true; FOR(i,1,2*N) if(v[i] != 0) a=false; if(a){ ind wyn = 0; if(N >= 2) wyn += (mod_ind({2*N})*(N-1)*(4*N-2)*(4*N-3)*filling[2*N-2]).v; wyn += (mod_ind({2*N})*filling[2*N-1]).v; cout << wyn%MOD << endl; return; } FOR(i,2,2*N){ if(v[i]==1 and v[i-1]==0) if(i%2 == 0){cout << "0\n"; return;} if(v[i]==0 and v[i-1]==1) if(i%2 == 1){cout << "0\n"; return;} } if(v[1]==0 and v[2*N]==1) {cout << "0\n"; return;} ind e = 2*N; ind add = 0; vector<pair<ind,ind>> groups; while(v[1] == v[e]){ e--; add++; } ind len = 1; FOR(i,2,e){ if(v[i] == v[i-1]){ len++; continue; } groups.push_back({v[i-1],len+add}); len=1; add=0; } groups.push_back({v[e], len+add}); len=1; add=0; ind w1=0; // w1 += (mod_ind({ind(groups.size())/2}) // * factorial[4*N-(groups.size()/2)] // * inverse_factorial[4*N-groups.size()] // * filling[2*N-groups.size()]).v; ind w2=0; // FOR(j,0,ind(groups.size())-1){ // if(groups[j].first==0) continue; // len = groups[j].second; // w1 += (mod_ind({factorial[4*N-(groups.size()/2)]}) // * inverse_factorial[4*N-groups.size()] // * filling[2*N-groups.size()]).v; // if(len > 1) // w1 -= (mod_ind({factorial[4*N-(groups.size()/2)-1]}) // * (len-1) // * inverse_factorial[4*N-groups.size()-2] // * filling[2*N-groups.size()-1]).v; // } // cerr << "groups " << groups.size() << endl; FOR(j,0,ind(groups.size())-1){ if(groups[j].first==0) continue; len = groups[j].second; w1 += (mod_ind({factorial[4*N-groups.size()]}) * inverse_factorial[4*N-2*groups.size()] * filling[2*N-groups.size()]).v; if(len > 1) w1 -= (mod_ind({factorial[4*N-groups.size()-1]}) * (len-1) * inverse_factorial[4*N-2*groups.size()-2] * filling[2*N-groups.size()-1]).v; // cerr << w1 << endl; } cout << (w1 + w2)%MOD << endl; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cerr << "hm" << endl; factorial[0]=1; FOR(i,1,4'000'00) factorial[i] = (factorial[i-1]*i)%MOD; filling[0]=1; FOR(i,1,2'000'00) filling[i] = (((filling[i-1] * (i))%MOD) * (2*i-1)) % MOD;//filling[i] = (filling[i-1] * (2*i) * (2*i-1) / 2) % MOD; inverse_factorial[0]=1; FOR(i,1,4'000'00) inverse_factorial[i] = inversed(factorial[i]); cerr << "nah" << endl; ind T; cin >> T; FOR(ti,1,T) solve(); } |
English