## KK’s Chemicals

Description

Our lovely KK has a difficult chemistry problem: There are N (1≤N≤100) bottles of chemicals in the laboratory, numbered from 0 to N-1. KK finds that for every bottle, there is a uniquely different bottle that will cause an explosion if the two are put together. KK needs to put the N bottles into K different boxes so that no explosions will happen. KK wants to know how many ways (modulo 1e9+7) he can do this.

Input a number T in the first line (1≤T≤200), which indicates the number of testcases. Each group of data has two rows. The first row has two integers N (1≤N≤100) and K (1≤K≤1000), indicating the number of chemicals and the number of boxes that need to be placed. Next, in the second row, N integers c[i] (0≤c[i]≤N−1) where on row i the bottle that makes bottle i explode is bottle c[i].

Output outputs an integer for each piece of data, which represents the result of the number of schemes modulo 1e9+7.

Sample Input

3
3 3
1 2 0
4 3
1 2 0 0
3 2
1 2 0


Sample Output

6
12
0


Solution

According to the given relationship, build a graph. The problem is transformed into coloring this graph with k different colors, where the colors of adjacent points cannot be the same. Considering that the i-th bottle will explode if and only if it is put together with the c[i]th bottle, it can be seen that this graph is composed of several connected blocks, where the number of points in each connected block is equal to the number of edges. Each connected block has only one ring, and other nodes branching off some vertexes of the ring. Therefore, we only need to dfs to find the connected block and the ring in the connected block.

• For the nodes not in the ring, each point only needs to have a different color from its adjacent points, so there are k-1 coloring schemes for each one. We can calculate the number of nodes not in the ring and do (k-1)^(the number of nodes).

• For the coloring on a ring, consider a ring with i points, and choose a point j. If the points at both ends of j have different colors, then j itself has k-2 coloring schemes, and we can calculate the answer as (k-2) times (the number of ways to color in the ring with i-1 points). If the colors of the points at both ends of j are the same, then j itself has k-1 coloring schemes. We can calculate the answer then as (k-1) times (the coloring of the ring of i-2 points) because the points with the same color at both ends of j are regarded as one point (as once we get the color of one of them, the other has to be the same). dp[i] represents the number of coloring schemes of the ring with i points.

• The transition can be obtained from the above analysis: dp[i]= ((k-2) * dp[i-1]) +((k-1) * dp[i-2])