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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include <bits/stdc++.h>
using namespace std;
#define fwd(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n)    fwd(i, 0, n)
#define all(X)       X.begin(), X.end()
#define sz(X)        int(size(X))
#define pb           push_back
#define eb           emplace_back
#define st           first
#define nd           second
using pii = pair<int, int>;
using vi = vector<int>;
using ll = long long;
using ld = long double;
#ifdef LOC
auto SS = signal(6, [](int) {
	*(int *)0 = 0;
});
#	define DTP(x, y)                                      \
		auto operator<<(auto &o, auto a)->decltype(y, o) { \
			o << "(";                                      \
			x;                                             \
			return o << ")";                               \
		}
DTP(o << a.st << ", " << a.nd, a.nd);
DTP(for (auto i : a) o << i << ", ", all(a));
void dump(auto... x) {
	((cerr << x << ", "), ...) << '\n';
}
#	define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x)
#else
#	define deb(...) 0
#endif

const int MOD = 1e9 + 7;

vector<ll> fact, inv_fact, inv2;

ll mod_pow(ll a, ll b) {
	ll res = 1;
	while (b) {
		if (b & 1)
			res = (res * a) % MOD;
		a = (a * a) % MOD;
		b /= 2;
	}
	return res;
}

ll mod_inv(ll a) {
	return mod_pow(a, MOD - 2);
}

ll nChooseK(ll n, ll k) {
	if (k > n || k < 0)
		return 0;
	return (((1LL * fact[n] * inv_fact[k]) % MOD) * inv_fact[n - k]) % MOD;
}

ll thingsIntoNOrderedPairs(ll n) {
	return (1LL * fact[2 * n] * inv2[n]) % MOD;
}

void prep_combinatorial() {
	const ll max_fact = 1e7;
	fact.resize(max_fact + 1);
	inv_fact.resize(max_fact + 1);
	inv2.resize(max_fact + 1);
	fact[0] = 1;
	for (int i = 1; i <= max_fact; i++) {
		fact[i] = (1LL * fact[i - 1] * i) % MOD;
	}
	inv_fact[max_fact] = mod_inv(fact[max_fact]);
	inv2[max_fact] = mod_inv(mod_pow(2, max_fact));
	for (int i = max_fact - 1; i >= 0; i--) {
		inv_fact[i] = (1LL * inv_fact[i + 1] * (i + 1)) % MOD;
		inv2[i] = (2 * inv2[i + 1]) % MOD;
	}
}

// implies that Max1 \in Even, Max2, Max3 \in Odd or other way around
ll case_all_ones(int n) {
	// assume Max1 \in Even and multiply by 2 at the end due to symmetry
	ll ways_to_split_small_cards = nChooseK(4 * n - 3, 2 * n - 2);
	ll ways_to_place_cards_in_team = thingsIntoNOrderedPairs(n);
	return (((2 * ways_to_split_small_cards * ways_to_place_cards_in_team) %
			 MOD) *
			ways_to_place_cards_in_team) %
		   MOD;
}

// implies that Max1, Max2 \in Even
ll case_all_zeros(int n) {
	ll ways_to_split_small_cards = nChooseK(4 * n - 2, 2 * n - 2);
	ll ways_to_place_cards_in_team = thingsIntoNOrderedPairs(n);
	return (((ways_to_split_small_cards * ways_to_place_cards_in_team) % MOD) *
			ways_to_place_cards_in_team) %
		   MOD;
}

// Segment that is loss iff ends with odd, else win if with even (win for odds)
struct NormalSegment {
	int even_count;
	int odd_count;
	bool is_loss; // so ends with odd
};

struct TotSegmentInfo {
	ll loss_segments;
	ll win_segments;
	ll n;
};

TotSegmentInfo get_tot_segment_info(vector<NormalSegment> segments) {
	TotSegmentInfo info = {0, 0, 0};
	for (auto seg : segments) {
		info.n += seg.even_count + seg.odd_count;
		if (seg.is_loss) {
			info.loss_segments++;
		} else {
			info.win_segments++;
		}
	}
	info.n /= 2;
	return info;
}

ll calc_loss_win(vector<NormalSegment> segments, TotSegmentInfo info) {
	// most general case, nothing gets compressed
	ll combinations = 0;
	for (int last_segment_idx = 0; last_segment_idx < sz(segments);
		 last_segment_idx++) {
		NormalSegment last_segment = segments[last_segment_idx];
		if (!last_segment.is_loss) {
			continue;
		}
		ll even_count_in_rest = info.n - last_segment.even_count;
		ll partially_filled_even_count_in_rest = info.win_segments;
		ll fully_empty_even_count_in_rest =
			even_count_in_rest - partially_filled_even_count_in_rest;
		ll places_for_even_cards_in_rest = fully_empty_even_count_in_rest * 2 +
										   partially_filled_even_count_in_rest;
		ll small_cards = 4 * info.n - int(segments.size()) - 1;
		ll ways_to_split_small_cards =
			nChooseK(small_cards, places_for_even_cards_in_rest - 1);
		ll ways_to_place_even_cards_in_rest =
			(fact[places_for_even_cards_in_rest] *
			 inv2[fully_empty_even_count_in_rest]) %
			MOD;
		ll partially_filled_count_in_tot = info.loss_segments;
		ll full_empty_count_in_tot =
			2 * info.n - even_count_in_rest - partially_filled_count_in_tot;
		ll places_for_remaining_cards_in_tot =
			full_empty_count_in_tot * 2 + partially_filled_count_in_tot;
		ll ways_to_place_remaining_cards_in_tot =
			(fact[places_for_remaining_cards_in_tot] *
			 inv2[full_empty_count_in_tot]) %
			MOD;
		ll ways_to_place_all_cards =
			(((ways_to_split_small_cards * ways_to_place_even_cards_in_rest) %
			  MOD) *
			 ways_to_place_remaining_cards_in_tot) %
			MOD;
		combinations = (combinations + ways_to_place_all_cards) % MOD;
	}
	return combinations;
}

ll calc_win_win(vector<NormalSegment> segments, TotSegmentInfo info) {
	ll combinations = 0;
	for (int last_segment_idx = 0; last_segment_idx < sz(segments);
		 last_segment_idx++) {
		NormalSegment last_segment = segments[last_segment_idx];
		if (last_segment.is_loss) {
			continue;
		}
		for (ll position_from_segment_end = 0;
			 position_from_segment_end <
			 last_segment.even_count + last_segment.odd_count;
			 position_from_segment_end++) {
			if (position_from_segment_end % 2 == 0) {
				continue;
			}
			ll even_count_before_position = position_from_segment_end / 2 + 1;
			ll even_count_after_position =
				last_segment.even_count - even_count_before_position;
			if (even_count_after_position == 0) {
				continue;
			}
			ll even_partially_filled_count = info.win_segments + 1;
			ll even_fully_empty_count = info.n - even_partially_filled_count;
			ll places_for_even_cards =
				even_partially_filled_count + 2 * even_fully_empty_count;
			ll small_cards = 4 * info.n - int(segments.size()) - 3;
			ll ways_to_split_small_cards =
				nChooseK(small_cards, places_for_even_cards);
			ll ways_to_place_even_cards =
				(fact[places_for_even_cards] * inv2[even_fully_empty_count]) %
				MOD;
			ll partially_filled_count_in_tot = info.loss_segments + 1;
			ll full_empty_count_in_tot = info.n - partially_filled_count_in_tot;
			ll places_for_remaining_cards_in_tot =
				full_empty_count_in_tot * 2 + partially_filled_count_in_tot;
			ll ways_to_place_remaining_cards_in_tot =
				(fact[places_for_remaining_cards_in_tot] *
				 inv2[full_empty_count_in_tot]) %
				MOD;
			ll ways_to_place_all_cards =
				(((((ways_to_split_small_cards * ways_to_place_even_cards) %
					MOD) *
				   ways_to_place_remaining_cards_in_tot) %
				  MOD) *
				 even_count_after_position) %
				MOD;
			combinations = (combinations + ways_to_place_all_cards) % MOD;
		}
	}
	return combinations;
}

ll calc_loss_loss(vector<NormalSegment> segments, TotSegmentInfo info) {
	ll combinations = 0;
	for (int last_segment_idx = 0; last_segment_idx < sz(segments);
		 last_segment_idx++) {
		NormalSegment last_segment = segments[last_segment_idx];
		if (!last_segment.is_loss) {
			continue;
		}
		if (last_segment.even_count == 0) {
			continue;
		}
		ll ways_to_place_even_in_left = last_segment.even_count;
		ll partial_even_count = info.win_segments + 1;
		ll empty_even_count = info.n - partial_even_count;
		ll places_for_even_cards = 2 * empty_even_count + partial_even_count;
		ll small_cards = 4 * info.n - int(segments.size()) - 2;
		ll ways_to_split_small_cards =
			nChooseK(small_cards, places_for_even_cards - 1);
		ll ways_to_place_even_cards =
			(fact[places_for_even_cards] * inv2[empty_even_count]) % MOD;
		ll partial_count_in_tot = info.loss_segments;
		ll full_empty_count_in_tot = info.n - partial_count_in_tot;
		ll places_for_remaining_cards_in_tot =
			full_empty_count_in_tot * 2 + partial_count_in_tot;
		ll ways_to_place_remaining_cards_in_tot =
			(fact[places_for_remaining_cards_in_tot] *
			 inv2[full_empty_count_in_tot]) %
			MOD;
		ll ways_to_place_all_cards =
			(((((ways_to_split_small_cards * ways_to_place_even_cards) % MOD) *
			   ways_to_place_remaining_cards_in_tot) %
			  MOD) *
			 ways_to_place_even_in_left) %
			MOD;
		combinations = (combinations + ways_to_place_all_cards) % MOD;
	}
	return combinations;
}

ll calc_win_loss(vector<NormalSegment> segments, TotSegmentInfo info) {
	ll combinations = 0;
	for (int last_segment_idx = 0; last_segment_idx < sz(segments);
		 last_segment_idx++) {
		NormalSegment last_segment = segments[last_segment_idx];
		if (last_segment.is_loss) {
			continue;
		}
		if (last_segment.even_count <= 1) {
			continue;
		}
		ll odds_in_between = 0;
		for (ll position_from_segment_end = 1;
			 position_from_segment_end <
			 last_segment.even_count + last_segment.odd_count;
			 position_from_segment_end++) {
			if (position_from_segment_end % 2 == 1) {
				odds_in_between++;
				continue;
			}
			ll partial_even_count = info.win_segments + 1;
			ll full_empty_count = info.n - partial_even_count;
			ll places_for_even_cards =
				2 * full_empty_count + partial_even_count;
			ll small_cards = 4 * info.n - int(segments.size()) - 2;
			ll ways_to_split_small_cards =
				nChooseK(small_cards, places_for_even_cards);
			ll ways_to_place_even_cards =
				(fact[places_for_even_cards] * inv2[full_empty_count]) % MOD;
			ll empty_odd_in_between = odds_in_between;
			ll partial_odd_in_total = info.loss_segments;
			ll empty_odd_legal =
				info.n - partial_odd_in_total - empty_odd_in_between;
			ll places_for_odd_cards_outside =
				2 * empty_odd_legal + partial_odd_in_total;
			ll places_for_odd_cards_in_between = 2 * empty_odd_in_between;
			ll places_for_odd_in_total =
				places_for_odd_cards_in_between + places_for_odd_cards_outside;
			ll ways_to_choose_odd_cards_outside = nChooseK(
				places_for_odd_in_total - 1, places_for_odd_cards_outside - 1);
			ll ways_to_place_odd_cards_outside =
				(fact[places_for_odd_cards_outside] * inv2[empty_odd_legal]) %
				MOD;
			ll ways_to_place_odd_cards_in_between =
				(fact[places_for_odd_cards_in_between] *
				 inv2[empty_odd_in_between]) %
				MOD;
			ll ways_to_place_odd_cards = (((ways_to_choose_odd_cards_outside *
											ways_to_place_odd_cards_outside) %
										   MOD) *
										  ways_to_place_odd_cards_in_between) %
										 MOD;
			ll ways_to_place_all_cards =
				((((ways_to_split_small_cards * ways_to_place_even_cards) %
				   MOD) *
				  ways_to_place_odd_cards) %
				 MOD);
			combinations = (combinations + ways_to_place_all_cards) % MOD;
		}
	}
	return combinations;
}

void sol() {
	int n;
	cin >> n;
	vi a(2 * n);
	rep(i, 2 * n) cin >> a[i];
	int min_val = *min_element(all(a));
	int max_val = *max_element(all(a));
	if (min_val == 0 && max_val == 2) {
		cout << 0 << '\n';
		return;
	}
	// normalize so that team at odd positions is the one worse off
	if (max_val == 2) {
		rep(i, 2 * n) a[i] = 2 - a[i];
		a.insert(a.begin(), a.back());
		a.pop_back();
		min_val = *min_element(all(a));
		max_val = *max_element(all(a));
	}
	if (min_val == max_val) {
		if (min_val == 1) {
			cout << case_all_ones(n) << '\n';
		} else {
			cout << case_all_zeros(n) << '\n';
		}
		return;
	}
	// now we have a mixed case, with 0s and 1s, and the team at odd
	// positions is the one worse off we also know that each consecutive
	// range of 1s or 0s has an endpoint because there is at least one 0 and
	// one 1 since even is better off even gets Max1 \in Even since Max1,
	// Max2 \in team implies the team always wins, and this is not the case
	// we know that Max2 \in Odd and Max3 \in Even nice general case
	vector<NormalSegment> segments_info;
	int start_idx = 0;
	while (a[start_idx] == a[(start_idx + 1) % (2 * n)]) {
		start_idx++;
	}
	int taken_length = 0;
	while (taken_length < 2 * n) {
		int cur_idx = start_idx;
		NormalSegment seg = {0, 0, a[cur_idx] == 0};
		if (seg.is_loss != (cur_idx % 2 == 1)) {
			cout << 0 << '\n';
			return;
		}
		while (a[cur_idx] == a[start_idx]) {
			if (cur_idx % 2 == 0) {
				seg.even_count++;
			} else {
				seg.odd_count++;
			}
			cur_idx = (cur_idx - 1 + 2 * n) % (2 * n);
		}
		segments_info.push_back(seg);
		taken_length += seg.even_count + seg.odd_count;
		start_idx = cur_idx;
	}
	reverse(all(segments_info));

	TotSegmentInfo info = get_tot_segment_info(segments_info);
	ll ans = calc_loss_win(segments_info, info);
	ans += calc_win_win(segments_info, info);
	ans += calc_loss_loss(segments_info, info);
	ans += calc_win_loss(segments_info, info);
	ans %= MOD;
	cout << ans << '\n';
}

int32_t main() {
	cin.tie(0)->sync_with_stdio(0);
	prep_combinatorial();

	int t;
	cin >> t;
	rep(i, t) {
		sol();
	}
#ifdef LOCF
	cout.flush();
	cerr << "- - - - - - - - -\n";
	(void)!system(
		"grep VmPeak /proc/$PPID/status | sed s/....kB/\' MB\'/1 >&2"); // 4x.kB
																		// ....kB
#endif
	return 0;
}