1534C.md (2275B)
1 Source: https://codeforces.com/problemset/problem/1534/C 2 3 C. Little Alawn's Puzzle 4 5 time limit per test 6 7 2.5 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 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$. 22 23 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. 24 25 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$. 26 27 Input 28 29 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. 30 31 The first line of each test case contains a single integer $n$ ($2 \le n \le 4 \cdot 10^5$). 32 33 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. 34 35 It is guaranteed that the sum of $n$ over all test cases does not exceed $4 \cdot 10^5$. 36 37 Output 38 39 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$. 40 41 The answer for each test case should be on a separate line. 42 43 Example 44 45 Input 46 47 ``` 48 2 49 4 50 1 4 2 3 51 3 2 1 4 52 8 53 2 6 5 1 4 3 7 8 54 3 8 7 5 1 2 4 6 55 ``` 56 57 Output 58 59 ``` 60 2 61 8 62 ``` 63 64 Note 65 66 The two possible puzzle configurations for example $1$ are: 67 68 * $[1,4,2,3]$ in the first row and $[3,2,1,4]$ in the second; 69 * $[3,2,1,4]$ in the first row and $[1,4,2,3]$ in the second.