Superpermutations

아직 제출이 없습니다시간 제한1초메모리 제한512 MB

문제

Once, during his informatics class, Dima had 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 numbers from 11 to nn occurs as a continuous subsegment in this sequence.

Dima quickly came up with an algorithm for generating superpermutations:

  • s_1=\[1]s\_1 = \[1].
  • Initially, set s_n+1s\_{n+1} equal to s_ns\_n.
  • Consider all subsegments of s_ns\_n of length nn from left to right, in order of increasing of their beginning.
  • If a current subsegment s_n\[l,l+1,,l+n1]s\_n\[l, l+1, \ldots, l+n-1] is a permutation of numbers from 11 to nn (that means that every number from 11 to nn occurs exactly once), then insert numbers n+1,s_n\[l],s_n\[l+1],,s_n\[l+n1]n + 1, s\_n\[l], s\_n\[l + 1], \ldots, s\_n\[l+n-1] into s_n+1s\_{n+1} right after the last element s_n\[l+n1]s\_n\[l+n-1] of the corresponding subsegment.

Let's take a look at how to construct superpermutations of order 11, 22, 33 and 44:

By definition, s_1=\[1]s\_1 = \[ 1 ].

Set s_2=\[1]s\_2 = \[ 1 ]. Consider the only subsegment of length 11 in s_1s\_1: \[1]\[1] is a permutation, so we insert \[2,1]\[2, 1] into s_2s\_2 after it. The result is s_2=\[1,2,1]s\_2 = \[1, \mathbf{2, 1}].

Set s_3=\[1,2,1]s\_3 = \[ 1, 2, 1 ]. Consider all subsegments of length 22 in s_2s\_2. \[1,2]\[1, 2] is a permutation, after inserting \[3,1,2]\[3, 1, 2], we get s_3=\[1,2,3,1,2,1]s\_3 = \[1, 2, \mathbf{3, 1, 2}, 1]. \[2,1]\[2, 1] is also a permutation, so we insert \[3,2,1]\[3, 2, 1] into s_3s\_3, we get s_3=\[1,2,3,1,2,1,3,2,1]s\_3 = \[1, 2, 3, 1, 2, 1, \mathbf{3, 2, 1}].

Initially, set s_4=\[1,2,3,1,2,1,3,2,1]s\_4 = \[ 1, 2, 3, 1, 2, 1, 3, 2, 1]. Consider all subsegments of length 33 in s_3s\_3:

  • \[1,2,3]\[1, 2, 3] is a permutation, so we insert \[4,1,2,3]\[4, 1, 2, 3] after it. Now s_4=\[1,2,3,4,1,2,3,1,2,1,3,2,1]s\_4 = \[ 1, 2, 3, \mathbf{4, 1, 2, 3}, 1, 2, 1, 3, 2, 1].
  • \[2,3,1]\[2, 3, 1] is a permutation, so we insert \[4,2,3,1]\[4, 2, 3, 1] after it. Now s_4=\[1,2,3,4,1,2,3,1,4,2,3,1,2,1,3,2,1]s\_4 = \[ 1, 2, 3, 4, 1, 2, 3, 1, \mathbf{4, 2, 3, 1}, 2, 1, 3, 2, 1].
  • \[3,1,2]\[3, 1, 2] is a permutation, so we insert \[4,3,1,2]\[4, 3, 1, 2] after it. Now s_4=\[1,2,3,4,1,2,3,1,4,2,3,1,2,4,3,1,2,1,3,2,1]s\_4 = \[ 1, 2, 3, 4, 1, 2, 3, 1, 4, 2, 3, 1, 2, \mathbf{4, 3, 1, 2}, 1, 3, 2, 1].
  • \[1,2,1]\[1, 2, 1] is not a permutation, so nothing happens here, we continue with the next subsegment.
  • \[2,1,3]\[2, 1, 3] is a permutation, so we insert \[4,2,1,3]\[4, 2, 1, 3] after it. Now s_4=\[1,2,3,4,1,2,3,1,4,2,3,1,2,4,3,1,2,1,3,4,2,1,3,2,1]s\_4 = \[ 1, 2, 3, 4, 1, 2, 3, 1, 4, 2, 3, 1, 2, 4, 3, 1, 2, 1, 3, \mathbf{4, 2, 1, 3}, 2, 1]. \item We do the same with two remaining permutations of length 3: \[1,3,2]\[1, 3, 2] and \[3,2,1]\[3, 2, 1].

Dima noticed that he came up with a pretty efficient way of constructing a superpermutation, because every permutation occurs exactly once. To make sure he did not make a mistake, Dima wants to find a position where a given permutation a_1,,a_na\_1, \dots, a\_n occurs in his superpermutation s_ns\_n. Positions are numbered starting with 1.

Since the length of s_ns\_n is huge, you need to find the index of the first element of s_ns\_n from which the occurrence of the given permutation starts, modulo 109+710^9 + 7.

입력

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

The second line contains nn integers a_1,a_2,,a_na\_1, a\_2, \ldots, a\_n --- the given permutation (1a_in1 \le a\_i \le n, all a_ia\_i are different).

출력

Output a single integer --- the position of the occurrence of the permutation a_1,a_2,,a_na\_1, a\_2, \ldots, a\_n in the superpermutation of order nn, modulo 109+710^9+7.