Little Alawn's Puzzle
Contest: Codeforces LATOKEN Round 1 (Div. 1 + Div. 2)
Difficulty rating: 1300
Tags: combinatorics, dp, dsu, graphs, math

When he's not training for IOI, Little Alawn enjoys playing with puzzles of various types to stimulate his brain. Today, he's playing with a puzzle that consists of a $$$2 \times n$$$ grid where each row is a permutation of the numbers $$$1,2,3,\ldots,n$$$.

The goal of Little Alawn's puzzle is to make sure no numbers on the same column or row are the same (we'll call this state of the puzzle as solved), and to achieve this he is able to swap the numbers in any column. However, after solving the puzzle many times, Little Alawn got bored and began wondering about the number of possible solved configurations of the puzzle he could achieve from an initial solved configuration only by swapping numbers in a column.

Unfortunately, Little Alawn got stuck while trying to solve this harder problem, so he was wondering if you could help him with it. Find the answer modulo $$$10^9+7$$$.

Input format:
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.

The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 4 \cdot 10^5$$$).

The next two lines of each test case describe the initial state of the puzzle grid. Each line will be a permutation of the numbers $$$1,2,3,\ldots,n$$$ and the numbers in each column and row will be pairwise distinct.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$4 \cdot 10^5$$$.

Output format:
For each test case output a single integer, the number of possible solved configurations of the puzzle Little Alawn can achieve from an initial solved configuration only by swapping numbers in a column. As the answer can be very large, please output it modulo $$$10^9+7$$$.

The answer for each test case should be on a separate line.

Constraints:
Time limit: 2.5 s
Memory limit: 256.0 MB

Note:
The two possible puzzle configurations for example $$$1$$$ are:

- $$$[1,4,2,3]$$$ in the first row and $$$[3,2,1,4]$$$ in the second;
- $$$[3,2,1,4]$$$ in the first row and $$$[1,4,2,3]$$$ in the second.

Example input:
2
4
1 4 2 3
3 2 1 4
8
2 6 5 1 4 3 7 8
3 8 7 5 1 2 4 6

Example output:
2
8

Editorial:
Define the "direction" of a column as the orientation of its numbers. Swapping the numbers in a column will flip its direction.
Let's create a simple, undirected graph where the nodes are the $$$n$$$ columns on the puzzle and we draw one edge connecting it to the $$$2$$$ other columns that share a number with it. Notice that the degree of every node in this graph is $$$2$$$, so the graph must be made of some number of disjoint simple cycles.
Now consider any component in the graph. If we fix the direction of any of the columns in the component, that will fix the direction of the columns adjacent to it, and so on until the direction of every column in the component has been fixed (also note that as the component is a simple cycle, we will never get a contradiction). As there are $$$2$$$ possible directions for any column, there are thus $$$2$$$ ways to direct the columns in this component.
Lastly, notice that the columns in each component are independent, so the answer is simply $$$2^k$$$, where $$$k$$$ is the number of components in our graph.
Time Complexity: $$$\mathcal{O}(n)$$$ with DFS