#include <bits/stdc++.h>
using namespace std;
const int MOD = 1'000'000'007;
int mul(int a, int b) {
return (long long) a * b % MOD;
}
int my_pow(int a, int b) {
int r = 1;
while (b) {
if (b % 2) {
r = mul(r, a);
}
a = mul(a, a);
b /= 2;
}
return r;
}
int my_inv(int a) {
return my_pow(a, MOD - 2);
}
int my_div(int a, int b) {
return mul(a, my_inv(b));
}
void test_case() {
int n;
cin >> n;
vector<int> fac(4 * n + 1);
fac[0] = 1;
for (int i = 1; i < (int) fac.size(); i++) {
fac[i] = mul(i, fac[i-1]);
}
vector<int> a(2 * n);
for (int& x : a) {
cin >> x;
}
int MIN = *min_element(a.begin(), a.end());
int MAX = *max_element(a.begin(), a.end());
const int DIVIDE = my_inv(my_pow(2, 2 * n));
if (MIN == 2 || MAX == 0) {
// This group gets MAX and MAX-1.
int ans = (long long) 2 * n % MOD * (2 * n - 1) % MOD * fac[4*n-2] % MOD * DIVIDE % MOD;
cout << ans << "\n";
return;
}
if (MIN == 1 && MAX == 1) {
// Choose one group. This group takes MAX, the other group takes MAX-1 and MAX-2.
int ans = (long long) 2 * 2 * n * 2 * n % MOD * (2 * n - 1) % MOD * fac[4*n-3] % MOD * DIVIDE % MOD;
cout << ans << "\n";
return;
}
if (MIN == 0 && MAX == 2) {
cout << "0\n";
return;
}
int diffs = 0;
for (int i = 0; i < (int) a.size(); i++) {
if (a[i] != a[(i+1)%(int)a.size()]) {
diffs++;
if ((i % 2 == 0) != (a[i] == MAX)) {
cout << "0\n";
return;
}
}
}
int ans = 0;
int ones = 0;
for (int i = 0; i < (int) a.size() && a[i] == 1; i++) {
ones++;
}
for (int i = (int) a.size() - 1; i >= 0; --i) {
if (a[i] != 1) {
ones = 0;
continue;
}
ones++;
if (ones % 2 == 1) {
// put MAX here
if (ones == 1) {
// standard
int nxt_range = 0;
for (int j = (i + 1) % (int) a.size(); a[j] != 1; j = (j + 1) % (int) a.size()) {
nxt_range++;
}
assert(nxt_range % 2 == 1);
const int TMP = my_pow(2, diffs);
int x = (long long) TMP * (2 * n - diffs / 2 - nxt_range / 2 * 2) % MOD * fac[4*n-diffs-1] % MOD * DIVIDE % MOD;
int y = (long long) TMP * (nxt_range / 2 * 2) % MOD * (2 * n - diffs / 2 - 1) % MOD * fac[4*n-diffs-2] % MOD * DIVIDE % MOD;
ans = (ans + x + 0LL + y) % MOD;
}
else {
// special
const int TMP = my_pow(2, diffs + 1);
int x = (long long) TMP * (2 * n - diffs / 2 - ones / 2 * 2) % MOD * fac[4*n-diffs-2] % MOD * DIVIDE % MOD;
int y = (long long) TMP * (ones / 2 * 2) % MOD * (2 * n - diffs / 2 - 1) % MOD * fac[4*n-diffs-3] % MOD * DIVIDE % MOD;
ans = (ans + x + 0LL + y) % MOD;
}
}
}
cout << ans << "\n";
// int ans = 0;
// int ones = 0;
// for (int i = (int) a.size() - 1; i >= 0 && a[i] == 1; --i) {
// ones++;
// }
// for (int i = 0; i < (int) a.size(); i++) {
// if (a[i] != 1) {
// ones = 0;
// continue;
// }
// ones++;
// }
// int ans = (long long) diffs / 2 * my_pow(2, diffs - 1) % MOD * fac[4*n-diffs] % MOD * DIVIDE % MOD;
// int spec = 0;
// for (int i = 0; i < (int) a.size(); i++) {
// if (a[i] == 1) {
// spec++;
// }
// }
// spec -= diffs / 2;
// assert(spec % 2 == 0);
// spec /= 2;
// int ans2 = (long long) spec * my_pow(2, diffs + 1) % MOD * fac[4*n-(diffs+1)] % MOD * DIVIDE % MOD;
// ans2 = 0;
// cout << (ans+ans2) % MOD << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while (T--) {
test_case();
}
}
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 | #include <bits/stdc++.h> using namespace std; const int MOD = 1'000'000'007; int mul(int a, int b) { return (long long) a * b % MOD; } int my_pow(int a, int b) { int r = 1; while (b) { if (b % 2) { r = mul(r, a); } a = mul(a, a); b /= 2; } return r; } int my_inv(int a) { return my_pow(a, MOD - 2); } int my_div(int a, int b) { return mul(a, my_inv(b)); } void test_case() { int n; cin >> n; vector<int> fac(4 * n + 1); fac[0] = 1; for (int i = 1; i < (int) fac.size(); i++) { fac[i] = mul(i, fac[i-1]); } vector<int> a(2 * n); for (int& x : a) { cin >> x; } int MIN = *min_element(a.begin(), a.end()); int MAX = *max_element(a.begin(), a.end()); const int DIVIDE = my_inv(my_pow(2, 2 * n)); if (MIN == 2 || MAX == 0) { // This group gets MAX and MAX-1. int ans = (long long) 2 * n % MOD * (2 * n - 1) % MOD * fac[4*n-2] % MOD * DIVIDE % MOD; cout << ans << "\n"; return; } if (MIN == 1 && MAX == 1) { // Choose one group. This group takes MAX, the other group takes MAX-1 and MAX-2. int ans = (long long) 2 * 2 * n * 2 * n % MOD * (2 * n - 1) % MOD * fac[4*n-3] % MOD * DIVIDE % MOD; cout << ans << "\n"; return; } if (MIN == 0 && MAX == 2) { cout << "0\n"; return; } int diffs = 0; for (int i = 0; i < (int) a.size(); i++) { if (a[i] != a[(i+1)%(int)a.size()]) { diffs++; if ((i % 2 == 0) != (a[i] == MAX)) { cout << "0\n"; return; } } } int ans = 0; int ones = 0; for (int i = 0; i < (int) a.size() && a[i] == 1; i++) { ones++; } for (int i = (int) a.size() - 1; i >= 0; --i) { if (a[i] != 1) { ones = 0; continue; } ones++; if (ones % 2 == 1) { // put MAX here if (ones == 1) { // standard int nxt_range = 0; for (int j = (i + 1) % (int) a.size(); a[j] != 1; j = (j + 1) % (int) a.size()) { nxt_range++; } assert(nxt_range % 2 == 1); const int TMP = my_pow(2, diffs); int x = (long long) TMP * (2 * n - diffs / 2 - nxt_range / 2 * 2) % MOD * fac[4*n-diffs-1] % MOD * DIVIDE % MOD; int y = (long long) TMP * (nxt_range / 2 * 2) % MOD * (2 * n - diffs / 2 - 1) % MOD * fac[4*n-diffs-2] % MOD * DIVIDE % MOD; ans = (ans + x + 0LL + y) % MOD; } else { // special const int TMP = my_pow(2, diffs + 1); int x = (long long) TMP * (2 * n - diffs / 2 - ones / 2 * 2) % MOD * fac[4*n-diffs-2] % MOD * DIVIDE % MOD; int y = (long long) TMP * (ones / 2 * 2) % MOD * (2 * n - diffs / 2 - 1) % MOD * fac[4*n-diffs-3] % MOD * DIVIDE % MOD; ans = (ans + x + 0LL + y) % MOD; } } } cout << ans << "\n"; // int ans = 0; // int ones = 0; // for (int i = (int) a.size() - 1; i >= 0 && a[i] == 1; --i) { // ones++; // } // for (int i = 0; i < (int) a.size(); i++) { // if (a[i] != 1) { // ones = 0; // continue; // } // ones++; // } // int ans = (long long) diffs / 2 * my_pow(2, diffs - 1) % MOD * fac[4*n-diffs] % MOD * DIVIDE % MOD; // int spec = 0; // for (int i = 0; i < (int) a.size(); i++) { // if (a[i] == 1) { // spec++; // } // } // spec -= diffs / 2; // assert(spec % 2 == 0); // spec /= 2; // int ans2 = (long long) spec * my_pow(2, diffs + 1) % MOD * fac[4*n-(diffs+1)] % MOD * DIVIDE % MOD; // ans2 = 0; // cout << (ans+ans2) % MOD << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int T; cin >> T; while (T--) { test_case(); } } |
English