QOJ.ac

QOJ

时间限制: 4.0 s 内存限制: 512 MB 总分: 100 可 Hack ✓

#18428. Sort

统计

Given a permutation $p$ of length $n$.

Each operation randomly chooses an integer $i$ with equal probability ($1 \le i \le n$), and then sorts the prefix interval $[1, i]$ and the suffix interval $[i + 1, n]$ of the permutation $p$ in ascending order, respectively. In particular, when $i = n$, the suffix interval is empty, which is equivalent to sorting the entire permutation.

You need to find the expected number of operations required until the final permutation becomes completely sorted in ascending order (that is, $p_j = j$).

The answer should be taken modulo $998\,244\,353$.

Input

This problem contains multiple test cases.

The first line contains an integer $T$ ($1 \le T \le 100$), the number of test cases.

For each test case:

  • The first line contains an integer $n$ ($1 \le n \le 500$), the length of the permutation.
  • The second line contains $n$ integers $p_1, p_2, \cdots, p_n$ ($1 \le p_j \le n$), representing the given permutation $p$. It is guaranteed that the given sequence is a permutation of $1 \sim n$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$.

Output

For each test case, output one line containing an integer, the expected number of operations modulo $998\,244\,353$.

Example

Input

3
3
3 1 2
2
2 1
5
3 2 5 1 4

Output

748683267
2
670695427

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.