Problem: You are given a permutation of length . Recall that permutation is an array consisting of distinct integers from to in arbitrary order.
You have a strength of and perform moves on the permutation . The -th move consists of the following:
- Pick two integers and such that , and swap the positions of the integers and in the permutation . Note that you can select in the operation, in which case no swap will occur.
You want to turn into another permutation after moves. However, some elements of are missing and are replaced with instead. Count the number of ways to replace each in with some integer from to so that is a permutation and it is possible to turn into with a strength of .
Since the answer can be large, output it modulo .
Input Format: The input consists of multiple test cases. The first line contains an integer () — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers and (; ) — the size of the permutation and your strength, respectively.
The second line of each test case contains integers () — the elements of . All elements of are distinct.
The third line of each test case contains integers ( or ) — the elements of . All elements of that are not equal to are distinct.
It is guaranteed that the sum of over all test cases does not exceed .
Output Format: For each test case, output a single integer — the number of ways to fill up the permutation so that it is possible to turn into using a strength of , modulo .
Note: In the first test case, . There are two possible ways to fill out the s in to make it a permutation: or . We can make into with a strength of as follows: It can be proven that it is impossible to make into with a strength of . Thus only one permutation satisfies the constraints, so the answer is .
In the second test case, and the same as the previous test case, but we now have a strength of . We can make into with a strength of as follows: We can still make into using a strength of as shown in the previous test case, so the answer is .
In the third test case, there is only one permutation . It can be shown that it is impossible to turn into , so the answer is .