atcoder#AGC054C. [AGC054C] Roughly Sorted
[AGC054C] Roughly Sorted
Score : points
Problem Statement
Snuke received a permutation of and an integer . He did some number of swaps of two adjacent elements in so that the following condition would be satisfied.
- For each , there are at most indices such that and .
Here, he did the minimum number of swaps needed to achieve this condition.
Afterward, he has forgotten the original permutation he received. Given the permutation after the swaps, find the number, modulo , of permutations that the original permutation could be.
Constraints
- ()
- For each , there is at most indices such that and .
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the answer.
3 1
3 1 2
2
could be one of the following two permutations.
- : The minimum number of swaps needed is . After zero swaps, the permutation is equal to .
- : The minimum number of swaps needed is : swapping and results in , which satisfies the condition and is equal to .
4 3
4 3 2 1
1
5 2
4 2 1 5 3
3