#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 1e9+7;
int mul(int a, int b){
return (a*b)%mod;
}
void add(int &a, int b) {
a += b;
if (a >= mod) a-=mod;
}
int add2(int a, int b){
a += b;
if (a >= mod) a-=mod;
return a;
}
void sub(int &a, int b){
a -= b;
if (a < 0) a += mod;
}
int power(int a, int b){
if (!b) return 1ll;
int k = power(a, b/2);
k = mul(k, k);
if (b&1) k = mul(k, a);
return k;
}
void solve() {
int n; cin >> n;
vector<int>a(2 * n);
vector<int>f(4 * n + 1, 1), inv(4 * n + 1, 1);
for (int i = 1; i < (int)f.size(); i++) {
f[i] = mul(f[i - 1], i);
inv[i] = power(f[i], mod - 2);
}
auto nck = [&](int N, int K){
if (N < K || N < 0 || K < 0) return 0ll;
return mul(f[N], mul(inv[K], inv[N - K]));
};
vector<int>cnt(3);
for (auto &x: a) {
cin >> x;
cnt[x]++;
}
if (cnt[0] > 0 && cnt[2] > 0){
cout << "0\n";
return;
}
if (cnt[2] > 0){
for (auto &x: a) x = 2 - x;
rotate(a.begin(), a.begin() + 1, a.end());
cnt[0] = cnt[2];
cnt[2] = 0;
}
vector<int>pot(2 * n + 1, 1);
for (int i = 1; i < (int)pot.size(); i++) {
pot[i] = mul(pot[i - 1], 2);
}
if (cnt[0] == 2 * n) {
cout << mul(mul(2 * n, 2 * n - 1), mul(power(pot[2 * n], mod - 2), f[4 * n - 2])) << "\n";
return;
}
if (cnt[1] == 2 * n) {
cout << mul(mul(mul(n, n), f[4 * n - 2]), power(pot[2 * n - 2], mod - 2)) << "\n";
return;
};
auto nxt = [&](int i){
return i == 2 * n - 1 ? 0 : i + 1;
};
int L = 0;
while (a[L + 1] == a[0]) L++;
int R = 2 * n - 1;
while (a[R - 1] == a.back()) R--;
if (a[0] == a.back() && (L + R) % 2 == 1) {
cout << "0\n";
return;
}
if (a[0] != a.back() && (L % 2 == 1 || R % 2 == 0)) {
cout << "0\n";
return;
}
int ile = 1;
for (int i = L + 2; i < R; i++) {
if (a[i] == a[i - 1]) {
ile++;
} else if (i > L + 1) {
if (ile % 2 == 0){
cout << "0\n";
return;
}
ile = 1;
}
}
int zmiana = 0;
// for (auto x: a) cout << x << " ";
// cout << endl;
int ans = 0;
vector<int>nast(2 * n, -1);
for (int i = 0; i < n; i++) {
if (a[i] != a[nxt(i)]) {
zmiana++;
nast[i] = i;
}
}
if (zmiana % 2 == 1) {
cout << "0\n";
return;
}
for (int rep = 0; rep < 2; rep++) {
for (int i = 2 * n - 1; i >= 0; i--) {
if (nast[i] == -1){
nast[i] = nast[nxt(i)];
}
}
}
auto dlugosc = [&](int i, int j){
if (i <= j) return j - i + 1;
return j + 2 * n - i + 1;
};
cout << "1\n";
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t; cin >> t;
while (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 | #include <bits/stdc++.h> using namespace std; #define int long long const int mod = 1e9+7; int mul(int a, int b){ return (a*b)%mod; } void add(int &a, int b) { a += b; if (a >= mod) a-=mod; } int add2(int a, int b){ a += b; if (a >= mod) a-=mod; return a; } void sub(int &a, int b){ a -= b; if (a < 0) a += mod; } int power(int a, int b){ if (!b) return 1ll; int k = power(a, b/2); k = mul(k, k); if (b&1) k = mul(k, a); return k; } void solve() { int n; cin >> n; vector<int>a(2 * n); vector<int>f(4 * n + 1, 1), inv(4 * n + 1, 1); for (int i = 1; i < (int)f.size(); i++) { f[i] = mul(f[i - 1], i); inv[i] = power(f[i], mod - 2); } auto nck = [&](int N, int K){ if (N < K || N < 0 || K < 0) return 0ll; return mul(f[N], mul(inv[K], inv[N - K])); }; vector<int>cnt(3); for (auto &x: a) { cin >> x; cnt[x]++; } if (cnt[0] > 0 && cnt[2] > 0){ cout << "0\n"; return; } if (cnt[2] > 0){ for (auto &x: a) x = 2 - x; rotate(a.begin(), a.begin() + 1, a.end()); cnt[0] = cnt[2]; cnt[2] = 0; } vector<int>pot(2 * n + 1, 1); for (int i = 1; i < (int)pot.size(); i++) { pot[i] = mul(pot[i - 1], 2); } if (cnt[0] == 2 * n) { cout << mul(mul(2 * n, 2 * n - 1), mul(power(pot[2 * n], mod - 2), f[4 * n - 2])) << "\n"; return; } if (cnt[1] == 2 * n) { cout << mul(mul(mul(n, n), f[4 * n - 2]), power(pot[2 * n - 2], mod - 2)) << "\n"; return; }; auto nxt = [&](int i){ return i == 2 * n - 1 ? 0 : i + 1; }; int L = 0; while (a[L + 1] == a[0]) L++; int R = 2 * n - 1; while (a[R - 1] == a.back()) R--; if (a[0] == a.back() && (L + R) % 2 == 1) { cout << "0\n"; return; } if (a[0] != a.back() && (L % 2 == 1 || R % 2 == 0)) { cout << "0\n"; return; } int ile = 1; for (int i = L + 2; i < R; i++) { if (a[i] == a[i - 1]) { ile++; } else if (i > L + 1) { if (ile % 2 == 0){ cout << "0\n"; return; } ile = 1; } } int zmiana = 0; // for (auto x: a) cout << x << " "; // cout << endl; int ans = 0; vector<int>nast(2 * n, -1); for (int i = 0; i < n; i++) { if (a[i] != a[nxt(i)]) { zmiana++; nast[i] = i; } } if (zmiana % 2 == 1) { cout << "0\n"; return; } for (int rep = 0; rep < 2; rep++) { for (int i = 2 * n - 1; i >= 0; i--) { if (nast[i] == -1){ nast[i] = nast[nxt(i)]; } } } auto dlugosc = [&](int i, int j){ if (i <= j) return j - i + 1; return j + 2 * n - i + 1; }; cout << "1\n"; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { solve(); } } |
English