Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/kombinatorika-orin-almashtirish-raqami

Index of a permutation

Hard 2000 ms 128 MB 81%
Share

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$.

A permutation; for each position the count of smaller numbers to its right is shown

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

Sign in to submit a solution

Signing up is free and takes a minute.