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
#include <bits/stdc++.h>
using namespace std;

const int INPUT_SIZE = 4 * 1000010;

long long fact[INPUT_SIZE];

long long MOD_V = 1000000007;
long long MOD(long long x) {
	// Assumes x >= 0
	return x % MOD_V;
}

long long poww_unsafe(long long x, long long exp) {
	// If x == 0, exp < 0 something went VERY wrong
	if (exp == 0) {
		return 1;
	}

	long long res = poww_unsafe(x, exp / 2);
	res *= res;
	res = MOD(res);

	if (exp % 2 == 1) {
		res *= x;
		res = MOD(res);
	}
	return res;
}

// I should store the inverse of 2 to make this faster
long long inverse(long long x) {
	return poww_unsafe(x, MOD_V - 2);
}

long long poww(long long x, long long exp) {
	if (exp < 0) {
		return poww_unsafe(inverse(x), -exp);
	}
	else {
		return poww_unsafe(x, exp);
	}
}

int n;
int wins[INPUT_SIZE];
int next_changepoint[INPUT_SIZE];

int segment_lengths[INPUT_SIZE];
int segment_types[INPUT_SIZE];

bool has0, has1, has2;

long long inv_2;

long long ways_to_place(int players_needing_1, int players_needing_2) {
	int index = 2 * players_needing_2 + players_needing_1;
	if (index < 0 || index >= INPUT_SIZE) {
		// Negative numbers of players cannot be placed
		return 0;
	}
	return MOD(fact[index] * poww(inv_2, (players_needing_2)));
}

bool is_changepoint(int pos) {
	return (wins[pos] != wins[(pos + 1) % (2 * n)]);
}

long long count_ways(int swap_count) {
	int tower_length = swap_count;
	//cerr << "We are counting ways" << endl;

	long long result = 0;
	int i = 0;
	if (has2 && (i % 2 == 0)) {
		i++;
	}
	for (; i < 2 * n; i+=2) {
		//cerr << "Considering placing highest number at " << i << "\n";
		if (wins[i] != 1) {
			// Bad head of tower placement
			continue;
		}



		// We assume this is the place we put the tower head;
		bool head_changepoint = is_changepoint(i);


		// First, analyse the two types of towers that lead to the top being a changepoint
		if (head_changepoint) {
			//cerr << "It's a changepoint\n";
			int tower_space = ((i - next_changepoint[i] + 2 * n) % (2 * n)) + 1;
			// All other tower elements are always changepoints

			// First do the P with loopback
			// First, assume the loopback is to a tower element
			long long ways_to_choose_below = tower_length / 2;
			int players_needing_1_more = tower_length - 1;
			int players_needing_2_more = 2 * n - players_needing_1_more - 1;
			long long ways_to_place_rest = ways_to_place(players_needing_1_more, players_needing_2_more);
			long long to_add = MOD(ways_to_choose_below * ways_to_place_rest);
			//cerr << to_add << " ways to add a tower with P-loopback, reuse\n";
			result = MOD(result + to_add);

			// Now, assume the loopback is to something else

			long long ways_to_choose_loopback = tower_space / 2 - tower_length / 2; //We got back to any element of P in the tower space, but not in the tower 
			players_needing_1_more = tower_length + 1; // Tower + P
			players_needing_2_more = 2 * n - players_needing_1_more;
			ways_to_place_rest = ways_to_place(players_needing_1_more, players_needing_2_more);
			to_add = MOD(ways_to_choose_loopback * ways_to_place_rest);
			//cerr << to_add << " ways to add a tower with P-loopback, no-reuse\n";
			result = MOD(result + to_add);


			// We are done with the first tower type. Now PP towers
			// First, assume the last P goes to something repeating
			long long ways_to_choose_P1 = n - tower_space / 2; // Any player with NO LOOPBACK; similar to above;
			long long ways_to_choose_P2 = tower_length / 2 + 1; // Any player from the tower, or P1 (P1 has no loopback, so not in tower)
			players_needing_1_more = tower_length; // +1 from P1, -1 from P2
			players_needing_2_more = 2 * n - players_needing_1_more - 1;
			ways_to_place_rest = ways_to_place(players_needing_1_more, players_needing_2_more);
			to_add = MOD(MOD(ways_to_choose_P1 * ways_to_choose_P2) * ways_to_place_rest);
			//cerr << to_add << " ways to add a tower with PP-noloop, reuse\n";
			result = MOD(result + to_add);

			// Now, assume second P is a new player
			ways_to_choose_P1 = n - tower_space / 2;//Any player with NO LOOPBACK, as above
			ways_to_choose_P2 = n - tower_length / 2 - 1; // We can choose anyone not from tower or P1
			players_needing_1_more = tower_length + 2; // Tower, P1, P2
			players_needing_2_more = 2 * n - players_needing_1_more;
			ways_to_place_rest = ways_to_place(players_needing_1_more, players_needing_2_more);
			to_add = MOD(MOD(ways_to_choose_P1 * ways_to_choose_P2) * ways_to_place_rest);
			//cerr << to_add << " ways to add a tower with PP-noloop, no-reuse\n";
			result = MOD(result + to_add);
		}
		else {
			//cerr << "It's not a changepoint\n";
			// Here we count the second type of towers -- ones with the dreadful changepoint foundation, where child processes get killed by daemons

			// First, the horrific PA tower, with loops for horns
			// The P is set to the changepoint, meaning we need not worry for its threat
			int P_pos = next_changepoint[i];
			int tower_plus_P_space = ((i - P_pos + 2 * n) % (2 * n)) + 1;
			// Assume A goes to a tower element
			long long ways_to_place_A = tower_length / 2;
			int players_needing_1_more = tower_length; // Tower + P1 - A
			int players_needing_2_more = 2*n - players_needing_1_more - 1; // A needs 0
			long long ways_to_place_rest = ways_to_place(players_needing_1_more, players_needing_2_more);
			long long to_add = MOD(ways_to_place_A * ways_to_place_rest);
			//cerr << to_add << " ways to add a tower with PA-loopback, reuse\n";
			result = MOD(result + to_add);

			// Now assume A loops back to a new element
			ways_to_place_A = (tower_plus_P_space - 1) / 2 - tower_length / 2; //Go to any A-element in the tower+P-space (has odd length) that is not in the tower
			players_needing_1_more = tower_length + 2; // Tower + P1 + A
			players_needing_2_more = 2 * n - players_needing_1_more;
			ways_to_place_rest = ways_to_place(players_needing_1_more, players_needing_2_more);
			to_add = MOD(ways_to_place_A * ways_to_place_rest);
			//cerr << to_add << " ways to add a tower with PA-loopback, no-reuse\n";
			result = MOD(result + to_add);


			// Now, the terrible PAA tower, which we assume have no loops for horns to prevent double counting
			// P is again a changepoint, so it is fixed
			// Assume the second A goes to an already appearing element
			long long ways_to_place_A1 = (2 * n - tower_plus_P_space + 1) / 2; //Ways to place without loopback -- any element not in the tower+P-space, +1 since this segment is odd with both endp in A
			long long ways_to_place_A2 = tower_length / 2 + 1; // Anything from tower or A1
			players_needing_1_more = tower_length + 1; // +P1, +A1, -A2
			players_needing_2_more = 2 * n - players_needing_1_more - 1;
			ways_to_place_rest = ways_to_place(players_needing_1_more, players_needing_2_more);
			to_add = MOD(MOD(ways_to_place_A1 * ways_to_place_A2) * ways_to_place_rest);
			//cerr << "A1: " << ways_to_place_A1 << "\nA2: " << ways_to_place_A2 << "\nRest: " << ways_to_place_rest << "\n";
			//cerr << to_add << " ways to add a tower with PAA, reuse\n";
			result = MOD(result + to_add);

			// Now finally, assume A goes to something new
			ways_to_place_A1 = (2 * n - tower_plus_P_space + 1) / 2;//Ways to place without loopback
			ways_to_place_A2 = n - (tower_length / 2) - 1; // Anything but tower or A1
			players_needing_1_more = tower_length + 3; // P, A1, A2
			players_needing_2_more = 2 * n - players_needing_1_more;
			ways_to_place_rest = ways_to_place(players_needing_1_more, players_needing_2_more);
			//cerr << "A1: " << ways_to_place_A1 << "\nA2: " << ways_to_place_A2 << "\nRest: " << ways_to_place_rest << "\n";
			to_add = MOD(MOD(ways_to_place_A1 * ways_to_place_A2) * ways_to_place_rest);
			//cerr << to_add << " ways to add a tower with PAA, no-reuse\n";
			result = MOD(result + to_add);
		}
/*
		// First count ways when below-tower is repeating a placement
		long long ways_to_place_below_head = tower_length / 2;
		int players_needing_1_more = tower_length - 1;
		int players_needing_2_more = 2 * n - players_needing_1_more - 1;

		long long add0 = MOD(MOD(ways_to_place_head * ways_to_place_below_head) * ways_to_place_rest);
		result = MOD(result + add0);

		// Now count ways when below-tower is a new player
		// Invalid if tower_length = 2n, but then we mult by 0
		ways_to_place_below_head = (n - tower_length / 2);
		players_needing_1_more = tower_length + 1;
		players_needing_2_more = 2 * n - players_needing_1_more;
		ways_to_place_rest = fact[2 * players_needing_2_more + players_needing_1_more] * poww(2, - players_needing_2_more);

		long long add1 = MOD(MOD(ways_to_place_head * ways_to_place_below_head) * ways_to_place_rest);
		result = MOD(result + add1);*/
	}

	return result;
}

int main() {
	std::ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	fact[0] = 1;
	for (int i = 1; i < INPUT_SIZE; i++) {
		fact[i] = MOD(i * fact[i - 1]);
	}

	inv_2 = inverse(2);
	
	int t;
	cin >> t;
	for (int r = 0; r < t; r++) {
		cin >> n;


		has0 = false;
		has1 = false;
		has2 = false;
		for (int i = 0; i < 2 * n; i++) {
			cin >> wins[i];
			has0 |= wins[i] == 0;
			has1 |= wins[i] == 1;
			has2 |= wins[i] == 2;
		}

		if (has0 && has2) {
			cout << 0 << "\n";
			continue;
		}
		if (has0 xor has1 xor has2) {
			if (has0 || has2) {
				long long one_player_tops = MOD(n * 1 * ways_to_place(0, 2 * n - 1));
				long long two_players_top = MOD(MOD(n * (n-1)) * ways_to_place(2, 2 * n - 2));
				cout << MOD(one_player_tops + two_players_top) << "\n";
			}
			// There is only one kind of number, do some mess
			if (has1) {
				// Top is APP or PAA; assume PAA and mult by 2
				long long ways_to_choose_P = n;
				long long ways_to_choose_A1 = n;
				//long long ways_to_repeat_A1 = 1;
				long long ways_for_new_A2 = (n-1);

				long long repeat_A = MOD(MOD(ways_to_choose_P * ways_to_choose_A1) * ways_to_place(1, 2 * n - 2));
				long long new_A = MOD(ways_to_choose_P * MOD(ways_to_choose_A1 * MOD(ways_for_new_A2 * ways_to_place(3, 2 * n - 3))));
				cout << MOD(2 * (repeat_A + new_A)) << "\n";
			}
			continue;
		}

		// We know there are exactly 2 kinds of numbers, we do analysis

		int start = 0;
		while (!is_changepoint(start)) {
			start++;
		}

		start++;
		// Check if changepoints have right parity
		if (wins[start] == 1) {
			if (wins[(start - 1 + 2 * n) % (2 * n)] == 0) {
				// Changepoint made it better for A
				// A P should be sitting here
				if (start % 2 != 0) {
					cout << 0 << "\n";
					continue;
				}
			}
			if (wins[(start - 1 + 2 * n) % (2 * n)] == 2) {
				// Changepoint made it better for P
				// An A should be sitting here
				if (start % 2 == 0) {
					cout << 0 << "\n";
					continue;
				}
			}
		}
		if (wins[start] == 2) {
			// Changepoint made it better for A
			// A P should be sitting here
			if (start % 2 != 0) {
				cout << 0 << "\n";
				continue;
			}
		}
		if (wins[start] == 0) {
			// Changepoint made it better for P
			// An A should be sitting here
			if (start % 2 == 0) {
				cout << 0 << "\n";
				continue;
			}
		}

		bool all_odd_lengths = true;
		int last_number = wins[start];

		int length = 1;
		int segment_count = 0;
		for (int i = 1; i < 2 * n; i++) {
			int w;
			w = wins[(start + i) % (2 * n)];
			if (w == last_number) {
				length++;
			}
			else {
				segment_lengths[segment_count] = length;
				segment_types[segment_count] = last_number;
				segment_count++;
				if (length % 2 == 0) {
					all_odd_lengths = false;
				}
				length = 1;
			}
			last_number = w;
		}
		segment_lengths[segment_count] = length;
		segment_types[segment_count] = last_number;
		segment_count++;

		if (!all_odd_lengths) {
			cout << 0 << "\n";
			continue;
		}

		assert(segment_count % 2 == 0);

		// We know that there are a bunch of segments, all with odd lengths. How many diff assignments there are?
		start = 1;
		while(!is_changepoint(start)) {
			start++;
		}
		int next = start;
		start--;
		for (int i = 0; i < 2 * n; i++) {
			int index = (start - i + 2 * n) % (2 * n);
			next_changepoint[index] = next;
			if (is_changepoint(index)) {
				next = index;
			}
		}
		
		
		cout << count_ways(segment_count) << "\n";
	}


}