luogu#P4769. [NOI2018] 冒泡排序

[NOI2018] 冒泡排序

Background

Please note that the testdata includes cases with n=0n = 0.

Problem Description

Recently, Xiao S has become very interested in bubble sort. To simplify the problem, Xiao S only studies bubble sorting permutations of 11 to nn.

Below is the algorithm description of bubble sort.

Input: a permutation p[1...n] of length n
Output: the sorted result of p.
for i = 1 to n do
	for j = 1 to n - 1 do
		if(p[j] > p[j + 1])
			swap the values of p[j] and p[j + 1]

The number of swaps in bubble sort is defined as the number of times the swap operation is executed. It can be proven that a lower bound of the swap count is 12i=1nipi\frac 1 2 \sum_{i=1}^n \lvert i - p_i \rvert, where pip_i is the number at position ii in the permutation pp. If you are interested in the proof, you can read the hint.

Xiao S starts focusing on permutations of length nn that satisfy swap count =12i=1nipi= \frac 1 2 \sum_{i=1}^n \lvert i - p_i \rvert (later, for convenience, we call all such permutations “good” permutations). He further wonders: are there many such permutations? Are they dense?

For a given permutation qq of length nn, Xiao S wants to compute the number of “good” permutations that are strictly greater than qq in lexicographical order. However, he cannot do it, so he asks you for help. Since the answer may be very large, you only need to output the result modulo 998244353998244353.

Input Format

The first line contains a positive integer TT, indicating the number of test cases.

For each test case, the first line contains a positive integer nn, with n6×105n \leq 6 \times 10^5.

The next line contains nn positive integers, corresponding to qiq_i in the statement, and it is guaranteed that the input is a permutation of 11 to nn.

Output Format

Output TT lines, each containing one integer.

For each test case, output one integer: the number of “good” permutations that are strictly greater than qq in lexicographical order, modulo 998244353998244353.

1
3
1 3 2
3
1
4
1 4 2 3
9

Hint

More Samples

Please download more samples from the attachment.

Sample 3

See inverse3.in and inverse3.ans in the attachment.

Explanation for Sample 1

Among the permutations that are lexicographically greater than 1 3 21 \ 3 \ 2, all of them are “good” except 3 2 13 \ 2 \ 1, so the answer is 33.

Constraints

Below is a description of the input size for each test point.

For all testdata, it holds that T=5T = 5 (samples may not satisfy this).

Let nmaxn_\mathrm{max} be the maximum nn among the test cases, and let n\sum n be the sum of nn over all test cases.

::cute-table{tuack}

Test Point nmax=n_\mathrm{max} = n\sum n \leq Special Properties
11 88 5 nmax5 \ n_\mathrm{max} None
22 99 ^
33 1010
44 1212
55 1313
66 1414
77 1616
88
99 1717
1010 1818
1111
1212 122122 700700 iqi=i\forall i \enspace q_i = i
1313 144144 ^ None
1414 166166 ^
1515 200200
1616 233233
1717 777777 40004000 iqi=i\forall i \enspace q_i = i
1818 888888 ^ None
1919 933933 ^
2020 10001000
2121 266666266666 20000002000000 iqi=i\forall i \enspace q_i = i
2222 333333333333 ^ None
2323 444444444444 ^
2424 555555555555
2525 600000600000

Hint

Below is the proof that a lower bound of the swap count is 12i=1nipi\frac 1 2 \sum_{i=1}^n \lvert i - p_i \rvert.

Sorting is essentially moving numbers, so the number of swaps in sorting can be described by the total distance that numbers move. For position ii, suppose the number at this position in the initial permutation is pip_i. Then we need to move this number to position pip_i, and the moving distance is ipi\lvert i - p_i \rvert. Therefore, the total moving distance is i=1nipi\sum_{i=1}^n \lvert i - p_i \rvert. Each step of bubble sort swaps two adjacent numbers, and each swap can reduce the total moving distance by at most 22. Hence, 12i=1nipi\frac 1 2 \sum_{i=1}^n \lvert i - p_i \rvert is a lower bound of the swap count in bubble sort.

Not all permutations reach this lower bound. For example, when n=3n = 3, consider the permutation 3 2 13 \ 2 \ 1. The number of swaps after bubble sorting this permutation is 33, but 12i=1nipi\frac 1 2 \sum_{i=1}^n \lvert i - p_i \rvert is only 22.

Translated by ChatGPT 5