#pragma GCC optimize("O3,unroll-loops,inline")
#include "bits/stdc++.h"
using namespace std;
#define rep(i,a,b) for(int i=(a); i<(b); ++i)
#define all(x) x.begin(),x.end()
#define sz(x) int(x.size())
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
const ll mod = 1e9+7;
ll mul(ll a, ll b){
return a*b%mod;
}ll modpow(ll a, ll e) {
ll ans = 1;
while(e){
if (e&1) ans = mul(ans, a);
a = mul(a,a);
e/=2;
}return ans;
}
const int mxN = 4e6+7;
int fact[mxN], ifact[mxN];
ll ncr(int n, int r){
if (r < 0 or r > n) return 0;
return mul(fact[n], mul(ifact[r],ifact[n-r]));
}
vi check(vector<array<int,2>> a){
int n = sz(a)/2;
vi left(n*2);
auto rec = [&](int st, int i, auto&& rec, bool start) -> int {
if (i == st and !start) {
int two = int(max_element(all(left))-begin(left))%2;
int mx = -1, mxat = -1;
rep(i,0,n*2) {
int cur = (a[i][0] == left[i] ? a[i][1] : a[i][0]);
if (cur > mx) mx = cur, mxat = i;
}
int one = mxat % 2;
return one + two;
}
rep(tk,0,2){
if (i%2) {
// want high
int ans = -1;
left[i] = a[i][0];
ans = max(ans, rec(st,(i+1)%(n*2),rec,0));
left[i] = a[i][1];
ans = max(ans, rec(st,(i+1)%(n*2),rec,0));
return ans;
}else{
// want low
int ans = 3;
left[i] = a[i][0];
ans = min(ans, rec(st,(i+1)%(n*2),rec,0));
left[i] = a[i][1];
ans = min(ans, rec(st,(i+1)%(n*2),rec,0));
return ans;
}
}
};
vi sol(n*2);
rep(i,0,n*2){
sol[i] = rec(i,i,rec,1);
}
return sol;
}
void solve(){
int n; cin >> n;
vi a(n*2);
for(auto& c : a) cin >> c;
if (count(all(a),0)==n*2 or count(all(a),2)==n*2){
ll ans = mul(mul(n*2, n*2-1), fact[n*4-2]);
ans = mul(ans, modpow(2, mod-1-(n*2)));
cout << ans << '\n';
return;
}
// not 2 and 0 at the same time
if (count(all(a),0) and count(all(a),2)){
cout << "0\n";
return;
}
if (count(all(a),1) == n*2){
// must be big block, 4n is self, but 4n-1, 4n-2 are other
ll ans = mul(fact[4*n-3], mul(n*2, mul(n*2, n*2-1)));
ans = mul(ans, modpow(2, mod-1-(n*2)));
ans = mul(ans, 2); // both players can have max
cout << ans << '\n';
return;
}
if (count(all(a),2)) {
for(auto& c : a) if (c==2) c = 0;
rotate(begin(a),begin(a)+1,end(a));
}
// now we have only 0 and 1 case
int st;
rep(i,0,n*2){
if (a[i] == 0 and a[(i+1)%(n*2)] == 1){
if (i%2 == 0){
cout << "0\n";
return;
}else{
st = i;
}
}
}
vi onesizes, zerosizes;
st = (st + 1) % (n*2);
for(int i = st, beg = 0; i != st or !beg; i = (i+1)%(n*2), beg = 1) {
{
int j = i;
while(a[j] == a[i]) j = (j + 1) % (n*2);
int len = j - i;
if (len < 0) len += n*2;
if (len%2 == 0){
cout << "0\n"; // ????????????????
return;
}
if (a[i]) onesizes.push_back(len);
else zerosizes.push_back(len);
i = (j + n*2 - 1);
}
}
ll ans = 0;
ll div = modpow(2, mod-1-n*2);
ll segpow = modpow(2, sz(onesizes)*2);
rep(i,0,sz(onesizes)) {
// block i contains 4n
// no special fx
{
int K = n*4 - sz(onesizes)*2;
int s = zerosizes[i];
// for(int j = 1; j < s-1; j += 2){
// // place big one here
// // K-1 total ways to place next one
// // but j+1 are forbidden <- ACTUALLY, THERES MORE? (s) bad ones? (all to left, and the one free spot where the next segment starts)
// ll cur = (K/2) - 1; //- (s); // (K-1) - (j+1)
// cur = mul(cur, fact[K-2]);
// cur = mul(cur, mul(segpow, div));
// cur = mul(cur, 2); // either top or bottom
// ans = (ans + cur) % mod;
// }
{
ll cur = mul((K/2) - 1, s/2); //- (s); // (K-1) - (j+1)
cur = mul(cur, fact[K-2]);
cur = mul(cur, mul(segpow, div));
cur = mul(cur, 2); // either top or bottom
ans = (ans + cur) % mod;
}
{
// otherwise, not to the left -> must be good player to the right
// there are (K - 2*(s-1))/2 such positions
ll cur = (K - 2*(s-1)) / 2;
cur = mul(cur, fact[K-1]);
cur = mul(cur, mul(segpow, div));
ans = (ans + cur) % mod;
}
// int bigs = K/2;
// int left = K/2 - 1;
// ll cur = mul(bigs, left);
// cur = mul(cur, fact[K-2]);
// cur = mul(cur, mul(segpow, div));
// ans = (ans + cur) % mod;
}
// special fx
{
int s = onesizes[i];
int K = n*4 - sz(onesizes)*2;
ll tri = mul(mul(s-1, s+1), (mod+1)/2);
{
// inside
ll cur = mul(tri, (K-2)/2);
cur = mul(cur, fact[K-3]);
cur = mul(cur, mul(segpow, div));
ans = (ans + cur) % mod;
}
{
// outside
ll cur = mul(s-1, K/2);
cur = (cur - tri + mod) % mod;
cur = mul(cur, fact[K-2]);
cur = mul(cur, mul(segpow, div));
ans = (ans + cur) % mod;
}
}
}
cout << ans << '\n';
}
int main(){
cin.tie(NULL),ios::sync_with_stdio(false);
fact[0] = 1;
for(int i = 1; i < mxN; ++i) fact[i] = mul(i, fact[i-1]);
ifact[mxN-1] = modpow(fact[mxN-1], mod-2);
for(int i = mxN-1; i > 0; --i) ifact[i-1] = mul(i,ifact[i]);
int t; cin >> t;
while(t--) solve();
exit(0);
int n; cin >> n;
vector<array<int,2>> a(n*2);
rep(i,0,n*2) cin >> a[i][0];
rep(i,0,n*2) cin >> a[i][1];
auto ret = check(a);
for(auto c : ret) cout << c << ' ';
cout << '\n';
// int n; cin >> n;
// map<vi,int> cnt;
// vector<array<int,2>> a(n*2);
// auto rec = [&](int i, auto&& rec, int msk = 0) -> void {
// if (i == n*2){
// cnt[check(a)]++;
// // auto x = check(a);
// // if (x == vi{1, 0, 1, 0} and a[0][1] == 5){
// // rep(i,0,n*2) cout << a[i][0] << ' '; cout << '\n';
// // rep(i,0,n*2) cout << a[i][1] << ' '; cout << '\n';
// // cout << '\n';
// // }
// return;
// }
// rep(j,0,(n*4)) if (!(msk&(1<<j))) rep(k,j+1,n*4) if (!(msk&(1<<k))) {
// a[i] = {j,k};
// int nmsk = msk | (1<<j) | (1<<k);
// rec(i+1,rec,nmsk);
// }
// };
// rec(0,rec);
// for(auto& [k,v] : cnt){
// rep(i,0,n*2) cout << k[i] << ' ';
// cout << " " << v << '\n';
// }
}
/*
4
1 2 3 4 8 5 6 7
15 8 6 9 10
1 1 1 0 0 0
real 108360
out 88200
diff 20160 -> 4 * 7!
1 1 1 0 1 0
real 11160
out 10080
diff 1080 -> 9 * 5!
1 1 1 0
real 114
out 90
diff 24 -> 4 * 3!
1 1 1 1 1 0
diff 7 * 7!
3
2 3 4 5 6 7
8 1 12 9 10 11
3
2 1 4 3 6 8
7 5 12 9 10 11
*/
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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | #pragma GCC optimize("O3,unroll-loops,inline") #include "bits/stdc++.h" using namespace std; #define rep(i,a,b) for(int i=(a); i<(b); ++i) #define all(x) x.begin(),x.end() #define sz(x) int(x.size()) typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<vi> vvi; const ll mod = 1e9+7; ll mul(ll a, ll b){ return a*b%mod; }ll modpow(ll a, ll e) { ll ans = 1; while(e){ if (e&1) ans = mul(ans, a); a = mul(a,a); e/=2; }return ans; } const int mxN = 4e6+7; int fact[mxN], ifact[mxN]; ll ncr(int n, int r){ if (r < 0 or r > n) return 0; return mul(fact[n], mul(ifact[r],ifact[n-r])); } vi check(vector<array<int,2>> a){ int n = sz(a)/2; vi left(n*2); auto rec = [&](int st, int i, auto&& rec, bool start) -> int { if (i == st and !start) { int two = int(max_element(all(left))-begin(left))%2; int mx = -1, mxat = -1; rep(i,0,n*2) { int cur = (a[i][0] == left[i] ? a[i][1] : a[i][0]); if (cur > mx) mx = cur, mxat = i; } int one = mxat % 2; return one + two; } rep(tk,0,2){ if (i%2) { // want high int ans = -1; left[i] = a[i][0]; ans = max(ans, rec(st,(i+1)%(n*2),rec,0)); left[i] = a[i][1]; ans = max(ans, rec(st,(i+1)%(n*2),rec,0)); return ans; }else{ // want low int ans = 3; left[i] = a[i][0]; ans = min(ans, rec(st,(i+1)%(n*2),rec,0)); left[i] = a[i][1]; ans = min(ans, rec(st,(i+1)%(n*2),rec,0)); return ans; } } }; vi sol(n*2); rep(i,0,n*2){ sol[i] = rec(i,i,rec,1); } return sol; } void solve(){ int n; cin >> n; vi a(n*2); for(auto& c : a) cin >> c; if (count(all(a),0)==n*2 or count(all(a),2)==n*2){ ll ans = mul(mul(n*2, n*2-1), fact[n*4-2]); ans = mul(ans, modpow(2, mod-1-(n*2))); cout << ans << '\n'; return; } // not 2 and 0 at the same time if (count(all(a),0) and count(all(a),2)){ cout << "0\n"; return; } if (count(all(a),1) == n*2){ // must be big block, 4n is self, but 4n-1, 4n-2 are other ll ans = mul(fact[4*n-3], mul(n*2, mul(n*2, n*2-1))); ans = mul(ans, modpow(2, mod-1-(n*2))); ans = mul(ans, 2); // both players can have max cout << ans << '\n'; return; } if (count(all(a),2)) { for(auto& c : a) if (c==2) c = 0; rotate(begin(a),begin(a)+1,end(a)); } // now we have only 0 and 1 case int st; rep(i,0,n*2){ if (a[i] == 0 and a[(i+1)%(n*2)] == 1){ if (i%2 == 0){ cout << "0\n"; return; }else{ st = i; } } } vi onesizes, zerosizes; st = (st + 1) % (n*2); for(int i = st, beg = 0; i != st or !beg; i = (i+1)%(n*2), beg = 1) { { int j = i; while(a[j] == a[i]) j = (j + 1) % (n*2); int len = j - i; if (len < 0) len += n*2; if (len%2 == 0){ cout << "0\n"; // ???????????????? return; } if (a[i]) onesizes.push_back(len); else zerosizes.push_back(len); i = (j + n*2 - 1); } } ll ans = 0; ll div = modpow(2, mod-1-n*2); ll segpow = modpow(2, sz(onesizes)*2); rep(i,0,sz(onesizes)) { // block i contains 4n // no special fx { int K = n*4 - sz(onesizes)*2; int s = zerosizes[i]; // for(int j = 1; j < s-1; j += 2){ // // place big one here // // K-1 total ways to place next one // // but j+1 are forbidden <- ACTUALLY, THERES MORE? (s) bad ones? (all to left, and the one free spot where the next segment starts) // ll cur = (K/2) - 1; //- (s); // (K-1) - (j+1) // cur = mul(cur, fact[K-2]); // cur = mul(cur, mul(segpow, div)); // cur = mul(cur, 2); // either top or bottom // ans = (ans + cur) % mod; // } { ll cur = mul((K/2) - 1, s/2); //- (s); // (K-1) - (j+1) cur = mul(cur, fact[K-2]); cur = mul(cur, mul(segpow, div)); cur = mul(cur, 2); // either top or bottom ans = (ans + cur) % mod; } { // otherwise, not to the left -> must be good player to the right // there are (K - 2*(s-1))/2 such positions ll cur = (K - 2*(s-1)) / 2; cur = mul(cur, fact[K-1]); cur = mul(cur, mul(segpow, div)); ans = (ans + cur) % mod; } // int bigs = K/2; // int left = K/2 - 1; // ll cur = mul(bigs, left); // cur = mul(cur, fact[K-2]); // cur = mul(cur, mul(segpow, div)); // ans = (ans + cur) % mod; } // special fx { int s = onesizes[i]; int K = n*4 - sz(onesizes)*2; ll tri = mul(mul(s-1, s+1), (mod+1)/2); { // inside ll cur = mul(tri, (K-2)/2); cur = mul(cur, fact[K-3]); cur = mul(cur, mul(segpow, div)); ans = (ans + cur) % mod; } { // outside ll cur = mul(s-1, K/2); cur = (cur - tri + mod) % mod; cur = mul(cur, fact[K-2]); cur = mul(cur, mul(segpow, div)); ans = (ans + cur) % mod; } } } cout << ans << '\n'; } int main(){ cin.tie(NULL),ios::sync_with_stdio(false); fact[0] = 1; for(int i = 1; i < mxN; ++i) fact[i] = mul(i, fact[i-1]); ifact[mxN-1] = modpow(fact[mxN-1], mod-2); for(int i = mxN-1; i > 0; --i) ifact[i-1] = mul(i,ifact[i]); int t; cin >> t; while(t--) solve(); exit(0); int n; cin >> n; vector<array<int,2>> a(n*2); rep(i,0,n*2) cin >> a[i][0]; rep(i,0,n*2) cin >> a[i][1]; auto ret = check(a); for(auto c : ret) cout << c << ' '; cout << '\n'; // int n; cin >> n; // map<vi,int> cnt; // vector<array<int,2>> a(n*2); // auto rec = [&](int i, auto&& rec, int msk = 0) -> void { // if (i == n*2){ // cnt[check(a)]++; // // auto x = check(a); // // if (x == vi{1, 0, 1, 0} and a[0][1] == 5){ // // rep(i,0,n*2) cout << a[i][0] << ' '; cout << '\n'; // // rep(i,0,n*2) cout << a[i][1] << ' '; cout << '\n'; // // cout << '\n'; // // } // return; // } // rep(j,0,(n*4)) if (!(msk&(1<<j))) rep(k,j+1,n*4) if (!(msk&(1<<k))) { // a[i] = {j,k}; // int nmsk = msk | (1<<j) | (1<<k); // rec(i+1,rec,nmsk); // } // }; // rec(0,rec); // for(auto& [k,v] : cnt){ // rep(i,0,n*2) cout << k[i] << ' '; // cout << " " << v << '\n'; // } } /* 4 1 2 3 4 8 5 6 7 15 8 6 9 10 1 1 1 0 0 0 real 108360 out 88200 diff 20160 -> 4 * 7! 1 1 1 0 1 0 real 11160 out 10080 diff 1080 -> 9 * 5! 1 1 1 0 real 114 out 90 diff 24 -> 4 * 3! 1 1 1 1 1 0 diff 7 * 7! 3 2 3 4 5 6 7 8 1 12 9 10 11 3 2 1 4 3 6 8 7 5 12 9 10 11 */ |
English