跳到主要内容
图灵 OJTURING / ONLINE JUDGE

#F2142. On Sum of Number of Inversions in Permutations

    ID: 2148 传统题 2000ms 256MiB 尝试: 0 已通过: 0 难度: (无) 上传者: 标签>组合数学数学难度待定难度共享题库Codeforces英文题面题目来源题面语言

On Sum of Number of Inversions in Permutations

题目描述

D. On Sum of Number of Inversions in Permutations
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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≤pin).

Output

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

Examples
Input
2
2 1
Output
1
Input
3
2 1 3
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).


题目来源:fps-www.educg.net-codeforce-1-2833.xml.zip;FPS 共享题包,题包内第 1743 题。保留原作者与原赛事署名。