#HK4107. 「BalkanOI 2023 Day1」Permutations
「BalkanOI 2023 Day1」Permutations
Description
You are given a permutation of the numbers . You need to answer queries.
The -th query (for ) is described by the numbers and (). The answer to the query is the number of permutations of length that start with the sequence and that, additionally, satisfy the property that the length of their longest decreasing subsequence is at most . Since the answers can be very large, output them modulo .
For a sequence , the length of the longest decreasing subsequence is the greatest integer such that there are indices with the properties and .
Input format
The first line contains the number .
The second line contains the numbers , i.e., distinct integers from the interval .
The third line contains the number .
The next lines specify the queries: the -th of those lines, for , contains the numbers and .
Output format
For each query, print the number of permutations modulo . Each should be on a separate line.
Input bounds
- .
- .
Subtasks
- (6 points) .
- (7 points) . Each query contains in its interval.
- (9 points) Each query contains in its interval.
- (12 points) . For each , , and for each ,.
- (18 points) For each , and for each .
- (12 points) .
- (36 points) No additional constraints.
5
4 2 1 5 3
4
1 1
2 3
2 4
1 3
4
5
1
0
For the first query, consider that there are four permutations of the sequence that start with and have the length of the longest decreasing subsequence at most $2¥. These are:
- ;
- ;
- ;
- .