#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define st first
#define nd second
ll mod = 1000000007;
ll fact[4000005];
ll n;
ll P;
ll powmod(ll x, ll k) {
ll res = 1;
for (ll y = x; k; k>>=1, y = (y*y)%mod) {
if (k&1) res = (res*y)%mod;
}
return res;
}
ll inv(ll x) {
return powmod(x, mod-2);
}
ll only_twos(int n) {
ll ans = (2*n*(2*n-1))%mod;
ans = (ans * fact[4*n-2])%mod;
ans = (ans * P)%mod;
return ans;
}
ll only_ones(int n) {
ll ans = (2*n*2*n*(2*n-1))%mod;
ans = (ans * fact[4*n-3])%mod;
ans = (ans * P)%mod;
ans = (ans+ans)%mod;
return ans;
}
ll type1(vector<int>& segments) {
int k = segments.size();
ll total = 0;
for (int i = 0; i < segments.size(); i+=2) { // ustalamy segment 1, na końcu będzie A
ll cur = 2*n-((segments[i+1]/2)*2)-(k/2);
cur = (cur * fact[4*n-k-1])%mod;
total += cur;
}
total %= mod;
total = (total * P)%mod;
total = (total*powmod(2, k))%mod;
return total;
}
ll type2(vector<int>& segments) {
int k = segments.size();
ll total = 0;
for (int i = 0; i < segments.size(); i+=2) { // ustalamy segment 1, na końcu będzie A
ll cur = (((segments[i+1]/2)*2) * (2*n-(k/2)-1))%mod;
cur = (cur * fact[4*n-k-2])%mod;
total += cur;
}
total %= mod;
total = (total * P)%mod;
total = (total*powmod(2, k))%mod;
return total;
}
ll type3(vector<int>& segments) {
int k = segments.size();
ll total = 0;
for (int i = 0; i < segments.size(); i+=2) { // ustalamy segment 1, na końcu będzie 10 a w środku A
for (int j = 0; j < segments[i]-1; j+=2) {
ll cur = (2*n-(k/2)-((segments[i]-1-j)/2)*2);
cur = (cur*fact[4*n-k-2])%mod;
total += cur;
}
}
total %= mod;
total = (total * P)%mod;
total = (total*powmod(2, k+1))%mod;
return total;
}
ll type4(vector<int>& segments) {
int k = segments.size();
ll total = 0;
for (int i = 0; i < segments.size(); i+=2) { // ustalamy segment 1, na końcu będzie 10 a w środku A, a pomiędzy A i 10 będzie 9
for (int j = 0; j < segments[i]-1; j+=2) {
ll cur = (((segments[i]-1-j)/2)*2 * (2*n-(k/2)-1))%mod;
cur = (cur*fact[4*n-k-3])%mod;
total += cur;
}
}
total %= mod;
total = (total * P)%mod;
total = (total*powmod(2, k+1))%mod;
return total;
}
void solve() {
cin >> n;
P = inv(powmod(2, 2*n));
vector<int> s(2*n);
bool ex[3] = {false, false, false};
for (int i = 0; i < 2*n; i++) {
cin >> s[i];
// cout << s[i] << '\n';
ex[s[i]] = true;
if (s[i] == 0) s[i] = 2;
}
if (ex[0] && ex[2]) {
cout << "0\n";
return;
}
int ace_pos;
if (ex[0]) ace_pos = 1;
else ace_pos = 0;
ex[2] = (ex[0] || ex[2]);
if (ex[1] == false) {
cout << only_twos(n) << '\n';
return;
}
if (ex[2] == false) {
cout << only_ones(n) << '\n';
return;
}
int start = 0;
while (s[start] == s.back()) {
s.push_back(s[start]);
start++;
}
while (s[start] == 2) {
s.push_back(2);
start++;
}
if (start&1) {
if (ace_pos == 1) {
cout << "0\n";
return;
}
}
else {
if (ace_pos == 0) {
cout << "0\n";
return;
}
}
vector<int> segments;
int len = 1;
for (int i = start+1; i < s.size(); i++) {
if (s[i] != s[i-1]) {
segments.push_back(len);
len=1;
}
else len++;
}
segments.push_back(len);
for (int x: segments) {
if (!(x&1)) {
cout << "0\n";
return;
}
}
ll ans = (type1(segments) + type2(segments) + type3(segments) + type4(segments))%mod;
cout << ans << '\n';
return;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1;
fact[0] = 1;
for (ll i = 1; i <= 4000000; i++) fact[i] = (fact[i-1]*i)%mod;
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 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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define st first #define nd second ll mod = 1000000007; ll fact[4000005]; ll n; ll P; ll powmod(ll x, ll k) { ll res = 1; for (ll y = x; k; k>>=1, y = (y*y)%mod) { if (k&1) res = (res*y)%mod; } return res; } ll inv(ll x) { return powmod(x, mod-2); } ll only_twos(int n) { ll ans = (2*n*(2*n-1))%mod; ans = (ans * fact[4*n-2])%mod; ans = (ans * P)%mod; return ans; } ll only_ones(int n) { ll ans = (2*n*2*n*(2*n-1))%mod; ans = (ans * fact[4*n-3])%mod; ans = (ans * P)%mod; ans = (ans+ans)%mod; return ans; } ll type1(vector<int>& segments) { int k = segments.size(); ll total = 0; for (int i = 0; i < segments.size(); i+=2) { // ustalamy segment 1, na końcu będzie A ll cur = 2*n-((segments[i+1]/2)*2)-(k/2); cur = (cur * fact[4*n-k-1])%mod; total += cur; } total %= mod; total = (total * P)%mod; total = (total*powmod(2, k))%mod; return total; } ll type2(vector<int>& segments) { int k = segments.size(); ll total = 0; for (int i = 0; i < segments.size(); i+=2) { // ustalamy segment 1, na końcu będzie A ll cur = (((segments[i+1]/2)*2) * (2*n-(k/2)-1))%mod; cur = (cur * fact[4*n-k-2])%mod; total += cur; } total %= mod; total = (total * P)%mod; total = (total*powmod(2, k))%mod; return total; } ll type3(vector<int>& segments) { int k = segments.size(); ll total = 0; for (int i = 0; i < segments.size(); i+=2) { // ustalamy segment 1, na końcu będzie 10 a w środku A for (int j = 0; j < segments[i]-1; j+=2) { ll cur = (2*n-(k/2)-((segments[i]-1-j)/2)*2); cur = (cur*fact[4*n-k-2])%mod; total += cur; } } total %= mod; total = (total * P)%mod; total = (total*powmod(2, k+1))%mod; return total; } ll type4(vector<int>& segments) { int k = segments.size(); ll total = 0; for (int i = 0; i < segments.size(); i+=2) { // ustalamy segment 1, na końcu będzie 10 a w środku A, a pomiędzy A i 10 będzie 9 for (int j = 0; j < segments[i]-1; j+=2) { ll cur = (((segments[i]-1-j)/2)*2 * (2*n-(k/2)-1))%mod; cur = (cur*fact[4*n-k-3])%mod; total += cur; } } total %= mod; total = (total * P)%mod; total = (total*powmod(2, k+1))%mod; return total; } void solve() { cin >> n; P = inv(powmod(2, 2*n)); vector<int> s(2*n); bool ex[3] = {false, false, false}; for (int i = 0; i < 2*n; i++) { cin >> s[i]; // cout << s[i] << '\n'; ex[s[i]] = true; if (s[i] == 0) s[i] = 2; } if (ex[0] && ex[2]) { cout << "0\n"; return; } int ace_pos; if (ex[0]) ace_pos = 1; else ace_pos = 0; ex[2] = (ex[0] || ex[2]); if (ex[1] == false) { cout << only_twos(n) << '\n'; return; } if (ex[2] == false) { cout << only_ones(n) << '\n'; return; } int start = 0; while (s[start] == s.back()) { s.push_back(s[start]); start++; } while (s[start] == 2) { s.push_back(2); start++; } if (start&1) { if (ace_pos == 1) { cout << "0\n"; return; } } else { if (ace_pos == 0) { cout << "0\n"; return; } } vector<int> segments; int len = 1; for (int i = start+1; i < s.size(); i++) { if (s[i] != s[i-1]) { segments.push_back(len); len=1; } else len++; } segments.push_back(len); for (int x: segments) { if (!(x&1)) { cout << "0\n"; return; } } ll ans = (type1(segments) + type2(segments) + type3(segments) + type4(segments))%mod; cout << ans << '\n'; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t = 1; fact[0] = 1; for (ll i = 1; i <= 4000000; i++) fact[i] = (fact[i-1]*i)%mod; cin >> t; while (t--) { solve(); } } |
English