Random Permutation
Time limit10sMemory limit2048 MB
Count the distinct sequences of minimums obtainable from a random permutation by removing one end element each step, modulo 998244353.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Recursion, Array
- Solved
- No attempts yet
Problem
You are given a permutation consisting of integers from to . You want to build a sequence from . To do that, you perform the following operation times:
- append the minimum element of to the end of ;
- remove one of the ends of (either left or right).
You are given a random permutation . Your task is to calculate the number of different sequences that can be obtained in the way described above. This number can be very large, so find it modulo . Two sequences are different if there is a position at which these sequences differ.
A permutation of size is a sequence of distinct integers from to .
Input
The first line contains an integer (), the number of test cases. The test cases follow.
The first line of each test case contains an integer , the size of the permutation (). The next line contains the permutation itself: distinct integers from to . The permutation is generated using a pseudorandom number generator.
The sum of over all test cases does not exceed .
Output
For each test case, output a line with a single integer: the required number modulo .