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)

use std::io::prelude::*;

fn input<T: std::str::FromStr>() -> T {
    let stdin = std::io::stdin();
    let token: String = stdin
        .lock()
        .bytes()
        .map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().unwrap()
}

#[allow(dead_code)]
fn with_stdout_lock<F: FnOnce(std::io::BufWriter<std::io::StdoutLock>)>(f: F) {
    let out = std::io::stdout();
    let w = std::io::BufWriter::new(out.lock());
    f(w);
}

#[allow(unused_macros)]
macro_rules! debug {
    ($var:expr) => {
        match &$var { v => eprintln!(concat!(stringify!($var), "={:?}"), v) }
    };
    ($var:expr, $($vars:expr),*) => {
        match &$var { v => eprint!(concat!(stringify!($var), "={:?} "), v) }
        debug!($($vars),*);
    };
}

fn main() {
    let n: usize = input();
    let k: i64 = input();
    let a: Vec<i32> = (0..n).map(|_| input()).collect();
    const MOD: i64 = 1000000007;

    let mut bit = FenwickTree::new(2010);
    let mut t = 0;
    for i in 0..n {
        let j = bit.sum(a[i] as usize);
        bit.add(a[i] as usize, 1);
        t += i as i64 - j;
    }

    let mut b = a.clone();
    b.sort();

    let mut ans = (t * k) % MOD;
    for i in 0..n {
        let c = b.lower_bound(&a[i]) as i64;
        ans += (k * (k - 1) / 2 % MOD) * c % MOD;
        ans %= MOD;
    }
    println!("{}", ans);
}

trait LowerBound<T: Ord> {
    fn lower_bound_by<F: FnMut(&T) -> std::cmp::Ordering>(&self, f: F) -> usize;
    fn lower_bound(&self, x: &T) -> usize {
        self.lower_bound_by(|e| e.cmp(x))
    }
    fn lower_bound_by_key<U: Ord, F: FnMut(&T) -> U>(&self, x: &U, mut f: F) -> usize {
        self.lower_bound_by(|e| f(e).cmp(x))
    }
}
impl<T: Ord> LowerBound<T> for [T] {
    fn lower_bound_by<F: FnMut(&T) -> std::cmp::Ordering>(&self, mut f: F) -> usize {
        let mut l = -1 as i64;
        let mut r = self.len() as i64;
        while r - l > 1 {
            let m = ((l + r) / 2) as usize;
            if f(&self[m]) == std::cmp::Ordering::Less {
                l = m as i64;
            } else {
                r = m as i64;
            }
        }
        r as usize
    }
}
struct FenwickTree(Vec<i64>);
#[allow(dead_code)]
impl FenwickTree {
    fn new(n: usize) -> FenwickTree {
        FenwickTree(vec![0; n + 1])
    }
    fn add(&mut self, i: usize, v: i64) {
        assert!(i > 0);
        let n = self.0.len() as i64;
        let mut i = i as i64;
        while i < n {
            self.0[i as usize] += v;
            i += i & -i;
        }
    }
    fn sum(&self, i: usize) -> i64 {
        assert!(i > 0);
        let mut i = i as i64;
        let mut r = 0;
        while i > 0 {
            r += self.0[i as usize];
            i -= i & -i;
        }
        r
    }
    fn lower_bound(&self, x: &i64) -> usize {
        let n = self.0.len();
        let mut k = 1;
        while k * 2 < n {
            k *= 2;
        }
        let mut i = 0;
        let mut x = *x;
        while k > 0 {
            if i + k < n && self.0[i + k] < x {
                x -= self.0[i + k];
                i += k;
            }
            k /= 2;
        }
        i + 1
    }
}