Problem:
Score : 1000 points

Problem Statement

You are given an integer sequence of length N: A_1,A_2,...,A_N. Let us perform Q operations in order.
The i-th operation is described by two integers X_i and Y_i. In this operation, we will choose one of the following two actions and perform it:

Swap the values of A_{X_i} and A_{Y_i}

Do nothing

There are 2^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 10^9+7.

Here, the inversion number of a sequence P_1,P_2,...,P_M is the number of pairs of integers (i,j) such that 1\leq i < j\leq M and P_i > P_j.

Constraints

1 \leq N \leq 3000

0 \leq Q \leq 3000

0 \leq A_i \leq 10^9(1\leq i\leq N)

1 \leq X_i,Y_i \leq N(1\leq i\leq Q)

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:

N Q
A_1
:
A_N
X_1 Y_1
:
X_Q Y_Q

Output

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

Sample Input 1

3 2
1
2
3
1 2
1 3

Sample Output 1

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,3, with the inversion number of 0.

Do nothing in the first operation, then perform the swap in the second. The final sequence would be 3,2,1, with the inversion number of 3.

Perform the swap in the first operation, then do nothing in the second. The final sequence would be 2,1,3, with the inversion number of 1.

Perform the swap, both in the first and second operations. The final sequence would be 3,1,2, with the inversion number of 2.

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

Sample Input 2

5 3
3
2
3
1
4
1 5
2 3
4 2

Sample Output 2

36

Sample Input 3

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

Sample Output 3

425

Solution in Rust (1.15.1) (judge verdict: Accepted)

// ${url}
//
#![allow(unused_imports)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, [ next / $t:tt ]) => {
        {
            let len = read_value!($iter, usize);
            (0..len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
        }
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

const MOD: i64 = 1e9 as i64 + 7;

fn get(u: usize, v: usize, qi: usize, dp: &mut Vec<Vec<i64>>, dp_last: &mut Vec<Vec<usize>>, p2: &Vec<i64>) -> i64 {
    assert!(u != v);
    assert!(qi >= dp_last[u][v]);
    let bai = qi - dp_last[u][v];
    dp[u][v] *= p2[bai];
    dp[u][v] %= MOD;
    dp_last[u][v] = qi;
    dp[u][v]
}

fn main() {
    input! {
        n: usize, q: usize,
        a: [i32; n],
        queries: [(usize1, usize1); q]
    };


    let mut dp = dvec!(0; n, n);
    let mut dp_last = dvec!(0; n, n);
    for i in 0..n {
        for j in 0..n {
            dp[i][j] = ifv!(a[i] < a[j], 1, 0);
        }
    }

    let mut p2 = vec![0; q+1];
    p2[0] = 1;
    for i in 0..q {
        p2[i+1] = p2[i] * 2 % MOD;
    }

    let mut tmpu = vec![0; n];
    let mut tmpv = vec![0; n];
    let mut cr = 0;
    for (u, v) in queries {
        // u,v
        tmpu[u] = 0;
        tmpv[v] = 0;

        let uv = get(u, v, cr, &mut dp, &mut dp_last, &p2);
        let vu = get(v, u, cr, &mut dp, &mut dp_last, &p2);
        tmpu[v] = (uv + vu) % MOD;
        tmpv[u] = (uv + vu) % MOD;

        for i in 0..n {
            if i != u && i != v {
                let ui = get(u, i, cr, &mut dp, &mut dp_last, &p2);
                let vi = get(v, i, cr, &mut dp, &mut dp_last, &p2);
                tmpu[i] = (ui + vi) % MOD;
                tmpv[i] = (ui + vi) % MOD;
                let iu = get(i, u, cr, &mut dp, &mut dp_last, &p2);
                let iv = get(i, v, cr, &mut dp, &mut dp_last, &p2);
                let nw = (iu + iv) % MOD;
                dp[i][u] = nw;
                dp[i][v] = nw;
                dp_last[i][u] = cr+1;
                dp_last[i][v] = cr+1;
            }
        }
        for i in 0..n {
            dp[u][i] = tmpu[i];
            dp[v][i] = tmpv[i];
            dp_last[u][i] = cr+1;
            dp_last[v][i] = cr+1;
        }
        cr += 1;
    }

    // debug!(dp);

    let mut ans = 0;
    for i in 0..n {
        for j in i+1..n {
            ans += get(j, i, q, &mut dp, &mut dp_last, &p2);
            ans %= MOD;
        }
    }



    println!("{}", ans);
}