Preparando MOJI

On Sum of Number of Inversions in Permutations

3000ms 262144K

Description:

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:

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

Output:

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

Sample Input:

2
2 1

Sample Output:

1

Sample Input:

3
2 1 3

Sample Output:

2

Note:

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 < j and pi > 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: AND (ai < bi).

Informação

Codeforces

Provedor Codeforces

Código CF396D

Tags

combinatoricsmath

Submetido 0

BOUA! 0

Taxa de BOUA's 0%

Datas 09/05/2023 08:49:17

Relacionados

Nada ainda