logo AlgoBeat OnlineJudge
登录 注册

#217717. [ICPC 2026 Wuhan I] Sort

内存限制:512 MiB 时间限制:4000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

Given a permutation of length .

Each operation randomly chooses an integer with equal probability (), and then sorts the prefix interval and the suffix interval of the permutation in ascending order, respectively. In particular, when , 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, ).

The answer should be taken modulo .

输入格式

This problem contains multiple test cases.

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

For each test case:

  • The first line contains an integer (), the length of the permutation.
  • The second line contains integers (), representing the given permutation . It is guaranteed that the given sequence is a permutation of .

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

输出格式

For each test case, output one line containing an integer, the expected number of operations modulo .

样例

样例输入 1

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

样例输出 1

748683267
2
670695427