This page is still under construction.

Parts of this page are still being built. What you see may change.

Cryptography

Time limit1sMemory limit512 MB

Summary
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:

  1. Randomly generate a sequence S of N distinct positive integers S1, . . . , SN
  2. Randomly shuffle S to obtain a permutation1 P of N elements P1, . . . , PN
  3. Find the lexicographical order of P
  4. 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

Examples3

  1. Example 1

    Input
    3
    42 100 1
    
    Expected output
    4
    
  2. Example 2

    Input
    5
    1 5 2 4 3
    
    Expected output
    20
    
  3. Example 3

    Input
    2
    2 1
    
    Expected output
    2