2134

Time limit: 0.8s Memory limit: 256MB Input: Output:

Task

Given a permutation P=[P0,P1,,PN1]P = [P_0, P_1, \ldots, P_{N-1}] of length NN, count the number of tuples 0a<b<c<d<N0 \leq a < b < c < d < N, where Pb<Pa<Pc<PdP_b < P_a < P_c < P_d. Since the answer can be large, print its remainder modulo 109+710^9 + 7.

Input data

The input file consists of:

  • a line containing integer NN.
  • a line containing the NN integers P0,,PN1P_{0}, \, \ldots, \, P_{N-1}.

Output data

The output file must contain a single line consisting of integer KK -- the number of such tuples modulo 109+710^9+7.

Constraints and clarifications

  • 1N500 0001 \le N \le 500 \ 000.
  • 1PiN1 \le P_i \le N for each i=0N1i=0\ldots N-1.
  • PiPjP_i \neq P_j for every 0i<j<N0\leq i < j < N.
# Score Constraints
1 0 Examples
2 30 N100N \le 100
3 30 N1 000N \le 1 \ 000
4 40 No additional restrictions

Example 1

stdin

5
2 1 3 4 5

stdout

3

Explanation

In the first sample case the following tuples meet the requirements: (0,1,2,3)(0, 1, 2, 3), (0,1,2,4)(0, 1, 2, 4), (0,1,3,4)(0, 1, 3, 4).

Example 2

stdin

10
4 5 1 2 8 7 9 3 6 10

stdout

27

Log in or sign up to be able to send submissions!