#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define BOOST ios_base::sync_with_stdio(0), cin.tie(0)
template<typename T>
using vc = vector<T>;
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
template<int MOD>
struct mod{
int val;
mod(ll x = 0){x%=MOD; if(x<0)x+=MOD; val=x;}
mod operator+(const mod& x) const{return mod(val+x.val);}
mod operator-(const mod& x) const{return mod(val-x.val);}
mod operator*(const mod& x) const{return mod(1ll*val*x.val);}
mod operator/(const mod& x) const{return *this*x.power(MOD-2);}
mod& operator+=(const mod& x){return *this=*this+x;}
mod& operator-=(const mod& x){return *this=*this-x;}
mod& operator*=(const mod& x){return *this=*this*x;}
mod& operator/=(const mod& x){return *this=*this/x;}
bool operator==(const mod& x) const{return val==x.val;}
bool operator!=(const mod& x) const{return val!=x.val;}
friend std::ostream& operator<<(std::ostream& out, const mod& x){return out << x.val;}
mod power(int p) const{
mod x = *this;
if(p < 0) return x.power(-p).power(MOD-2);
mod res = {1};
for(; p; p>>=1){
if(p&1) res *= x;
x *= x;
}
return res;
}
};
using mint = mod<(int)1e9+7>;
const int N = 5e6 + 5;
mint f[N];
mint binom(int a, int b){
if(a < b) return 0;
return f[a] / (f[b] * f[a-b]);
}
mint pow2[N];
void solve(){
int n;
cin >> n;
vc<int> t(4*n+1);
bool zero = 0, one = 0, two = 0;
for(int i=1; i<=2*n; i++){
cin >> t[i];
if(t[i] == 0) zero = 1;
if(t[i] == 1) one = 1;
if(t[i] == 2) two = 1;
}
if(zero && two){
cout << 0 << "\n";
return;
}
if(zero){
int swp = t[1];
for(int i=1; i<2*n; i++) t[i] = t[i+1];
t[2*n] = swp;
for(int i=1; i<=2*n; i++) if(t[i] == 0) t[i] = 2;
}
for(int i=1; i<=2*n; i++) t[2*n+i] = t[i];
zero = 0, one = 0, two = 0;
for(int i=1; i<=4*n; i++){
if(t[i] == 0) zero = 1;
if(t[i] == 1) one = 1;
if(t[i] == 2) two = 1;
}
assert(!zero);
mint ans = 0;
if(one && !two){
if(n > 1) ans += (mint)n * n * (n-1) * f[4*n-3] * pow2[2*n-3];
ans += (mint)n * n * f[4*n-3] * pow2[2*n-2];
cout << ans * 2 << "\n";
return;
}
if(!one && two){
if(n > 1) ans += (mint)n * (n-1) * f[4*n-2] * pow2[2*n-2];
ans += (mint)n * f[4*n-2] * pow2[2*n-1];
cout << ans << "\n";
return;
}
vc<int> sc;
int bad = 0;
int last = 0;
auto upd = [&](int i){
int nxt = i + 2*n;
if(t[nxt-1] != t[nxt]){
if((sc.back()&1) == ((nxt-1)&1)){
bad++;
}
sc.pb(nxt-1);
}
if(sc[last] <= i){
if((sc[last]&1) == (sc[last+1]&1)) bad--;
last++;
}
};
auto skip = [&](int i) -> bool {
if(bad) return true;
if((sc.back()&1) == 0) return true;
if(t[i + 2*n-1] == 2) return true;
return false;
};
for(int i=1; i<2*n; i++){
if(t[i] != t[i+1]) sc.pb(i);
}
for(int i=1; i<(int)sc.size(); i++){
if((sc[i-1]&1) == (sc[i]&1)) bad++;
}
for(int i=1; i<=2*n; i+=2){
int s = sc.size();
assert(last != s);
int fnt = sc[last];
if(skip(i)){upd(i); upd(i+1); continue;}
int seq = s - last + 1;
int afree = 2*n-seq;
int myseq = seq/2;
int myfree = n - myseq;
int myleft = ((fnt-i+1)/2);
int myright = myfree - myleft;
int cards = 4*n-seq;
mint p1 = 0, p2 = 0, p3 = 0, p4 = 0, ways = 0;
if(myfree >= 1) p1 = (mint)myleft * (myseq+1) * f[cards-2] * pow2[afree-1]; // 4n-3 w zajete
if(myfree >= 2) p2 = (mint)myleft * (myfree-1) * f[cards-2] * pow2[afree-2]; // 4n-3 w wolne
if(myright) p3 = (mint)myright * f[cards-1] * pow2[afree-1]; // 4n-2 w wolne na prawo
if(myseq) p4 = (mint)myseq * f[cards-1] * pow2[afree]; // 4n-2 w zajete na prawo
ways = p1 + p2 + p3 + p4;
ans += ways;
upd(i); upd(i+1);
}
cout << ans << "\n";
}
int main(){
BOOST;
f[0] = 1;
for(int i=1; i<N; i++) f[i] = f[i-1] * i;
pow2[0] = 1;
pow2[1] = (mint)1/2;
for(int i=2; i<N; i++) pow2[i] = pow2[i-1] * pow2[1];
int q;
cin >> q;
while(q--) 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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | #include <bits/stdc++.h> using namespace std; #define st first #define nd second #define pb push_back #define all(x) (x).begin(), (x).end() #define BOOST ios_base::sync_with_stdio(0), cin.tie(0) template<typename T> using vc = vector<T>; using ll = long long; using ld = long double; using ii = pair<int, int>; template<int MOD> struct mod{ int val; mod(ll x = 0){x%=MOD; if(x<0)x+=MOD; val=x;} mod operator+(const mod& x) const{return mod(val+x.val);} mod operator-(const mod& x) const{return mod(val-x.val);} mod operator*(const mod& x) const{return mod(1ll*val*x.val);} mod operator/(const mod& x) const{return *this*x.power(MOD-2);} mod& operator+=(const mod& x){return *this=*this+x;} mod& operator-=(const mod& x){return *this=*this-x;} mod& operator*=(const mod& x){return *this=*this*x;} mod& operator/=(const mod& x){return *this=*this/x;} bool operator==(const mod& x) const{return val==x.val;} bool operator!=(const mod& x) const{return val!=x.val;} friend std::ostream& operator<<(std::ostream& out, const mod& x){return out << x.val;} mod power(int p) const{ mod x = *this; if(p < 0) return x.power(-p).power(MOD-2); mod res = {1}; for(; p; p>>=1){ if(p&1) res *= x; x *= x; } return res; } }; using mint = mod<(int)1e9+7>; const int N = 5e6 + 5; mint f[N]; mint binom(int a, int b){ if(a < b) return 0; return f[a] / (f[b] * f[a-b]); } mint pow2[N]; void solve(){ int n; cin >> n; vc<int> t(4*n+1); bool zero = 0, one = 0, two = 0; for(int i=1; i<=2*n; i++){ cin >> t[i]; if(t[i] == 0) zero = 1; if(t[i] == 1) one = 1; if(t[i] == 2) two = 1; } if(zero && two){ cout << 0 << "\n"; return; } if(zero){ int swp = t[1]; for(int i=1; i<2*n; i++) t[i] = t[i+1]; t[2*n] = swp; for(int i=1; i<=2*n; i++) if(t[i] == 0) t[i] = 2; } for(int i=1; i<=2*n; i++) t[2*n+i] = t[i]; zero = 0, one = 0, two = 0; for(int i=1; i<=4*n; i++){ if(t[i] == 0) zero = 1; if(t[i] == 1) one = 1; if(t[i] == 2) two = 1; } assert(!zero); mint ans = 0; if(one && !two){ if(n > 1) ans += (mint)n * n * (n-1) * f[4*n-3] * pow2[2*n-3]; ans += (mint)n * n * f[4*n-3] * pow2[2*n-2]; cout << ans * 2 << "\n"; return; } if(!one && two){ if(n > 1) ans += (mint)n * (n-1) * f[4*n-2] * pow2[2*n-2]; ans += (mint)n * f[4*n-2] * pow2[2*n-1]; cout << ans << "\n"; return; } vc<int> sc; int bad = 0; int last = 0; auto upd = [&](int i){ int nxt = i + 2*n; if(t[nxt-1] != t[nxt]){ if((sc.back()&1) == ((nxt-1)&1)){ bad++; } sc.pb(nxt-1); } if(sc[last] <= i){ if((sc[last]&1) == (sc[last+1]&1)) bad--; last++; } }; auto skip = [&](int i) -> bool { if(bad) return true; if((sc.back()&1) == 0) return true; if(t[i + 2*n-1] == 2) return true; return false; }; for(int i=1; i<2*n; i++){ if(t[i] != t[i+1]) sc.pb(i); } for(int i=1; i<(int)sc.size(); i++){ if((sc[i-1]&1) == (sc[i]&1)) bad++; } for(int i=1; i<=2*n; i+=2){ int s = sc.size(); assert(last != s); int fnt = sc[last]; if(skip(i)){upd(i); upd(i+1); continue;} int seq = s - last + 1; int afree = 2*n-seq; int myseq = seq/2; int myfree = n - myseq; int myleft = ((fnt-i+1)/2); int myright = myfree - myleft; int cards = 4*n-seq; mint p1 = 0, p2 = 0, p3 = 0, p4 = 0, ways = 0; if(myfree >= 1) p1 = (mint)myleft * (myseq+1) * f[cards-2] * pow2[afree-1]; // 4n-3 w zajete if(myfree >= 2) p2 = (mint)myleft * (myfree-1) * f[cards-2] * pow2[afree-2]; // 4n-3 w wolne if(myright) p3 = (mint)myright * f[cards-1] * pow2[afree-1]; // 4n-2 w wolne na prawo if(myseq) p4 = (mint)myseq * f[cards-1] * pow2[afree]; // 4n-2 w zajete na prawo ways = p1 + p2 + p3 + p4; ans += ways; upd(i); upd(i+1); } cout << ans << "\n"; } int main(){ BOOST; f[0] = 1; for(int i=1; i<N; i++) f[i] = f[i-1] * i; pow2[0] = 1; pow2[1] = (mint)1/2; for(int i=2; i<N; i++) pow2[i] = pow2[i-1] * pow2[1]; int q; cin >> q; while(q--) solve(); } |
English