Under construction

Still being built. Things may change

Double Derangement

Interview

Time limit1sMemory limit1024 MB

Summary
Given two permutations a and b of length N (up to 16), count the permutations c where c_i differs from both a_i and b_i at every index.
Level

Medium5 of 10

Topics
Dynamic programming, Bit manipulation
Solved
No attempts yet

Problem

You are given two permutations aa and bb, each of length NN. Each permutation contains every integer in the range [1,N][1, N] exactly once. Construct a new permutation cc of length NN that contains every integer in the range [1,N][1, N], such that for every index ii (11-indexed), the following condition holds.

ci≠aiandci≠bic_i \ne a_i \quad \text{and} \quad c_i \ne b_i

In other words, cic_i can never equal aia_i or bib_i at the same index. Given aa and bb, count the permutations cc that satisfy this condition.

Input

The first line contains a single integer TT, the number of test cases.

Each test case is given as follows. The first line contains a single integer NN, the length of the permutations. The next line contains NN space-separated integers, the permutation aa, where the ii-th integer is aia_i. The next line contains NN space-separated integers, the permutation bb, where the ii-th integer is bib_i.

Constraints: 1≤N≤161 \le N \le 16; 1≤ai≤N1 \le a_i \le N; 1≤bi≤N1 \le b_i \le N; within each permutation, all values are distinct. The sum of NN over all test cases is at most 2,0002{,}000.

Output

For each test case, output the number of permutations cc that satisfy the condition.

Hint

You may have to use a type larger than a 32-bit integer to prevent overflow.

Examples1

  1. Example 1

    Input
    1
    4
    1 2 3 4
    2 3 1 4
    
    Expected output
    3