atcoder#AGC030D. [AGC030D] Inversion Sum

[AGC030D] Inversion Sum

Score : 10001000 points

Problem Statement

You are given an integer sequence of length NN: A1,A2,...,ANA_1,A_2,...,A_N. Let us perform QQ operations in order. The ii-th operation is described by two integers XiX_i and YiY_i. In this operation, we will choose one of the following two actions and perform it:

  • Swap the values of AXiA_{X_i} and AYiA_{Y_i}
  • Do nothing

There are 2Q2^Q ways to perform these operations. Find the sum of the inversion numbers of the final sequences for all of these ways to perform operations, modulo 109+710^9+7.

Here, the inversion number of a sequence P1,P2,...,PMP_1,P_2,...,P_M is the number of pairs of integers (i,j)(i,j) such that 1i<jM1\leq i < j\leq M and Pi>PjP_i > P_j.

Constraints

  • 1N30001 \leq N \leq 3000
  • 0Q30000 \leq Q \leq 3000
  • 0Ai109(1iN)0 \leq A_i \leq 10^9(1\leq i\leq N)
  • 1Xi,YiN(1iQ)1 \leq X_i,Y_i \leq N(1\leq i\leq Q)
  • XiYi(1iQ)X_i\neq Y_i(1\leq i\leq Q)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN QQ

A1A_1

::

ANA_N

X1X_1 Y1Y_1

::

XQX_Q YQY_Q

Output

Print the sum of the inversion numbers of the final sequences, modulo 109+710^9+7.

3 2
1
2
3
1 2
1 3
6

There are four ways to perform operations, as follows:

  • Do nothing, both in the first and second operations. The final sequence would be 1,2,31,2,3, with the inversion number of 00.
  • Do nothing in the first operation, then perform the swap in the second. The final sequence would be 3,2,13,2,1, with the inversion number of 33.
  • Perform the swap in the first operation, then do nothing in the second. The final sequence would be 2,1,32,1,3, with the inversion number of 11.
  • Perform the swap, both in the first and second operations. The final sequence would be 3,1,23,1,2, with the inversion number of 22.

The sum of these inversion numbers, 0+3+1+2=60+3+1+2=6, should be printed.

5 3
3
2
3
1
4
1 5
2 3
4 2
36
9 5
3
1
4
1
5
9
2
6
5
3 5
8 9
7 9
3 2
3 8
425