#P4561. [JXOI2018] 排序问题

[JXOI2018] 排序问题

Description

Kujou Karen has recently been studying the properties of various sorting algorithms, and she discovered a very interesting sorting method: Gobo sort!

The algorithm of Gobo sort is roughly as follows:

  • Suppose we want to sort a sequence aa of size nn.
  • Randomly and uniformly generate a permutation pp of size nn.
  • Construct a sequence bb of size nn such that bi=apib_i = a_{p_i}, check whether bb is sorted. If bb is already sorted, end the algorithm and return bb; otherwise, go back to step 2.

Obviously, the expected time complexity of this algorithm is O(n×n!)O(n \times n!). However, to her surprise, by using the magic of quantum mechanics, in a quantum system this algorithm can be optimized to linear time.

Kujou Karen studied this sorting algorithm further and found that if a sequence satisfies certain properties, Gobo sort will quickly compute the correct result. To quantify this speed, she defines the number of rounds of Gobo sort as the number of times step 2 is executed.

Then she came up with the following problem:

Now there is a sequence xx of length nn. Kujou Karen will append mm elements to the end of this sequence, each element being a positive integer in [l,r][l, r]. She wants the expected number of rounds for Gobo sort on the new sequence of length n+mn + m to be as large as possible. She wants to obtain this maximum expected number of rounds.

Kujou Karen is very smart and quickly computed the answer. She wants to check it with you. Since this expected number is too large, she only asks you to output the result modulo 998244353998244353.

Input Format

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

The next 2×T2 \times T lines describe the TT test cases.

Each test case consists of two lines. The first line contains four positive integers n,m,l,rn, m, l, r, representing the length of the sequence, the number of added numbers, and the range of the added numbers. The second line contains nn positive integers; the ii-th is xix_i.

Output Format

Output TT integers, the answers.

2
3 3 1 2
1 3 4
3 3 5 7
1 3 4
180
720

Hint

Sample Explanation

For the first test case, we can append {1,2,2}\{1, 2, 2\} to the end of the sequence, making it 1 3 4 1 2 2. Then the success probability in one round is 1180\frac{1}{180}, so the expected number of rounds is 180180.

For the second test case, we can append {5,6,7}\{5, 6, 7\} to the end of the sequence, making it 1 3 4 5 6 7. Then the success probability in one round is 1720\frac{1}{720}, so the expected number of rounds is 720720.

Constraints

  • For 30% of the testdata, T10T \leq 10, n,m,l,r8n, m, l, r \leq 8.
  • For 50% of the testdata, T300T \leq 300, n,m,l,r,ai300n, m, l, r, a_i \leq 300.
  • For 60% of the testdata, (rl+1)107\sum (r - l + 1) \leq 10^7.
  • For 70% of the testdata, n2×105\sum n \leq 2 \times 10^5.
  • For 90% of the testdata, m2×105m \leq 2 \times 10^5.
  • For 100% of the testdata, T105T \leq 10^5, n2×105n \leq 2 \times 10^5, m107m \leq 10^7, 1lr1091 \leq l \leq r \leq 10^9, 1ai1091 \leq a_i \leq 10^9, n2×106\sum n \leq 2 \times 10^6.

Translated by ChatGPT 5