luogu#P11256. [GDKOI2023 普及组] 置换

[GDKOI2023 普及组] 置换

Problem Description

Moon has recently been playing a card game called Shadowverse. During the game, Moon thought of a shuffling problem. Suppose there are nn cards in the deck. The label of the ii-th card is ii. We define a shuffling method as a permutation X={x1,x2,...,xn}X=\{x_1, x_2, ..., x_n\}, meaning that the card in position ii in the deck is moved to position xix_i. Now suppose Moon shuffles the deck kk times using the method XX. Let the final permutation be Y={y1,y2,...,yn}Y=\{y_1, y_2, ..., y_n\}, where yiy_i denotes the label of the ii-th card after shuffling. Moon wants you to compute how many valid shuffling methods XX satisfy that after shuffling kk times, the result becomes permutation YY. Since the answer may be very large, you only need to output the answer modulo 998244353998244353.

Formally, for a permutation P={p1,p2,...,pn}P=\{p_1, p_2, ..., p_n\} and a permutation Q={q1,q2,...,qn}Q=\{q_1, q_2, ..., q_n\}, define their product as:

P×Q={qp1,qp2,...,qpn}P \times Q = \{q_{p1} , q_{p2} , ..., q_{pn} \}

The kk-th power XkX^k of permutation XX is the product of kk copies of XX. Now given a permutation YY and a positive integer kk, compute the number of permutations XX that satisfy the equation Xk=YX^k = Y, modulo 998244353998244353.

Input Format

The first line contains an integer TT, denoting the number of testdata cases.

Each test case consists of two lines. The first line contains two positive integers n,kn, k, representing the length of permutations XX and YY, and that the deck is shuffled kk times.

The second line contains nn distinct positive integers {y1,y2,...,yn}\{y_1, y_2, ..., y_n\} from 11 to nn, representing permutation YY.

Output Format

Output TT lines. Each line contains one integer, representing the number of valid shuffling methods, modulo 998244353998244353.

1
5 6
2 1 4 3 5
2
见/example/permutation/下的
permutation1.in
见/example/permutation/下的
permutation1.out

Hint

Sample Explanation

In the sample, X=[3,4,2,1,5]X = [3,4,2,1,5] or [4,3,1,2,5][4,3,1,2,5]. There are 22 valid permutations in total.

Constraints

For all testdata, 1n30001 \le n \le 3000, 1k1061 \le k \le 10^6, 1T101 \le T \le 10.

For 20%20\% of the testdata, 1n,k81 \le n, k \le 8.

For another 10%10\% of the testdata, only 1n81 \le n \le 8 is guaranteed.

For another 30%30\% of the testdata, only 1n501 \le n \le 50 is guaranteed.

Translated by ChatGPT 5