~/algor $ cat problems/kombinatorika-orin-almashtirish-raqami
Index of a permutation
Hard
2000 ms
128 MB
81%
All permutations of the numbers $1, 2, \dots, n$ are numbered in lexicographic (dictionary) order from $1$ to $n!$. Find the number of the given permutation. Print the answer modulo $10^{9} + 7$.
Input
The first line contains a positive integer $n$. The second line contains a permutation of the numbers from $1$ to $n$.
Output
One integer — the index modulo $10^{9} + 7$.
Note
$1 \le n \le 10^{5}$
Examples
Input #1
4 3 1 4 2
Output #1
14
Input #2
1 1
Output #2
1