#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
const int MOD = 1e9 + 7;
const int N = 1e6;
const long long HALF = 500000004;
vector<long long> factorials(4 * N + 1);
vector<long long> powers(2 * N + 2);
void computeFactorials() {
factorials[0] = 1;
for (int i = 1; i <= 4 * N; ++i) {
factorials[i] = (factorials[i - 1] * i) % MOD;
}
}
void computePowersOfHalf() {
powers[0] = 2;
powers[1] = 1;
for (int i = 1; i <= 2 * N; ++i) {
powers[i + 1] = (powers[i] * HALF) % MOD;
}
}
long long mul(long long a, long long b) {
return (a * b) % MOD;
}
void test() {
int n;
cin >> n;
vector<int> a(2 * n);
bool zero = false, one = false, two = false;
for (int i = 0; i < 2 * n; ++i) {
cin >> a[i];
if (a[i] == 0) zero = true;
else if (a[i] == 1) one = true;
else two = true;
if (a[i] > 1) {
a[i] = 0;
}
}
if (zero && two) {
cout << 0 << endl;
return;
}
if (!one) {
cout << mul(mul(mul(n, 2 * n - 1), factorials[4 * n - 2]), powers[2 * n]) << endl;
return;
}
if (!zero && !two) {
cout << mul(mul(mul(mul(n, n), 2 * n - 1), factorials[4 * n - 3]), powers[2 * n - 2]) << endl;
return;
}
int parity = two ? 1 : 0;
int changes = 0;
int blockSize = 1;
int firstBlockSize = 1;
for (int i = 1; i < 2 * n; ++i) {
if (a[i] != a[i - 1]) {
if (changes > 0 && blockSize % 2 == 0) {
cout << 0 << endl;
return;
}
changes++;
blockSize = 1;
} else {
blockSize++;
}
if (changes == 0) {
firstBlockSize++;
}
}
if (a[2 * n - 1] == a[0]) {
if ((firstBlockSize + blockSize) % 2 == 0) {
cout << 0 << endl;
return;
}
} else {
if (blockSize % 2 == 0) {
cout << 0 << endl;
return;
}
changes++;
}
long long result = 0;
int greatestPointer = 0;
int lowestPointer = 0;
bool overflow = false;
while (greatestPointer < 2 * n) {
// cout << "greatestPointer: " << greatestPointer << ", a: " << a[greatestPointer] << endl;
if (a[greatestPointer] == 1 && greatestPointer % 2 == parity) {
if (a[greatestPointer] == a[(greatestPointer - 1 + 2 * n) % (2 * n)] && a[greatestPointer] != a[(greatestPointer - 2 + 2 * n) % (2 * n)]) {
cout << 0 << endl;
return;
}
int cards = a[greatestPointer] != a[(greatestPointer + 1) % (2 * n)] ? changes : changes + 1;
if (!overflow && lowestPointer <= greatestPointer) {
lowestPointer = (greatestPointer + 1) % (2 * n);
}
while (a[lowestPointer] == a[(lowestPointer + 1) % (2 * n)]) {
lowestPointer = (lowestPointer + 1) % (2 * n);
}
int fromLowestToGreatest = (greatestPointer - lowestPointer + 2 * n) % (2 * n);
int fromGreatestToLowest = (lowestPointer - greatestPointer + 2 * n) % (2 * n) - 1;
long long positionsFromLowestToGreatest = (fromLowestToGreatest + 1) / 2; // TODO check if this is correct
long long positionsFromGreatestToLowest = (fromGreatestToLowest + 1) / 2; // TODO check if this is correct
long long ourPositionsFromLowestToGreatest = cards / 2; // TODO check if this is correct
// cout << "changes: " << changes << " cards: " << cards << " parity: " << parity << " greatest: " << greatestPointer << ", lowest: " << lowestPointer << ", a: " << a[greatestPointer] << ", fromLowestToGreatest: " << fromLowestToGreatest << ", fromGreatestToLowest: " << fromGreatestToLowest << " positionsFromLowestToGreatest: " << positionsFromLowestToGreatest << ", positionsFromGreatestToLowest: " << positionsFromGreatestToLowest << " ourPositionsFromLowestToGreatest: " << ourPositionsFromLowestToGreatest << endl;
// cout << "factorial: " << factorials[4 * n - 2 - cards] << " power: " << powers[2 * n - cards + 1] << endl;
// cout << "component1: " << (2 * positionsFromLowestToGreatest - ourPositionsFromLowestToGreatest) * (4 * n - cards - 1) % MOD << endl;
// cout << "component2: " << 4 * positionsFromGreatestToLowest * (n - 1 - ourPositionsFromLowestToGreatest) % MOD << endl;
// cout << "component3: " << 2 * positionsFromGreatestToLowest * (1 + ourPositionsFromLowestToGreatest) % MOD << endl;
result = (result + mul(mul(factorials[4 * n - 2 - cards], powers[2 * n - cards + 1]), ((2 * positionsFromLowestToGreatest - ourPositionsFromLowestToGreatest) * (4 * n - cards - 1) + 4 * positionsFromGreatestToLowest * (n - 1 - ourPositionsFromLowestToGreatest) + 2 * positionsFromGreatestToLowest * (1 + ourPositionsFromLowestToGreatest)) % MOD)) % MOD;
}
greatestPointer++;
}
cout << result << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
computeFactorials();
computePowersOfHalf();
int t;
cin >> t;
for (int i = 0; i < t; ++i) {
test();
}
}
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 | #include <iostream> #include <vector> #include <unordered_map> using namespace std; const int MOD = 1e9 + 7; const int N = 1e6; const long long HALF = 500000004; vector<long long> factorials(4 * N + 1); vector<long long> powers(2 * N + 2); void computeFactorials() { factorials[0] = 1; for (int i = 1; i <= 4 * N; ++i) { factorials[i] = (factorials[i - 1] * i) % MOD; } } void computePowersOfHalf() { powers[0] = 2; powers[1] = 1; for (int i = 1; i <= 2 * N; ++i) { powers[i + 1] = (powers[i] * HALF) % MOD; } } long long mul(long long a, long long b) { return (a * b) % MOD; } void test() { int n; cin >> n; vector<int> a(2 * n); bool zero = false, one = false, two = false; for (int i = 0; i < 2 * n; ++i) { cin >> a[i]; if (a[i] == 0) zero = true; else if (a[i] == 1) one = true; else two = true; if (a[i] > 1) { a[i] = 0; } } if (zero && two) { cout << 0 << endl; return; } if (!one) { cout << mul(mul(mul(n, 2 * n - 1), factorials[4 * n - 2]), powers[2 * n]) << endl; return; } if (!zero && !two) { cout << mul(mul(mul(mul(n, n), 2 * n - 1), factorials[4 * n - 3]), powers[2 * n - 2]) << endl; return; } int parity = two ? 1 : 0; int changes = 0; int blockSize = 1; int firstBlockSize = 1; for (int i = 1; i < 2 * n; ++i) { if (a[i] != a[i - 1]) { if (changes > 0 && blockSize % 2 == 0) { cout << 0 << endl; return; } changes++; blockSize = 1; } else { blockSize++; } if (changes == 0) { firstBlockSize++; } } if (a[2 * n - 1] == a[0]) { if ((firstBlockSize + blockSize) % 2 == 0) { cout << 0 << endl; return; } } else { if (blockSize % 2 == 0) { cout << 0 << endl; return; } changes++; } long long result = 0; int greatestPointer = 0; int lowestPointer = 0; bool overflow = false; while (greatestPointer < 2 * n) { // cout << "greatestPointer: " << greatestPointer << ", a: " << a[greatestPointer] << endl; if (a[greatestPointer] == 1 && greatestPointer % 2 == parity) { if (a[greatestPointer] == a[(greatestPointer - 1 + 2 * n) % (2 * n)] && a[greatestPointer] != a[(greatestPointer - 2 + 2 * n) % (2 * n)]) { cout << 0 << endl; return; } int cards = a[greatestPointer] != a[(greatestPointer + 1) % (2 * n)] ? changes : changes + 1; if (!overflow && lowestPointer <= greatestPointer) { lowestPointer = (greatestPointer + 1) % (2 * n); } while (a[lowestPointer] == a[(lowestPointer + 1) % (2 * n)]) { lowestPointer = (lowestPointer + 1) % (2 * n); } int fromLowestToGreatest = (greatestPointer - lowestPointer + 2 * n) % (2 * n); int fromGreatestToLowest = (lowestPointer - greatestPointer + 2 * n) % (2 * n) - 1; long long positionsFromLowestToGreatest = (fromLowestToGreatest + 1) / 2; // TODO check if this is correct long long positionsFromGreatestToLowest = (fromGreatestToLowest + 1) / 2; // TODO check if this is correct long long ourPositionsFromLowestToGreatest = cards / 2; // TODO check if this is correct // cout << "changes: " << changes << " cards: " << cards << " parity: " << parity << " greatest: " << greatestPointer << ", lowest: " << lowestPointer << ", a: " << a[greatestPointer] << ", fromLowestToGreatest: " << fromLowestToGreatest << ", fromGreatestToLowest: " << fromGreatestToLowest << " positionsFromLowestToGreatest: " << positionsFromLowestToGreatest << ", positionsFromGreatestToLowest: " << positionsFromGreatestToLowest << " ourPositionsFromLowestToGreatest: " << ourPositionsFromLowestToGreatest << endl; // cout << "factorial: " << factorials[4 * n - 2 - cards] << " power: " << powers[2 * n - cards + 1] << endl; // cout << "component1: " << (2 * positionsFromLowestToGreatest - ourPositionsFromLowestToGreatest) * (4 * n - cards - 1) % MOD << endl; // cout << "component2: " << 4 * positionsFromGreatestToLowest * (n - 1 - ourPositionsFromLowestToGreatest) % MOD << endl; // cout << "component3: " << 2 * positionsFromGreatestToLowest * (1 + ourPositionsFromLowestToGreatest) % MOD << endl; result = (result + mul(mul(factorials[4 * n - 2 - cards], powers[2 * n - cards + 1]), ((2 * positionsFromLowestToGreatest - ourPositionsFromLowestToGreatest) * (4 * n - cards - 1) + 4 * positionsFromGreatestToLowest * (n - 1 - ourPositionsFromLowestToGreatest) + 2 * positionsFromGreatestToLowest * (1 + ourPositionsFromLowestToGreatest)) % MOD)) % MOD; } greatestPointer++; } cout << result << endl; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); computeFactorials(); computePowersOfHalf(); int t; cin >> t; for (int i = 0; i < t; ++i) { test(); } } |
English