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.42.0) (judge verdict: Accepted)

#![allow(unused_imports)]
#![allow(clippy::many_single_char_names)]

// language: Rust(1.42.0)
// check available crates on AtCoder at "https://atcoder.jp/contests/language-test-202001"
// My Library Repo' is at "https://github.com/Loptall/prcn_lib"

/*
    6月で水色になるぞ〜!!!
*/

// from std...
use std::cmp::{
    max, min,
    Ordering::{Equal, Greater, Less},
    Reverse,
};
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
use std::mem::swap;
use std::ops::*;
use std::process::exit;
use std::{f32, f64, i128, i16, i32, i64, i8, isize, u128, u16, u32, u64, u8, usize};

// Dep' crates are...
use itertools::*;
use itertools_num::*;
use lazy_static::lazy_static;
use maplit::{btreemap, btreeset, hashmap, hashset};
use num_bigint::{BigInt, BigUint};
use num_complex::Complex;
use num_integer::{binomial, gcd, lcm, Integer};
use num_rational::Rational;
use num_traits::{one, zero, One, Zero};
use permutohedron::Heap;
use petgraph::unionfind::UnionFind;
use proconio::{fastout, input, is_stdin_empty, marker::*};
use rand::random;

pub const MOD: u128 = 1000000007;
pub const MAX: usize = 1usize << 63usize;
pub const MIN: i64 = i64::MIN;
pub const PI: f64 = f64::consts::PI;
pub const A: u8 = 'A' as u8;

/// 更新、区間和の取得がともに`O(log n)`で
/// 可能なデータ構造
#[derive(Debug, Clone)]
pub struct FenwickTree<T: Add> {
    len: usize,
    segment: Vec<T>,
}
impl<T: Add + Sub<Output = T> + Zero + Clone + Ord + Into<usize>> FenwickTree<T> {
    /// サイズを指定して、zerosで初期化
    pub fn new(n: usize) -> Self {
        Self {
            len: n,
            segment: vec![T::zero(); n + 1],
        }
    }
    /// 配列から構築
    pub fn from(vec: &[T]) -> Self {
        let m = vec.iter().max().unwrap().clone();
        let mut f = Self::new(m.into());
        for (i, v) in vec.iter().enumerate() {
            f.add(i, v.clone());
        }
        f
    }
    /// 扱える要素の最大値
    pub fn len(&self) -> usize {
        self.len
    }
    /// # 更新
    /// `i`番目の要素 += `v`
    ///
    /// `O(log n)`
    pub fn add(&mut self, i: usize, v: T) {
        let mut i = i + 1;
        while i <= self.len() {
            self.segment[i] = self.segment[i].clone() + v.clone();
            i += (i as i64 & -(i as i64)) as usize;
        }
    }
    /// `[0..i)`の区間和
    ///
    /// `O(log n)`
    pub fn sum(&self, mut i: usize) -> T {
        let mut s = T::zero();
        while i > 0 {
            s = s.clone() + self.segment[i].clone();
            i -= (i as i64 & -(i as i64)) as usize;
        }
        s
    }
    /// `[a..b)`の区間和
    ///
    /// `O(log n)`
    pub fn partial_sum(&self, a: usize, b: usize) -> T {
        self.sum(b) - self.sum(a)
    }
}
/// 配列の転倒数をSegmentTree<Sum>を利用してカウント
///
/// `O(n log n)`
///
/// #[test]
/// fn inversation_test() {
///     let v = vec![3, 1, 2];
///     assert_eq!(inversation(&v), 2);
///     let v = vec![3, 2, 1];
///     assert_eq!(inversation(&v), 3);
///     let v = vec![3, 3, 3];
///     assert_eq!(inversation(&v), 0);
///     let v = vec![1, 2, 4, 3, 6, 5];
///     assert_eq!(inversation(&v), 2);
/// }
pub fn inversation(v: &Vec<usize>) -> usize {
    let m = *v.iter().max().unwrap();
    let mut f = FenwickTree::new(m + 1);
    let mut t = 0usize;
    for i in 0..v.len() {
        t += i - f.sum(v[i] + 1);
        f.add(v[i], 1);
    }
    t
}

// code...
// #[fastout]
fn main() {
    input! {
        n: usize, k: u128,
        v: [usize; n]
    }

    let one = (inversation(&v) as u128 % MOD) as u128;
    let two = (inversation(&v.repeat(2)) as u128 % MOD) as u128;

    // dbg!(one, two);

    let ans = one * k % MOD + (two - one * 2) % MOD * (k * (k - 1) / 2) % MOD;
    println!("{}", ans % MOD);
}