Counting Permutations with a Given Greedily Increasing Subsequence
Time limit1sMemory limit512 MB
Count the permutations of 1 to N whose greedily increasing subsequence equals a given sequence G, modulo 1e9+7.
- Level
Hard8 of 10
- Topics
- Combinatorics, Math, Dynamic programming, Implementation
- Solved
- No attempts yet
Problem
Given a permutation of the integers , we define the greedily increasing subsequence (GIS) in the following way.
Let . For every , let be the leftmost integer in that is strictly larger than . If for a given there is no such integer, we say that the GIS of the sequence is the sequence .
For example, consider the permutation . First, we have . The leftmost integer larger than is , so . The leftmost integer larger than is ( is too small), so . Finally, . Thus, the GIS of is .
Given a sequence , how many permutations of the integers have as its GIS?
Input
The first line of input contains the integers , the number of elements of the permutation , and , the length of the sequence .
The next line contains positive integers between and , the elements of the sequence .
Output
Output a single integer: the number of -element permutations having the given sequence as its GIS. Since this number may be large, output it modulo the prime number .