-
Notifications
You must be signed in to change notification settings - Fork 0
/
Completion (Easy)
214 lines (173 loc) Β· 4.29 KB
/
Completion (Easy)
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
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod=998244353;
const int N=1e6+1;
vector<int>fact(N);
vector<int>tree[200001];
int up[200001][20];
int lvl[200001];
void dfs(int src,int par,int level=0){
lvl[src]=level;
for(auto it:tree[src]){
if(it!=par) dfs(it,src,level+1);
}
}
void binary_lifting(int src,int par){
up[src][0]=par;
for(int i=1;i<20;i++){
if(up[src][i-1]!=-1){
up[src][i]=up[up[src][i-1]][i-1];
}
else up[src][i]=-1;
}
for(auto child:tree[src]){
if(child!=par) binary_lifting(child,src);
}
}
int lift_node(int node,int jump_required){
if(node==-1 || jump_required==0) return node;
for(int i=19;i>=0;i--){
if(node==-1 || jump_required==0) break;
if(jump_required>=(1<<i)){
jump_required-=(1<<i);
node=up[node][i];
}
}
return node;
}
//binary search LCA Approach O(log^2(N))
int LCA(int u,int v){
if(lvl[u]<lvl[v]) swap(u,v);
u=lift_node(u,lvl[u]-lvl[v]);
int lo=0;
int hi=lvl[u];
int ans=-1;
while(hi>=lo){
int mid=(lo+hi)/2;
int x1=lift_node(u,mid);
int x2=lift_node(v,mid);
if(x1==x2){
ans=mid;
hi=mid-1;
}
else lo=mid+1;
}
return lift_node(u,ans);
}
//O(LOG(N)) algorithm
int LCA1(int u,int v){
if(lvl[u]<lvl[v]) swap(u,v);
u=lift_node(u,lvl[u]-lvl[v]);
if(u==v) return u;
for(int i=19;i>=0;i++){
if(up[u][i]!=up[v][i]){
u=up[u][i];
v=up[v][i];
}
}
return lift_node(u,1);
}
class SGTree{
vector<int>seg;
public:
SGTree(int n){
seg.resize(4*n+1);
}
void build(int ind,int low,int high,int arr[]){
if(low==high){
seg[ind]=arr[low];
return;
}
int mid=(low+high)/2;
build(2*ind+1,low,mid,arr);
build(2*ind+2,mid+1,high,arr);
seg[ind]=min(seg[2*ind+1],seg[2*ind+2]);
}
int query(int ind,int low,int high,int l,int r){
//no overlap
// l r low high or low high l r
if(l>high ||r<low) return INT_MAX;
//complete overlap
//l low high r
if(l<=low && high<=r){
return seg[ind];
}
//partial overlap
int mid=(low+high)/2;
int left=query(2*ind+1,low,mid,l,r);
int right=query(2*ind+1,mid+1,high,l,r);
return min(left,right);
}
void update(int ind,int low,int high,int i,int val){
if(low==high){
seg[ind]=val;
return;
}
int mid=(low+high)/2;
if(i<=mid) update(2*ind+1,low,mid,i,val);
else update(2*ind+2,mid+1,high,i,val);
seg[ind]=min(seg[2*ind+1],seg[2*ind+2]);
}
};
long long binpow(long long a, long long b, long long m) {
a %= m;
long long res = 1;
while (b > 0) {
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
long long factorial(long long b) {
long long temp = 1;
for (long long i = 1; i <= b; i++) {
temp = (temp * i) % mod;
}
return temp;
}
void solve(){
int n;
cin >> n;
n =n* 2;
vector<long long> arr(n);
long long ans = 1;
for (int i = 0; i < n; i++) cin >> arr[i];
long long g1 = n / 2, g2 = n / 2;
for (int i = 0; i < n; i++) {
if (arr[i] == 0) continue;
if (arr[i] <= n / 2) g1--;
else g2--;
}
for (int i = 0; i < n; i += 2) {
long long k1 = arr[i];
long long k2 = arr[i + 1];
if (k1 == 0 && k2 == 0) continue;
else {
if (k1 <= n / 2) {
ans = (ans * g2) % mod;
g2--;
} else {
ans = (ans * g1) % mod;
g1--;
}
}
}
long long fact = factorial(g1);
long long pow = binpow(2, g1,mod);
ans = (ans * pow) % mod;
ans = (ans * fact) % mod;
ans = (ans * fact) % mod;
cout << ans << endl;
}
int32_t main(){
int t;
cin>>t;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
while(t--){
solve();
}
}