#include <bits/stdc++.h>
using namespace std;
#define fors(u, n, s) for(int u = (s); u<(n); u++)
#define foru(u, n) fors(u, n, 0)
#define f first
#define s second
#define vec vector
#define pb push_back
#define sz(x) ((int)x.size())
typedef long long ll;
// #define debug(x) cerr << __LINE__ << " | " << x << endl
#define debug(x) {}
const int MOD = 1e9+7;
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) {
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;}
};
const int N = 5e6;
Modular fac[N];
Modular twoexp[N];
Modular anachronic_help(int n, int swaps, int first_swap){
int up_swaps = swaps>>1;
int down_swaps = swaps - up_swaps;
bool first_swap_down = swaps&1;
int spaces;
if(first_swap_down){
spaces = n - up_swaps - first_swap - (first_swap&1);
} else {
spaces = n - down_swaps - first_swap + (first_swap&1);
}
int free = 2*n-swaps-1;
return twoexp[swaps]*Modular(spaces)*fac[free];
}
Modular duble_help(int n, int swaps, int first_swap){
int up_swaps = swaps>>1;
int down_swaps = swaps - up_swaps;
bool first_swap_down = swaps&1;
int first_spaces;
int second_spaces;
if(first_swap_down) {
first_spaces = first_swap + (first_swap&1);
second_spaces = n - up_swaps - 1;
} else {
first_spaces = first_swap - (first_swap&1);
second_spaces = n - down_swaps - 1;
}
int free = 2*n-swaps-2;
return twoexp[swaps]*Modular(first_spaces)*Modular(second_spaces)*fac[free];
}
void solve() {
int n; cin >> n; n *= 2;
vec<int> v(n);
foru(i, n) cin >> v[i];
int zero_cnt = 0;
int two_cnt = 0;
for(auto i : v)
zero_cnt += (i==0),
two_cnt += (i==2);
if(zero_cnt && two_cnt){
cout << 0 << endl;
return;
}
if(zero_cnt == 0 && two_cnt == 0){
cout << Modular(2)*Modular(n)*Modular(n)*Modular(n-1)*fac[2*n-3]/mexp(Modular(2), n) << endl;
return;
}
if(zero_cnt == n || two_cnt == n){
cout << Modular(n/2)*Modular(2)*Modular(n-1)*fac[2*n-2]/mexp(Modular(2), n) << endl;
return;
}
bool winner = (two_cnt != 0);
foru(i, n) v[i] = (v[i] != 1);
if(!winner) foru(i, n-1) swap(v[i], v[i+1]);
Modular ans = 0;
bool fst = v[0];
vec<int> segs;
int st_idx = 0;
int even_cnt = 0;
foru(i, n){
if(i == 0 || v[i-1] != v[i]) segs.pb(1);
else segs.back()++;
}
for(auto i : segs) even_cnt += ((i&1) == 0);
foru(_i, n/2){
debug(st_idx);
int len = sz(segs)-st_idx;
if(!(fst^(len&1))) goto skip;
if(even_cnt >= 2) goto skip;
if(even_cnt == 1 && segs[st_idx]&1) goto skip;
ans += anachronic_help(n, len, segs[st_idx]-1);
ans += duble_help(n, len, segs[st_idx]-1);
skip:;
int todo = 2;
while(todo != 0){
int op = min(todo, segs[st_idx]);
todo -= op;
len = sz(segs)-st_idx;
if((len&1) == 0) segs.pb(0), even_cnt++;
even_cnt -= ((segs[st_idx]&1) == 0);
even_cnt -= ((segs.back()&1) == 0);
segs[st_idx] -= op;
segs.back() += op;
even_cnt += ((segs[st_idx]&1) == 0);
even_cnt += ((segs.back()&1) == 0);
if(segs[st_idx] == 0){
st_idx ++;
fst = !fst;
even_cnt--;
}
}
}
cout << ans/mexp(Modular(2), n) << endl;
}
int main()
{
cin.tie(0); cout.tie(0);
ios_base::sync_with_stdio(0);
srand(time(NULL));
fac[0] = 1;
fors(i, N, 1) fac[i] = fac[i-1]*Modular(i);
twoexp[0] = 1;
fors(i, N, 1) twoexp[i] = twoexp[i-1]*Modular(2);
int t; cin >> t;
foru(_i, t)
solve();
return 0;
}
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 | #include <bits/stdc++.h> using namespace std; #define fors(u, n, s) for(int u = (s); u<(n); u++) #define foru(u, n) fors(u, n, 0) #define f first #define s second #define vec vector #define pb push_back #define sz(x) ((int)x.size()) typedef long long ll; // #define debug(x) cerr << __LINE__ << " | " << x << endl #define debug(x) {} const int MOD = 1e9+7; 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) { 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;} }; const int N = 5e6; Modular fac[N]; Modular twoexp[N]; Modular anachronic_help(int n, int swaps, int first_swap){ int up_swaps = swaps>>1; int down_swaps = swaps - up_swaps; bool first_swap_down = swaps&1; int spaces; if(first_swap_down){ spaces = n - up_swaps - first_swap - (first_swap&1); } else { spaces = n - down_swaps - first_swap + (first_swap&1); } int free = 2*n-swaps-1; return twoexp[swaps]*Modular(spaces)*fac[free]; } Modular duble_help(int n, int swaps, int first_swap){ int up_swaps = swaps>>1; int down_swaps = swaps - up_swaps; bool first_swap_down = swaps&1; int first_spaces; int second_spaces; if(first_swap_down) { first_spaces = first_swap + (first_swap&1); second_spaces = n - up_swaps - 1; } else { first_spaces = first_swap - (first_swap&1); second_spaces = n - down_swaps - 1; } int free = 2*n-swaps-2; return twoexp[swaps]*Modular(first_spaces)*Modular(second_spaces)*fac[free]; } void solve() { int n; cin >> n; n *= 2; vec<int> v(n); foru(i, n) cin >> v[i]; int zero_cnt = 0; int two_cnt = 0; for(auto i : v) zero_cnt += (i==0), two_cnt += (i==2); if(zero_cnt && two_cnt){ cout << 0 << endl; return; } if(zero_cnt == 0 && two_cnt == 0){ cout << Modular(2)*Modular(n)*Modular(n)*Modular(n-1)*fac[2*n-3]/mexp(Modular(2), n) << endl; return; } if(zero_cnt == n || two_cnt == n){ cout << Modular(n/2)*Modular(2)*Modular(n-1)*fac[2*n-2]/mexp(Modular(2), n) << endl; return; } bool winner = (two_cnt != 0); foru(i, n) v[i] = (v[i] != 1); if(!winner) foru(i, n-1) swap(v[i], v[i+1]); Modular ans = 0; bool fst = v[0]; vec<int> segs; int st_idx = 0; int even_cnt = 0; foru(i, n){ if(i == 0 || v[i-1] != v[i]) segs.pb(1); else segs.back()++; } for(auto i : segs) even_cnt += ((i&1) == 0); foru(_i, n/2){ debug(st_idx); int len = sz(segs)-st_idx; if(!(fst^(len&1))) goto skip; if(even_cnt >= 2) goto skip; if(even_cnt == 1 && segs[st_idx]&1) goto skip; ans += anachronic_help(n, len, segs[st_idx]-1); ans += duble_help(n, len, segs[st_idx]-1); skip:; int todo = 2; while(todo != 0){ int op = min(todo, segs[st_idx]); todo -= op; len = sz(segs)-st_idx; if((len&1) == 0) segs.pb(0), even_cnt++; even_cnt -= ((segs[st_idx]&1) == 0); even_cnt -= ((segs.back()&1) == 0); segs[st_idx] -= op; segs.back() += op; even_cnt += ((segs[st_idx]&1) == 0); even_cnt += ((segs.back()&1) == 0); if(segs[st_idx] == 0){ st_idx ++; fst = !fst; even_cnt--; } } } cout << ans/mexp(Modular(2), n) << endl; } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); srand(time(NULL)); fac[0] = 1; fors(i, N, 1) fac[i] = fac[i-1]*Modular(i); twoexp[0] = 1; fors(i, N, 1) twoexp[i] = twoexp[i-1]*Modular(2); int t; cin >> t; foru(_i, t) solve(); return 0; } |
English