Problem:
Score : 300 points

Problem Statement

We have a sequence of N integers A~=~A_0,~A_1,~...,~A_{N - 1}.

Let B be a sequence of K \times N integers obtained by concatenating K copies of A. For example, if A~=~1,~3,~2 and K~=~2, B~=~1,~3,~2,~1,~3,~2.

Find the inversion number of B, modulo 10^9 + 7.

Here the inversion number of B is defined as the number of ordered pairs of integers (i,~j)~(0 \leq i < j \leq K \times N - 1) such that B_i > B_j.

Constraints

All values in input are integers.

1 \leq N \leq 2000

1 \leq K \leq 10^9

1 \leq A_i \leq 2000

Input

Input is given from Standard Input in the following format:

N K
A_0 A_1 ... A_{N - 1}

Output

Print the inversion number of B, modulo 10^9 + 7.

Sample Input 1

2 2
2 1

Sample Output 1

3

In this case, B~=~2,~1,~2,~1. We have:

B_0 > B_1

B_0 > B_3

B_2 > B_3

Thus, the inversion number of B is 3.

Sample Input 2

3 5
1 1 1

Sample Output 2

0

A may contain multiple occurrences of the same number.

Sample Input 3

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

Sample Output 3

185297239

Be sure to print the output modulo 10^9 + 7.

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

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

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

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

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

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

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

    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };

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

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}
fn inversion(a: &mut [u32]) -> u64 {
    let n = a.len();
    if n <= 1 {
        return 0;
    }
    let m = n / 2;
    let mut ans = inversion(&mut a[0..m]);
    ans += inversion(&mut a[m..]);
    let mut tmp = vec![0; m];
    tmp.copy_from_slice(&a[0..m]);
    let mut i = 0;
    let mut j = m;
    let mut k = 0;
    while i < m && j < n {
        if tmp[i] > a[j] {
            ans += (m - i) as u64;
            a[k] = a[j];
            j += 1;
        } else {
            a[k] = tmp[i];
            i += 1;
        }
        k += 1;
    }
    while i < m {
        a[k] = tmp[i];
        i += 1;
        k += 1;
    }
    ans
}
fn main() {
    input!{
        n: usize,
        k: u64,
        a: [u32;n]
    }
    let MOD:u64 = 1_000_000_007;
    let mut b = a.clone();
    let mut c = a.clone();
    let mut a = &mut a.clone()[..];
    b.append(&mut c);
    // println!("{:?}", b);
    let mut b = &mut b.clone()[..];
    let ans0: u64 = inversion(&mut a);
    let ans1 = inversion(&mut b);
    let sec = ans1 - ans0*2;
    let ans = (((ans0%MOD)*(k%MOD))%MOD + (((k*(k-1)/2)%MOD)*(sec%MOD))%MOD)%MOD;
    // println!("{}", ans0);
    // println!("{}", ans1);
    println!("{}", ans);
}