#include <cstdio>
#include <vector>
using namespace std;
// #define dprint(...) printf(__VA_ARGS__)
#define dprint(...)
const int M = 1000000007;
typedef long long LL;
LL mul(LL a, LL b) {
return (a*b)%M;
}
LL add(LL a, LL b) { return (a+b)%M; }
LL pow(LL a, int k) {
if (k == 0) return 1;
LL b = pow(mul(a, a), k/2);
if (k % 2) return mul(b, a); else return b;
}
LL inv(LL a) { return pow(a, M-2); }
LL div1(LL a, LL b) { return mul(a, inv(b)); }
LL div2(LL a) { return mul(a, (M+1)/2); }
void work() {
int n; scanf("%d", &n);
// kombinacje na karty rozlozone pozostalym tylu osobom
vector<LL> dist(n*2+1);
vector<LL> fact(n*4+1);
dist[0] = 1;
for (int i=1; i<=n*2; ++i) {
dist[i] = mul(dist[i-1], div2(mul(i*2, i*2-1)));
}
fact[0] = 1;
for (int i=1; i<=n*4; ++i) {
fact[i] = mul(fact[i-1], i);
}
vector<int> a(n*2);
vector<int> cnt(3);
for (int i=0; i<n*2; ++i) {
scanf("%d", &a[i]);
cnt[a[i]]++;
}
if (cnt[0] > 0 && cnt[2] > 0) {
printf("0\n");
return;
}
// zawsze jedna strona
if (cnt[1] == 0) {
LL res = 0;
// dwie najwyzsze u roznych osob
if (n > 1) {
LL res2 = mul(n, n-1);
res2 = mul(res2, mul(4*n-2, 4*n-3));
res2 = mul(res2, dist[2*n-2]);
res = add(res, res2);
}
// dwie najwyzsze u jednej osoby
LL res1 = mul(n, dist[2*n-1]);
res = add(res, res1);
printf("%lld\n", res);
return;
}
// same remisy
if (cnt[0] == 0 && cnt[2] == 0) {
LL res = 0;
// drugie najwyzsze u roznych osob
if (n > 1) {
LL res1 = mul(n, mul(n, n-1));
res1 = mul(res1, mul(n*4-3, mul(n*4-4, n*4-5)));
res1 = mul(res1, dist[n*2-3]);
res = add(res, res1);
}
// drugie najwyzsze u jednej osoby
LL res2 = mul(n, mul(n, n*4-3));
res2 = mul(res2, dist[n*2-2]);
res = add(res, res2);
res = mul(res, 2);
printf("%lld\n", res);
return;
}
vector<int> wins, ties;
for (int i=0; i<n*2; ++i) {
if (a[i] == 1) {
if (i == 0 || a[i] != a[i-1]) ties.push_back(1);
else ties.back()++;
} else {
if (i == 0 || a[i] != a[i-1]) wins.push_back(1);
else wins.back()++;
}
}
for (int i=1; i<n*2; ++i) {
if (a[i] != a[i-1]) {
if (a[i] == 1) {
if ((a[i-1] == 2) ^ (i%2 == 1)) {
printf("0\n");
return;
}
}
if (a[i-1] == 1) {
if ((a[i] == 2) ^ (i%2 == 0)) {
printf("0\n");
return;
}
}
}
}
if (wins.size() > ties.size()) {
wins[0] += wins.back();
wins.pop_back();
}
if (ties.size() > wins.size()) {
ties[0] += ties.back();
ties.pop_back();
}
if (a[0] == 1) {
ties.push_back(ties[0]);
ties.erase(ties.begin());
}
/*
printf("Ties: ");
for (int c: ties) printf("%d ", c);
printf("\nWins: ");
for (int c: wins) printf("%d ", c);
printf("\n");
*/
bool bad = false;
for (int c: ties) if (c%2==0) bad = true;
for (int c: wins) if (c%2==0) bad = true;
if (bad) {
printf("0\n");
return;
}
int k = wins.size();
LL res = 0;
for (int i=0; i<k; ++i) {
// krok wstecz
// nie pokrywa sie
if (n-wins[i]/2 > k) {
LL res1 = mul( n-wins[i]/2-k , div1(fact[n*4-k*2-1], fact[n*4-k*4-2]));
res1 = mul(res1, dist[n*2-k*2-1]);
res = add(res, res1);
}
// pokrywa sie
LL res2 = mul(k, div1(fact[n*4-k*2-1], fact[n*4-k*4]));
res2 = mul(res2, dist[n*2-k*2]);
res = add(res, res2);
// dwa takie same
if (wins[i] > 1) {
// nie pokrywa sie
LL res3 = mul( wins[i]/2 , mul( n-k-1, div1(fact[n*4-k*2-2], fact[n*4-k*4-4])));
res3 = mul(res3, dist[n*2-k*2-2]);
res = add(res, res3);
// pokrywa sie
LL res4 = mul( wins[i]/2 , mul( k+1, div1(fact[n*4-k*2-2], fact[n*4-k*4-2])));
res4 = mul(res4, dist[n*2-k*2-1]);
res = add(res, res4);
}
}
dprint("Kuku2\n");
wins.push_back(wins[0]);
wins.erase(wins.begin());
for (int i=0; i<k; ++i) {
if (ties[i] > 1) {
// krok wstecz
dprint("res1\n");
// nie pokrywa sie
LL res1 = mul(n - ties[i]/2 - k, ties[i]/2);
if (ties[i] > 1) {
res1 = add(res1, div2(mul(ties[i]/2, ties[i]/2-1)));
}
res1 = mul(res1, div1(fact[n*4-k*2-2], fact[n*4-k*4-4]));
res1 = mul(res1, dist[n*2-k*2-2]);
res = add(res, res1);
dprint("res2\n");
// pokrywa sie
LL res2 = mul(mul( ties[i]/2, k ), div1(fact[n*4-k*2-2], fact[n*4-k*4-2]));
res2 = mul(res2, dist[n*2-k*2-1]);
res = add(res, res2);
// dwa takie same
if (n*4-k*4-6 >= 0) {
dprint("res3\n");
// nie pokrywa sie
LL res3 = mul( div2(mul(ties[i]/2, (ties[i]+1)/2)), mul(n-k-1, div1(fact[n*4-k*2-3], fact[n*4-k*4-6])));
res3 = mul(res3, dist[n*2-k*2-3]);
res = add(res, res3);
}
dprint("res4\n");
// pokrywa sie
LL res4 = mul( div2(mul(ties[i]/2, (ties[i]+1)/2)), mul( k+1, div1(fact[n*4-k*2-3], fact[n*4-k*4-4])));
res4 = mul(res4, dist[n*2-k*2-2]);
res = add(res, res4);
}
}
printf("%lld\n", res);
}
int main() {
int t; scanf("%d", &t);
while (t--) work();
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 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 | #include <cstdio> #include <vector> using namespace std; // #define dprint(...) printf(__VA_ARGS__) #define dprint(...) const int M = 1000000007; typedef long long LL; LL mul(LL a, LL b) { return (a*b)%M; } LL add(LL a, LL b) { return (a+b)%M; } LL pow(LL a, int k) { if (k == 0) return 1; LL b = pow(mul(a, a), k/2); if (k % 2) return mul(b, a); else return b; } LL inv(LL a) { return pow(a, M-2); } LL div1(LL a, LL b) { return mul(a, inv(b)); } LL div2(LL a) { return mul(a, (M+1)/2); } void work() { int n; scanf("%d", &n); // kombinacje na karty rozlozone pozostalym tylu osobom vector<LL> dist(n*2+1); vector<LL> fact(n*4+1); dist[0] = 1; for (int i=1; i<=n*2; ++i) { dist[i] = mul(dist[i-1], div2(mul(i*2, i*2-1))); } fact[0] = 1; for (int i=1; i<=n*4; ++i) { fact[i] = mul(fact[i-1], i); } vector<int> a(n*2); vector<int> cnt(3); for (int i=0; i<n*2; ++i) { scanf("%d", &a[i]); cnt[a[i]]++; } if (cnt[0] > 0 && cnt[2] > 0) { printf("0\n"); return; } // zawsze jedna strona if (cnt[1] == 0) { LL res = 0; // dwie najwyzsze u roznych osob if (n > 1) { LL res2 = mul(n, n-1); res2 = mul(res2, mul(4*n-2, 4*n-3)); res2 = mul(res2, dist[2*n-2]); res = add(res, res2); } // dwie najwyzsze u jednej osoby LL res1 = mul(n, dist[2*n-1]); res = add(res, res1); printf("%lld\n", res); return; } // same remisy if (cnt[0] == 0 && cnt[2] == 0) { LL res = 0; // drugie najwyzsze u roznych osob if (n > 1) { LL res1 = mul(n, mul(n, n-1)); res1 = mul(res1, mul(n*4-3, mul(n*4-4, n*4-5))); res1 = mul(res1, dist[n*2-3]); res = add(res, res1); } // drugie najwyzsze u jednej osoby LL res2 = mul(n, mul(n, n*4-3)); res2 = mul(res2, dist[n*2-2]); res = add(res, res2); res = mul(res, 2); printf("%lld\n", res); return; } vector<int> wins, ties; for (int i=0; i<n*2; ++i) { if (a[i] == 1) { if (i == 0 || a[i] != a[i-1]) ties.push_back(1); else ties.back()++; } else { if (i == 0 || a[i] != a[i-1]) wins.push_back(1); else wins.back()++; } } for (int i=1; i<n*2; ++i) { if (a[i] != a[i-1]) { if (a[i] == 1) { if ((a[i-1] == 2) ^ (i%2 == 1)) { printf("0\n"); return; } } if (a[i-1] == 1) { if ((a[i] == 2) ^ (i%2 == 0)) { printf("0\n"); return; } } } } if (wins.size() > ties.size()) { wins[0] += wins.back(); wins.pop_back(); } if (ties.size() > wins.size()) { ties[0] += ties.back(); ties.pop_back(); } if (a[0] == 1) { ties.push_back(ties[0]); ties.erase(ties.begin()); } /* printf("Ties: "); for (int c: ties) printf("%d ", c); printf("\nWins: "); for (int c: wins) printf("%d ", c); printf("\n"); */ bool bad = false; for (int c: ties) if (c%2==0) bad = true; for (int c: wins) if (c%2==0) bad = true; if (bad) { printf("0\n"); return; } int k = wins.size(); LL res = 0; for (int i=0; i<k; ++i) { // krok wstecz // nie pokrywa sie if (n-wins[i]/2 > k) { LL res1 = mul( n-wins[i]/2-k , div1(fact[n*4-k*2-1], fact[n*4-k*4-2])); res1 = mul(res1, dist[n*2-k*2-1]); res = add(res, res1); } // pokrywa sie LL res2 = mul(k, div1(fact[n*4-k*2-1], fact[n*4-k*4])); res2 = mul(res2, dist[n*2-k*2]); res = add(res, res2); // dwa takie same if (wins[i] > 1) { // nie pokrywa sie LL res3 = mul( wins[i]/2 , mul( n-k-1, div1(fact[n*4-k*2-2], fact[n*4-k*4-4]))); res3 = mul(res3, dist[n*2-k*2-2]); res = add(res, res3); // pokrywa sie LL res4 = mul( wins[i]/2 , mul( k+1, div1(fact[n*4-k*2-2], fact[n*4-k*4-2]))); res4 = mul(res4, dist[n*2-k*2-1]); res = add(res, res4); } } dprint("Kuku2\n"); wins.push_back(wins[0]); wins.erase(wins.begin()); for (int i=0; i<k; ++i) { if (ties[i] > 1) { // krok wstecz dprint("res1\n"); // nie pokrywa sie LL res1 = mul(n - ties[i]/2 - k, ties[i]/2); if (ties[i] > 1) { res1 = add(res1, div2(mul(ties[i]/2, ties[i]/2-1))); } res1 = mul(res1, div1(fact[n*4-k*2-2], fact[n*4-k*4-4])); res1 = mul(res1, dist[n*2-k*2-2]); res = add(res, res1); dprint("res2\n"); // pokrywa sie LL res2 = mul(mul( ties[i]/2, k ), div1(fact[n*4-k*2-2], fact[n*4-k*4-2])); res2 = mul(res2, dist[n*2-k*2-1]); res = add(res, res2); // dwa takie same if (n*4-k*4-6 >= 0) { dprint("res3\n"); // nie pokrywa sie LL res3 = mul( div2(mul(ties[i]/2, (ties[i]+1)/2)), mul(n-k-1, div1(fact[n*4-k*2-3], fact[n*4-k*4-6]))); res3 = mul(res3, dist[n*2-k*2-3]); res = add(res, res3); } dprint("res4\n"); // pokrywa sie LL res4 = mul( div2(mul(ties[i]/2, (ties[i]+1)/2)), mul( k+1, div1(fact[n*4-k*2-3], fact[n*4-k*4-4]))); res4 = mul(res4, dist[n*2-k*2-2]); res = add(res, res4); } } printf("%lld\n", res); } int main() { int t; scanf("%d", &t); while (t--) work(); return 0; } |
English