#FT0000079. Thứ hạng từ điển của hoán vị (The Permutation Game Again)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Thứ hạng từ điển của hoán vị (The Permutation Game Again)
The Permutation Game Again
Source: SPOJ
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a permutation of integers , with each number appearing exactly once.
Consider all permutations of these numbers, ordered in increasing lexicographic order. At the first position where two permutations differ, the one containing the smaller number is ordered first. The first permutation has rank 1, not rank 0.
Compute the rank of the given permutation and output its remainder modulo .
Input
The first line contains integer .
The second line contains integers , forming a permutation of .
Output
Print one integer: the 1-based lexicographic rank of the permutation modulo .
Subtasks
-
Subtask 1 (20 points): ; ; each integer from to occurs exactly once.
-
Subtask 2 (30 points): ; ; each integer from to occurs exactly once.
-
Subtask 3 (50 points): ; ; each integer from to occurs exactly once.
Examples
Input
4
2 1 4 3
Output
8
Explanation
For , there are permutations. The first begin with .
Permutations beginning with come next. Within this group, prefix precedes prefixes and . Exactly two permutations begin with :
- has rank .
- has rank .
The input is the latter, so its 1-based rank is and the required output is 8.