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 Go (1.6) (judge verdict: Wrong Answer)

// Codeforces 1.12.6
// AtCoder 1.6
// Paiza 1.7.3

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type io struct {
	Scanner *bufio.Scanner
}

func (io *io) s() string {
	io.Scanner.Scan()
	return io.Scanner.Text()
}

func (io *io) i() int {
	i, _ := strconv.Atoi(io.s())
	return i
}

func (io *io) ss(n int) []string {
	a := make([]string, n)
	for i := range a {
		a[i] = io.s()
	}
	return a
}

func (io *io) is(n int) []int {
	a := make([]int, n)
	for i := range a {
		a[i] = io.i()
	}
	return a
}

func max(a int, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a int, b int) int {
	if a < b {
		return a
	}
	return b
}

func stdin() io {
	scn := bufio.NewScanner(os.Stdin)
	scn.Split(bufio.ScanWords)
	return io{Scanner: scn}
}

func main() {
	// WA

	io := stdin()
	n, k := io.i(), io.i()
	a := io.is(n)

	// solve here
	ans := 0
	for i := 0; i < n; i++ {
		bef := 0
		for j := 0; j < i; j++ {
			if a[i] > a[j] {
				bef++
			}
		}
		aft := 0
		for j := i + 1; j < n; j++ {
			if a[i] > a[j] {
				aft++
			}
		}
		ans += aft * k % 1000000007
		ans %= 1000000007
		ans += k * (k - 1) / 2 * (bef + aft) % 1000000007
		ans %= 1000000007
	}

	fmt.Println(ans)
}