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
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <utility>

using namespace std;

const int MOD = 1000000007;

int n, m, tot;
vector<int> V, V2, V3;
vector<pair<int,int>> V4;
int start;

int solveMask(int mask){
    if(V3[mask] != -1){
        return V3[mask];
    }

    int used = 0;
    for(int i = 0; i < tot; i++){
        if(mask & (1 << i)){
            used++;
        }
    }

    if(used == m){
        int x = 0;
        int y = 0;
        int z = 0;
        int xx = 0;

        for(int c = 1; c <= tot; c++){
            if(mask & (1 << (c - 1))){
                if(c > x){
                    x = c;
                    y = V2[c];
                }
            } else {
                if(c > z){
                    z = c;
                    xx = V2[c];
                }
            }
        }

        V3[mask] = y + xx;
        return V3[mask];
    }

    int pos = (start + used) % m;
    int team = pos % 2;

    int c1 = V4[pos].first;
    int c2 = V4[pos].second;

    int v1 = solveMask(mask | (1 << (c1 - 1)));
    int v2 = solveMask(mask | (1 << (c2 - 1)));

    if(team == 1){
        V3[mask] = max(v1, v2);
    } else {
        V3[mask] = min(v1, v2);
    }

    return V3[mask];
}

bool checkDistribution(){
    fill(V2.begin(), V2.end(), 0);

    for(int i = 0; i < m; i++){
        int team = i % 2; 
        V2[V4[i].first] = team;
        V2[V4[i].second] = team;
    }

    for(int s = 0; s < m; s++){
        start = s;
        V3.assign(1 << tot, -1);
        int got = solveMask(0);
        if(got != V[s]) return false;
    }

    return true;
}

void gen(int pos, int remMask, long long &out){
    if(pos == m){
        if(checkDistribution()){
            out++;
            if(out >= MOD){
                out -= MOD;
            }
        }
        return;
    }

    for(int i = 0; i < tot; i++){
        if(remMask & (1 << i)){
            for(int j = i + 1; j < tot; j++) {
                if(remMask & (1 << j)){
                    V4[pos] = {i + 1, j + 1};
                    gen(pos + 1, remMask ^ (1 << i) ^ (1 << j), out);
                }
            }
        }
    }
}

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

    int t;
    cin >> t;

    while(t--){
        cin >> n;
        m = 2 * n;
        tot = 4 * n;

        V.assign(m, 0);
        for(int i = 0; i < m; i++){
            cin >> V[i];
        }

        if(n > 2) {
            cout << 0 << '\n';
            continue;
        }

        V4.assign(m, {0, 0});
        V2.assign(tot + 1, 0);

        long long out = 0;
        gen(0, (1 << tot) - 1, out);

        cout << out % MOD << '\n';
    }
    return 0;
}