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

int n;
vector<int> a;
vector<int> own;
vector<vector<int>> kar;

int sym(int sp, int d, int a1, int p1, int a2, int p2) {
    if (d == 2 * n) {
        return (p1 % 2 == 1) + (p2 % 2 == 1);
    }

    int cp = (sp + d) % (2 * n);

    bool bul = 0;
    int odp = 3;

    if (cp % 2 == 1)  bul = 1;
    if (bul) odp = -1;

    for (int i = 0; i < 2; i++) {

        int c1 = kar[cp][i];
        int c2 = kar[cp][1 - i];
        int na1 = a1;
        int np1 = p1;
        int na2 = a2;
        int np2 = p2;

        if (c1 > a1) {
            na1 = c1;
            np1 = cp;
        }

        if (c2 > a2) {
            na2 = c2;
            np2 = cp;
        }

        int v = sym(sp, d + 1, na1, np1, na2, np2);

        if (bul) odp = max(odp, v);
        else odp = min(odp, v);
    }

    return odp;
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int t;
    cin >> t;
    while (t--) {
        cin >> n;
        a.assign(2 * n, 0);
        own.assign(4 * n, 0);

        for (auto &i : a) cin >> i;

        for (int i = 0; i < 4 * n; i++) own[i] = i / 2;

        long long ct = 0;

        do {
            kar.assign(2 * n, {});

            for (int i = 0; i < 4 * n; i++) kar[own[i]].push_back(i + 1);

            bool bul = 1;

            for (int s = 0; s < 2 * n; s++) {
                if (sym(s, 0, -1, -1, -1, -1) != a[s]) {
                    bul = 0;
                    break;
                }
            }

            if (bul) ct++;

        } while (next_permutation(own.begin(), own.end()));

        cout << ct % mod << '\n';
    }
}