#include <bits/stdc++.h>
#define e using u=ostream;template<class a,class b>u&operator<<(u&o,pair<a,b>&x)
using namespace std;e;u&operator<<(u&o,string&s){return o<<s.c_str();}template<
class t>auto operator<<(u&o,t&x)->decltype(x.end(),o){o<<'{';int i=2;for(auto y:
x)o<<", "+i<<y,i=0;return o<<'}';}e{return o<<'('<<x.first<<", "<<x.second<<')';}
#ifdef DEBUG
#define LOG(x...)cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x)
#else
#define LOG(...)
#endif
#define ff first
#define ss second
#define ll long long
const ll maxn = 5000000;
const ll mod = 1000000007;
vector <ll> fact(maxn);
ll fastpow(ll a, ll b) {
ll res = 1;
a %= mod;
while (b) {
if (b & 1) res = res * a % mod;
a *= a;
a %= mod;
b >>= 1;
}
return res;
}
void makefact() {
fact[0] = 1;
for (ll i = 1; i < maxn; i++) {
fact[i] = fact[i-1] * i;
fact[i] %= mod;
}
}
ll count(ll n) {
ll numerator = 1LL * n % mod * (2LL*n - 1) % mod * fact[4LL*n - 2] % mod;
ll denom = fastpow(2, 2LL*n - 1);
ll odwr = fastpow(denom, mod - 2);
return numerator * odwr % mod;
}
ll tylko1(ll n) {
ll numerator = n * fact[4LL*n] % mod;
ll denom = (4*n-1) * fastpow(2, 2*n) % mod;
ll odwr = fastpow(denom, mod - 2);
return numerator * odwr % mod;
}
map <vector <int>, int> table2 = {
{{0, 0, 0, 0}, 540},
{{0, 0, 1, 0}, 54},
{{1, 0, 0, 0}, 54},
{{1, 0, 1, 0}, 24},
{{1, 0, 1, 1}, 114},
{{1, 1, 1, 0}, 114},
{{1, 1, 1, 1}, 720},
{{1, 1, 2, 1}, 114},
{{2, 1, 1, 1}, 114},
{{2, 1, 2, 1}, 24},
{{2, 1, 2, 2}, 54},
{{2, 2, 2, 1}, 54},
{{2, 2, 2, 2}, 540},
};
map <vector <int>, int> table3 = {
{{0, 0, 0, 0, 0, 0}, 1701000},
{{0, 0, 0, 0, 1, 0}, 63000},
{{0, 0, 1, 0, 0, 0}, 63000},
{{0, 0, 1, 0, 1, 0}, 8640},
{{0, 0, 1, 1, 1, 0}, 108360},
{{1, 0, 0, 0, 0, 0}, 63000},
{{1, 0, 0, 0, 1, 0}, 8640},
{{1, 0, 0, 0, 1, 1}, 108360},
{{1, 0, 1, 0, 0, 0}, 8640},
{{1, 0, 1, 0, 1, 0}, 1080},
{{1, 0, 1, 0, 1, 1}, 11160},
{{1, 0, 1, 1, 1, 0}, 11160},
{{1, 0, 1, 1, 1, 1}, 148680},
{{1, 1, 1, 0, 0, 0}, 108360},
{{1, 1, 1, 0, 1, 0}, 11160},
{{1, 1, 1, 0, 1, 1}, 148680},
{{1, 1, 1, 1, 1, 0}, 148680},
{{1, 1, 1, 1, 1, 1}, 2041200},
{{1, 1, 1, 1, 2, 1}, 148680},
{{1, 1, 2, 1, 1, 1}, 148680},
{{1, 1, 2, 1, 2, 1}, 11160},
{{1, 1, 2, 2, 2, 1}, 108360},
{{2, 1, 1, 1, 1, 1}, 148680},
{{2, 1, 1, 1, 2, 1}, 11160},
{{2, 1, 1, 1, 2, 2}, 108360},
{{2, 1, 2, 1, 1, 1}, 11160},
{{2, 1, 2, 1, 2, 1}, 1080},
{{2, 1, 2, 1, 2, 2}, 8640},
{{2, 1, 2, 2, 2, 1}, 8640},
{{2, 1, 2, 2, 2, 2}, 63000},
{{2, 2, 2, 1, 1, 1}, 108360},
{{2, 2, 2, 1, 2, 1}, 8640},
{{2, 2, 2, 1, 2, 2}, 63000},
{{2, 2, 2, 2, 2, 1}, 63000},
{{2, 2, 2, 2, 2, 2}, 1701000},
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
makefact();
cin >> t;
while (t--) {
int n;
cin >> n;
vector <int> cnt(3);
vector <int> v(2*n);
for (int i = 0; i < 2*n; i++) {
cin >> v[i];
cnt[v[i]]++;
}
if (cnt[0] && cnt[2]) {
cout << "0\n";
continue;
}
if (cnt[1] == 0) {
cout << count(n) << "\n";
continue;
}
if (n == 1) {
if (cnt[0] == 2 || cnt[2] == 2) {
cout << "1\n";
continue;
}
if (cnt[1] == 2) {
cout << "4\n";
continue;
}
cout << "0\n";
continue;
}
if (n == 2) {
if (table2.find(v) == table2.end()) {
cout << "0\n";
}
else {
cout << table2[v] << "\n";
}
continue;
}
if (n == 3) {
if (table3.find(v) == table3.end()) {
cout << "0\n";
}
else {
cout << table3[v] << "\n";
}
continue;
}
if (cnt[0] == 0 && cnt[2] == 0) {
cout << tylko1(n) << "\n";
continue;
}
cout << "0\n";
}
}
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 | #include <bits/stdc++.h> #define e using u=ostream;template<class a,class b>u&operator<<(u&o,pair<a,b>&x) using namespace std;e;u&operator<<(u&o,string&s){return o<<s.c_str();}template< class t>auto operator<<(u&o,t&x)->decltype(x.end(),o){o<<'{';int i=2;for(auto y: x)o<<", "+i<<y,i=0;return o<<'}';}e{return o<<'('<<x.first<<", "<<x.second<<')';} #ifdef DEBUG #define LOG(x...)cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x) #else #define LOG(...) #endif #define ff first #define ss second #define ll long long const ll maxn = 5000000; const ll mod = 1000000007; vector <ll> fact(maxn); ll fastpow(ll a, ll b) { ll res = 1; a %= mod; while (b) { if (b & 1) res = res * a % mod; a *= a; a %= mod; b >>= 1; } return res; } void makefact() { fact[0] = 1; for (ll i = 1; i < maxn; i++) { fact[i] = fact[i-1] * i; fact[i] %= mod; } } ll count(ll n) { ll numerator = 1LL * n % mod * (2LL*n - 1) % mod * fact[4LL*n - 2] % mod; ll denom = fastpow(2, 2LL*n - 1); ll odwr = fastpow(denom, mod - 2); return numerator * odwr % mod; } ll tylko1(ll n) { ll numerator = n * fact[4LL*n] % mod; ll denom = (4*n-1) * fastpow(2, 2*n) % mod; ll odwr = fastpow(denom, mod - 2); return numerator * odwr % mod; } map <vector <int>, int> table2 = { {{0, 0, 0, 0}, 540}, {{0, 0, 1, 0}, 54}, {{1, 0, 0, 0}, 54}, {{1, 0, 1, 0}, 24}, {{1, 0, 1, 1}, 114}, {{1, 1, 1, 0}, 114}, {{1, 1, 1, 1}, 720}, {{1, 1, 2, 1}, 114}, {{2, 1, 1, 1}, 114}, {{2, 1, 2, 1}, 24}, {{2, 1, 2, 2}, 54}, {{2, 2, 2, 1}, 54}, {{2, 2, 2, 2}, 540}, }; map <vector <int>, int> table3 = { {{0, 0, 0, 0, 0, 0}, 1701000}, {{0, 0, 0, 0, 1, 0}, 63000}, {{0, 0, 1, 0, 0, 0}, 63000}, {{0, 0, 1, 0, 1, 0}, 8640}, {{0, 0, 1, 1, 1, 0}, 108360}, {{1, 0, 0, 0, 0, 0}, 63000}, {{1, 0, 0, 0, 1, 0}, 8640}, {{1, 0, 0, 0, 1, 1}, 108360}, {{1, 0, 1, 0, 0, 0}, 8640}, {{1, 0, 1, 0, 1, 0}, 1080}, {{1, 0, 1, 0, 1, 1}, 11160}, {{1, 0, 1, 1, 1, 0}, 11160}, {{1, 0, 1, 1, 1, 1}, 148680}, {{1, 1, 1, 0, 0, 0}, 108360}, {{1, 1, 1, 0, 1, 0}, 11160}, {{1, 1, 1, 0, 1, 1}, 148680}, {{1, 1, 1, 1, 1, 0}, 148680}, {{1, 1, 1, 1, 1, 1}, 2041200}, {{1, 1, 1, 1, 2, 1}, 148680}, {{1, 1, 2, 1, 1, 1}, 148680}, {{1, 1, 2, 1, 2, 1}, 11160}, {{1, 1, 2, 2, 2, 1}, 108360}, {{2, 1, 1, 1, 1, 1}, 148680}, {{2, 1, 1, 1, 2, 1}, 11160}, {{2, 1, 1, 1, 2, 2}, 108360}, {{2, 1, 2, 1, 1, 1}, 11160}, {{2, 1, 2, 1, 2, 1}, 1080}, {{2, 1, 2, 1, 2, 2}, 8640}, {{2, 1, 2, 2, 2, 1}, 8640}, {{2, 1, 2, 2, 2, 2}, 63000}, {{2, 2, 2, 1, 1, 1}, 108360}, {{2, 2, 2, 1, 2, 1}, 8640}, {{2, 2, 2, 1, 2, 2}, 63000}, {{2, 2, 2, 2, 2, 1}, 63000}, {{2, 2, 2, 2, 2, 2}, 1701000}, }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; makefact(); cin >> t; while (t--) { int n; cin >> n; vector <int> cnt(3); vector <int> v(2*n); for (int i = 0; i < 2*n; i++) { cin >> v[i]; cnt[v[i]]++; } if (cnt[0] && cnt[2]) { cout << "0\n"; continue; } if (cnt[1] == 0) { cout << count(n) << "\n"; continue; } if (n == 1) { if (cnt[0] == 2 || cnt[2] == 2) { cout << "1\n"; continue; } if (cnt[1] == 2) { cout << "4\n"; continue; } cout << "0\n"; continue; } if (n == 2) { if (table2.find(v) == table2.end()) { cout << "0\n"; } else { cout << table2[v] << "\n"; } continue; } if (n == 3) { if (table3.find(v) == table3.end()) { cout << "0\n"; } else { cout << table3[v] << "\n"; } continue; } if (cnt[0] == 0 && cnt[2] == 0) { cout << tylko1(n) << "\n"; continue; } cout << "0\n"; } } |
English