Cryptography
Time limit1sMemory limit512 MB
Given a permutation P of N distinct integers, output the 1-based rank of P among all permutations of its values, modulo 1,000,000,007.
- Level
Medium6 of 10
- Topics
- Combinatorics, Sorting, Prefix sum, Math
- Solved
- No attempts yet
Problem
Charles the Cryptographer has been researching novel methods of generating random numbers. In particular, by combining multiple sources of random numbers, he hopes to create a cryptographically secure pseudorandom number generator (CSPRNG).
One algorithm that he has recently invented is as follows:
- Randomly generate a sequence S of N distinct positive integers S1, . . . , SN
- Randomly shuffle S to obtain a permutation1 P of N elements P1, . . . , PN
- Find the lexicographical order of P
- As the answer can be very large, output the value modulo2 1 000 000 007
The lexicographical order of P is defined as the number of permutations of S that are lexicographically smaller than3 or equal to P.
Unfortunately, Charles is a Cryptographer and not a Coder. Given the resultant permutation P, help Charles to find its lexicographical order, modulo 1 000 000 007.
1A permutation P of a sequence S is a rearrangement of the elements of S
2The remainder when the value is divided by 1 000 000 007
3A permutation P1, . . . , PN is considered lexicographically smaller than another permutation P'1 , . . . , P'N if there exists 1 ≤ k ≤ N such that Pk < P'k and Pi = P'i for i = 1, . . . , k − 1.
Input
Your program must read from standard input.
The first line contains a single integer N.
The second line contains N space-separated integers, P1, . . . , PN.
Output
Your program must print to standard output. The output should contain a single integer on a single line, the lexicographical order of P, modulo 1 000 000 007.
Constraints
- 1 ≤ N ≤ 3 × 105
- 1 ≤ Pi ≤ 109
- Pi ≠ Pj for i ≠ j