Under construction

Still being built. Things may change

Superpermutations

Time limit1sMemory limit512 MB

Summary
Given a permutation of 1 to n, find the position where it starts in the superpermutation s_n built by Dima's insertion process, modulo 1e9+7.
Level

Hard8 of 10

Topics
Combinatorics, Math, Implementation
Solved
No attempts yet

Superpermutations

Once, during his informatics class, Dima solved all the problems about permutations. Then he came up with a problem about a superpermutation.

A superpermutation of order nn is a sequence of integers from 11 to nn such that every permutation of the numbers from 11 to nn occurs as a contiguous subsegment of the sequence.

Dima came up with the following algorithm for generating superpermutations:

  • s1=[1]s_1 = [1].
  • Set sn+1s_{n+1} equal to sns_n at first.
  • Consider all subsegments of sns_n of length nn, from left to right, in order of increasing start position.
  • If the current subsegment sn[l],sn[l+1],…,sn[l+n−1]s_n[l], s_n[l+1], \ldots, s_n[l+n-1] is a permutation of the numbers from 11 to nn (each number from 11 to nn occurs exactly once), insert the numbers n+1,sn[l],sn[l+1],…,sn[l+n−1]n+1, s_n[l], s_n[l+1], \ldots, s_n[l+n-1] into sn+1s_{n+1} right after the last element sn[l+n−1]s_n[l+n-1] of that subsegment.

Here is how the superpermutations of order 11, 22, 33 and 44 are built.

By definition, s1=[1]s_1 = [1].

Set s2=[1]s_2 = [1]. The only subsegment of length 11 is [1][1], which is a permutation, so we insert [2,1][2, 1] after it. The result is s2=[1,2,1]s_2 = [1, 2, 1].

Set s3=[1,2,1]s_3 = [1, 2, 1]. The subsegment [1,2][1, 2] is a permutation, so we insert [3,1,2][3, 1, 2] after it, giving [1,2,3,1,2,1][1, 2, 3, 1, 2, 1]. The subsegment [2,1][2, 1] is also a permutation, so we insert [3,2,1][3, 2, 1] after it. The result is s3=[1,2,3,1,2,1,3,2,1]s_3 = [1, 2, 3, 1, 2, 1, 3, 2, 1].

Set s4=[1,2,3,1,2,1,3,2,1]s_4 = [1, 2, 3, 1, 2, 1, 3, 2, 1]. The subsegments of length 33 that are permutations are, in order, [1,2,3][1, 2, 3], [2,3,1][2, 3, 1], [3,1,2][3, 1, 2] and [2,1,3][2, 1, 3]. We insert [4,1,2,3][4, 1, 2, 3], [4,2,3,1][4, 2, 3, 1], [4,3,1,2][4, 3, 1, 2] and [4,2,1,3][4, 2, 1, 3] after them, respectively. The subsegments [1,2,1][1, 2, 1] is not a permutation, so nothing happens there. The remaining two permutations, [1,3,2][1, 3, 2] and [3,2,1][3, 2, 1], are handled the same way.

Dima noticed that this method builds a superpermutation in which every permutation occurs exactly once. To check his work, he wants to find the position where a given permutation a1,…,ana_1, \dots, a_n occurs in his superpermutation sns_n. Positions start at 1.

Since sns_n is extremely long, output the index of the first element of sns_n where the occurrence starts, modulo 109+710^9 + 7.

Input

The first line contains a single integer nn, the length of the permutation (1≤n≤300 0001 \le n \le 300\,000).

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n, the given permutation (1≤ai≤n1 \le a_i \le n, all aia_i are distinct).

Output

Output a single integer, the position where the permutation a1,a2,…,ana_1, a_2, \ldots, a_n occurs in the superpermutation of order nn, modulo 109+710^9+7.

Examples3

  1. Example 1

    Input
    3
    2 3 1
    
    Expected output
    2
    
  2. Example 2

    Input
    4
    2 3 1 4
    
    Expected output
    6
    
  3. Example 3

    Input
    4
    4 3 1 2
    
    Expected output
    14