3309.On Sum of Number of Inversions in Permutations

Time Limit: 1s Memory Limit: 256MB

You are given a permutation p. Calculate the total number of inversions in all permutations that lexicographically do not exceed the given one.

As this number can be very large, print it modulo 1000000007 (109+7).

Input Format(From the terminal/stdin)

The first line contains a single integer n (1 \le n \le 106) - the length of the permutation. The second line contains n distinct integers p1,p2,...,pn (1 \le pi \le n).

Output Format(To the terminal/stdout)

Print a single number - the answer to the problem modulo 1000000007 (109+7).

Sample Input 1

Copy
2
2 1
 \n
 · \n

Sample Output 1

Copy
1
 \n

Sample Input 2

Copy
3
2 1 3
 \n
 · · \n

Sample Output 2

Copy
2
 \n

Hints

Permutation p of length n is the sequence that consists of n distinct integers, each of them is from 1 to n.

An inversion of permutation p1,p2,...,pn is a pair of indexes (i,j), such that i \lt j and pi \gt pj.

Permutation a do not exceed permutation b lexicographically, if either a=b or there exists such number i, for which the following logical condition fulfills: 3309_1.png AND (ai \lt bi).

Submit

请先 登录

© 2025 FAQs