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 (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
n,k = MAP()
a = LIST()
b = [0 for _ in range(n)] 
c = [0 for _ in range(n)] 
for i in range(n):
    for j in range(i+1, n):
        if a[i] > a[j]:
            b[i] += 1
for i in range(n):
    for j in range(n):
        if a[i] > a[j]:
            c[i] += 1
tt = k * (k - 1) // 2
ans = (k * sum(b) % mod + tt * sum(c) % mod) % mod
print(ans)

Solution (Python):
from bisect import bisect_right

mod = 10**9+7
N, K = map(int, input().split())
A = list(map(int, input().split()))
A_front = []
A_sorted = sorted(A)
ans = 0
for a in A:
    ans += (N - bisect_right(A_sorted, a)) * K*(K-1)//2
    ans %= mod
    i = bisect_right(A_front, a)
    ans += (len(A_front) - i) * K
    ans %= mod
    A_front.insert(i, a)
print(ans)

Solution (Python):
N,K = map(int,input().split())
A = list(map(int,input().split()))

t1 = 0
for i in range(N-1):
    for j in range(i+1,N):
        if A[i] > A[j]:
            t1 += 1

t2 = 0
for i in range(N-1):
    for j in range(i+1,N):
        if A[i] < A[j]:
            t2 += 1

ans = t1 * (K*(K+1)//2) + t2 * (K*(K-1)//2)
print(ans % (10**9 + 7))

Solution (Python):
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
class BIT:
  def __init__(self, n):
    self.n = n
    self.data = [0] * (n + 1)
    self.el = [0] * (n + 1)
  def sum(self, i):
    s = 0
    while i > 0:
      s += self.data[i]
      i -= i & -i
    return s
  def add(self, i, x):
    self.el[i] += x
    while i <= self.n:
      self.data[i] += x
      i += i & -i
  def get(self, i, j = None):
    if j is None:
      return self.el[i]
    return self.sum(j) - self.sum(i)

fwk = BIT(2001)
res = 0
for i in range(N):
  res += fwk.get(a[i], 2001)
  res %= mod
  fwk.add(a[i], 1)
res *= K
res %= mod

for i in range(N):
  res += (K * (K - 1) // 2) % mod * fwk.get(a[i], 2001) % mod
  res %= mod
print(res)

Solution (Python):
def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y


def modinv(a, m):
    g, x, y = egcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m




def combination(n, r, mod=10 ** 9 + 7):
    r = min(r, n - r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i + 1, mod) % mod
    return res

n, k = map(int, input().split())
dat = list(map(int, input().split()))
datAfter = [0] * n
datSelf = [0] * n
numAfter = 0
numSelf = 0
m = 10**9 + 7
for i in range(n):
    for j in range(i+1, n):
        if dat[i] > dat[j]:
            numAfter += 1
for i in range(n):
    for j in range(n):
        
        if dat[i] > dat[j]:
            numSelf += 1
res = 0
res += numAfter * k
res += numSelf * combination(k, 2)
res %= m

print(res)

Solution (Python):
def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y


def modinv(a, m):
    g, x, y = egcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m




def combination(n, r, mod=10 ** 9 + 7):
    r = min(r, n - r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i + 1, mod) % mod
    return res

n, k = map(int, input().split())
dat = list(map(int, input().split()))
datAfter = [0] * n
datSelf = [0] * n
numAfter = 0
numSelf = 0
m = 10**9 + 7
for i in range(n):
    for j in range(i+1, n):
        if dat[i] > dat[j]:
            numAfter += 1
for i in range(n):
    for j in range(n):
        
        if dat[i] > dat[j]:
            numSelf += 1
res = 0
res += numAfter * k
res += numSelf * combination(k, 2)
res %= m

print(res)

Solution (Python):
N, K=map(int, input().split())
A=list(map(int, input().split()))
A+=A
tmp=[0]*2001
S1=[0]*N
S2=[0]*N
for i in range(1, N):
  tmp[A[i-1]]+=1
  S1[i]=sum(tmp[A[i]+1:])
tmp[A[-1]]+=1
S2[0]=sum(tmp[A[0]+1:])
for i in range(1, N):
  tmp[A[i-1]]+=1
  S2[i]=sum(tmp[A[i]+1:])
S2=[S2[i]-S1[i] for i in range(N)]
print((K*sum(S1)+sum(S2)*K*(K-1)//2)%(10**9+7))

Solution (Python):
mod = 10 ** 9 + 7
N, K = map(int, input().split())
A = list(map(int, input().split()))

cnt_list = [[0, 0] for _ in range(N)]
for i in range(N):
    right = 0
    left = 0
    for j in range(N):
        if i < j:       
            if A[i] > A[j]:
                right += 1
        else:           
            if A[i] > A[j]:
                left += 1
    cnt_list[i][0] = left
    cnt_list[i][1] = right
ans = 0
for i in range(N):
    left = cnt_list[i][0]
    right = cnt_list[i][1]
    ans += left * (K - 1) * K // 2
    ans += right *  K * (K + 1) // 2
    ans %= mod
print(ans)

Solution (Python):
n,k=map(int,input().split())
*a,=map(int,input().split())
p=10**9+7
cnt=0
ans1=0
for i in range(n):
    x=[j for j in a[:i] if j>a[i]]
    ans1+=len(x)
a=sorted(a)
un=0
for i in range(n):
  y=[j for j in a[i+1:] if j>a[i]]
  un+=len(y)
k%=p
ans=ans1*k+(k*(k-1)//2)*un
print(ans%p)

Solution (Python):
N,K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
mod = 10**9+7
ans =0
for i in range(N):
    df = 0
    db = 0
    for j in range(i):
        if A[j] < A[i]:
            df +=1
    for k in range(i+1,N):
        if A[k] < A[i]:
            db +=1
    D = K*(K+1)//2
    L = D * (df+db) - K * df
    L %= mod
    ans +=L
    ans %= mod
print(ans)

Solution (Python):
import sys
from bisect import bisect_right
n, k, *a = map(int, sys.stdin.read().split())

mod = 1000000007
ans = 0
for i in range(n-1):
    for j in range(i+1, n):
        if a[i] > a[j]:
            ans += 1
ans = k * ans % mod
a.sort()
cnt = 0
for ai in a:
    cnt += n - bisect_right(a, ai)
ans += (k * (k - 1) // 2) % mod * cnt
print(ans % mod)

Solution (Python):
import sys
from bisect import bisect_right
n, k, *a = map(int, sys.stdin.read().split())

mod = 1000000007
ans = 0
for i in range(n-1):
    for j in range(i+1, n):
        if a[i] > a[j]:
            ans += 1
ans = k * ans % mod
a.sort()
cnt = 0
for ai in a:
    cnt += n - bisect_right(a, ai)
ans += (k * (k - 1) // 2) % mod * cnt
print(ans % mod)

Solution (Python):
import sys
input=sys.stdin.readline
n,k = map(int, input().split())
a = list(map(int, input().split()))
soa = sorted(a)
import bisect
ans=0
for i in range(len(a)):
    cnt = bisect.bisect_left(soa,a[i])
    slf=0
    for j in range(i,len(a)):
        if a[i] > a[j]:
            slf+=1
    sm = k*(k-1)*cnt // 2 + slf*k
    ans+=sm
mod=10**9+7
print(ans%mod)

Solution (Python):
class BinaryIndexedTree:
    def __init__(self, N):
        self.N = N
        self.bit = [0] * (self.N+1)

    def sum(self, i):
        res = 0
        while i > 0:
            res += self.bit[i]
            i -= i & -i
        return res

    def add(self, i, x):
        while i <= self.N:
            self.bit[i] += x
            i += i & -i

MOD=10**9+7
class Fp(int):
    def __new__(self,x=0):return super().__new__(self,x%MOD)
    def inv(self):return self.__class__(super().__pow__(MOD-2,MOD))
    def __add__(self,value):return self.__class__(super().__add__(value))
    def __sub__(self,value):return self.__class__(super().__sub__(value))
    def __mul__(self,value):return self.__class__(super().__mul__(value))
    def __floordiv__(self,value):return self.__class__(self*self.__class__(value).inv())
    def __pow__(self,value):return self.__class__(super().__pow__(value%(MOD-1), MOD))
    __radd__=__add__
    __rmul__=__mul__
    def __rsub__(self,value):return self.__class__(-super().__sub__(value))
    def __rfloordiv__(self,value):return self.__class__(self.inv()*value)
    def __iadd__(self,value):self=self+value;return self
    def __isub__(self,value):self=self-value;return self
    def __imul__(self,value):self=self*value;return self
    def __ifloordiv__(self,value):self=self //value;return self
    def __ipow__(self,value):self=self**value;return self
    def __neg__(self):return self.__class__(super().__neg__())

N,K,*A=map(int, open(0).read().split())
bit=BinaryIndexedTree(2001)
c=0
for i,a in enumerate(A):
    c+=i-bit.sum(a)
    bit.add(a,1)
d=0
for i,a in enumerate(A):
    d+=N-bit.sum(a)
K=Fp(K)
print(K*c+K*(K-1)//2*d)

Solution (Python):
n, k = map(int, input().split())
A = list(map(int, input().split()))
counts = [[0, 0] for _ in range(n)]
for i in range(n):
  for j in range(n):
    if A[i] < A[j]:
      if i > j:
        counts[i][0] += 1
      if i < j:
        counts[i][1] += 1

total = 0
for i in range(n):
  total += counts[i][0] * (k * (k + 1) // 2) + counts[i][1] * (k * (k - 1) // 2)
  total %= 1000000007

print(total)

Solution (Python):

import sys

MOD = 1000000007  


def solve(N: int, K: int, A: "List[int]"):
    al = [0] * N
    bl = [0] * N
    for i in range(N):
        for j in range(N):
            if i == j:
                continue
            elif A[i] > A[j]:
                al[i] += 1
                if i < j:
                    bl[i] += 1

    ans = 0
    for i in range(N):
        ans += ((K*(K-1)//2)*al[i] + K*bl[i]) % MOD

    
    print(ans%MOD)
    
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N - 1 - 0 + 1)]  
    solve(N, K, A)

if __name__ == '__main__':
    main()

Solution (Python):
N,K=map(int,input().split())
A=list(map(int,input().split()))
cnt0=0
cnt1=0
for i in range(N):
  for j in range(N):
    if A[i]>A[j] and j>=i:
      cnt0+=1
    if A[i]>A[j]:
      cnt1+=1
print((cnt1*(K-1)*K//2+cnt0*K)%(10**9+7))

Solution (Python):

N, K = map(int, input().split())
mod = 10**9 + 7
A = list(map(int, input().split()))
l = []
for i in range(N):
    tmp = [0, 0]
    for j in range(N):
        if j < i:
            if A[i] < A[j]:
                tmp[0] += 1
        else:
            if A[i] < A[j]:
                tmp[1] += 1
    l.append(tmp[:])
ans = 0
for i in range(N):
    ans += K * ((l[i][0] * (K+1)) + (l[i][1]*(K-1))) // 2
    ans %= mod
print(ans % mod)

Solution (Python):

N, K = map(int, input().split())
mod = 10**9 + 7
A = list(map(int, input().split()))
l = []
for i in range(N):
    tmp = [0, 0]
    for j in range(N):
        if j < i:
            if A[i] < A[j]:
                tmp[0] += 1
        else:
            if A[i] < A[j]:
                tmp[1] += 1
    l.append(tmp[:])
ans = 0
for i in range(N):
    ans += K * ((l[i][0] * (K+1)) + (l[i][1]*(K-1))) // 2
    ans %= mod
print(ans % mod)

Solution (Python):

N, K = map(int, input().split())
mod = 10**9 + 7
A = list(map(int, input().split()))
l = []
for i in range(N):
    tmp = [0, 0]
    for j in range(N):
        if j < i:
            if A[i] < A[j]:
                tmp[0] += 1
        else:
            if A[i] < A[j]:
                tmp[1] += 1
    l.append(tmp[:])
ans = 0
for i in range(N):
    ans += l[i][0]*K*(K+1) // 2
    ans %= mod
    ans += l[i][1] * K * (K - 1) // 2
    ans %= mod
print(ans % mod)

Solution (Python):
def main():
  import sys
  n,k=map(int,input().split())
  a=tuple(map(int,sys.stdin.readline().split()))
  
  def count(arr,now):
    return sum(element<now for element in arr)
  
  ans=0
  prime=10**9+7
  k=k%prime
  for i in range(n):
    left=count(a[:i],a[i])
    right=count(a[i+1:],a[i])
    res=(k-1)*k//2*(right+left)+right*k
    ans+=res%prime
    
  print(ans%prime)

if __name__=="__main__":
  main()

Solution (Python):
def main():
  import sys
  n,k=map(int,input().split())
  a=tuple(map(int,sys.stdin.readline().split()))
  
  count=lambda arr,now:sum(element<now for element in arr)
  
  ans=0
  prime=10**9+7
  k=k%prime
  for i in range(n):
    left=count(a[:i],a[i])
    right=count(a[i+1:],a[i])
    res=(k-1)*k//2*(right+left)+right*k
    ans+=res%prime
    
  print(ans%prime)

if __name__=="__main__":
  main()

Solution (Python):
def main():
  
  n,k=map(int,input().split())
  a=tuple(map(int,input().split()))
  
  count=lambda arr,now:sum(element<now for element in arr)
  
  ans=0
  prime=10**9+7
  
  for i in range(n):
    left=count(a[:i],a[i])
    right=count(a[i+1:],a[i])
    res=(k-1)*k//2*(right+left)+right*k
    ans+=res%prime
    
  print(ans%prime)

if __name__=="__main__":
  main()

Solution (Python):
def main():
  
  n,k=map(int,input().split())
  a=list(map(int,input().split()))
  
  count=lambda arr,now:sum(element<now for element in arr)
  
  ans=0
  prime=10**9+7
  
  for i in range(n):
    left=count(a[:i],a[i])
    right=count(a[i+1:],a[i])
    res=(k-1)*k//2*(right+left)+right*k
    ans+=res%prime
    
  print(ans%prime)

if __name__=="__main__":
  main()

Solution (Python):
def main():
  
  n,k=map(int,input().split())
  a=list(map(int,input().split()))
  
  count=lambda arr,now:sum(element<now for element in arr)
  b=[[count(a[:i],a[i]),count(a[i+1:],a[i])] for i in range(n)]

  ans=0
  prime=10**9+7
  
  for x in b:
    left=x[0]
    right=x[1]
    res=(k-1)*k//2*(right+left)+right*k
    ans+=res%prime
    
  print(ans%prime)

if __name__=="__main__":
  main()

Solution (Python):
N,K = list(map(int,input().split()))
A = list(map(int,input().split()))
mod = 10**9 + 7
asum = 0
for i in range(N-1):
    for j in range(i+1,N):
        if A[i] > A[j]:
            asum += 1
asum = (asum*K)%mod
bsum = 0
for i in range(N):
    for j in range(N):
        if A[i] > A[j]:
            bsum += 1
bsum = (bsum*K*(K-1)//2)%mod
print((asum+bsum)%mod )

Solution (Python):
n,k = map(int,input().split())
a = [int(i) for i in input().split()]
mod = 10**9+7
x = 0
y = 0

for i in range(n):
    for j in range(n):
        if a[i] > a[j]:
            x += 1
            if i < j:
                y += 1

ans = (x*k*(k-1)//2%mod + y*k%mod)%mod
print(ans)

Solution (Python):
n,k = map(int,input().split())
a = [int(i) for i in input().split()]
ans = 0
m = max(a)+2
cnt1 = [0]*m
cnt2 = [0]*n
mod = 10**9+7

for i in range(n-1):
    cnt1[a[i]+1] += 1
    for j in range(i+1,n):
        if a[i] > a[j]: cnt2[i] += 1
cnt1[a[n-1]+1] += 1

for i in range(1,len(cnt1)):
    cnt1[i] += cnt1[i-1]

for i in range(n):
    ans = (ans + cnt1[a[i]]*(k*(k-1))//2%mod + cnt2[i]*k%mod)%mod

print(ans)

Solution (Python):
N,K = map(int,input().split())
A = list(map(int,input().split()))

P = 10**9+7

ans1 = 0
ans2 = 0
for i in range(N):
    for j in range(N):
        if A[i] > A[j]:
            if i < j:
                ans1 += 1
            elif i > j:
                ans2 += 1

K1 = (K*(K+1)//2)%P
K2 = (K*(K-1)//2)%P

ans = (ans1*K1+ans2*K2)%P

print(ans)

Solution (Python):
N,K = map(int,input().split())
A = list(map(int,input().split()))

P = 10**9+7

ans1 = 0
ans2 = 0
for i in range(N):
    for j in range(N):
        if i == j:
            continue
        if A[i] > A[j]:
            if i < j:
                ans1 += 1
            else:
                ans2 += 1

K1 = (K*(K+1)//2)%P
K2 = (K*(K-1)//2)%P

ans = (ans1*K1+ans2*K2)%P

print(ans)

Solution (Python):
N,K = map(int,input().split())
A = list(map(int,input().split()))

P = 10**9+7

ans = 0
for i in range(N):
    for j in range(N):
        if i == j:
            continue
        if A[i] > A[j]:
            if i < j:
                ans += K*(K+1)//2
                ans %= P
            else:
                ans += K*(K-1)//2
                ans %= P
print(ans)

Solution (Python):

import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def roundUp(a, b):
        return -(-a // b)

    @staticmethod
    def toUpperMultiple(a, x):
        return Math.roundUp(a, x) * x

    @staticmethod
    def toLowerMultiple(a, x):
        return (a // x) * x

    @staticmethod
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

    @staticmethod
    def sign(n):
        if n == 0:
            return 0
        if n < 0:
            return -1
        return 1

    @staticmethod
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


class PriorityQueue:
    def __init__(self, l=[]):
        self.__q = l
        heapq.heapify(self.__q)
        return

    def push(self, n):
        heapq.heappush(self.__q, n)
        return

    def pop(self):
        return heapq.heappop(self.__q)


MOD = int(1e09) + 7
INF = int(1e15)


def calc(N):
    return sum(int(x) for x in str(N))


def main():
    
    N, K = Scanner.map_int()
    A = Scanner.map_int()
    ans = 0
    for i in range(N):
        c = 0
        for j in range(i):
            if A[i] > A[j]:
                c += 1
        ans += ((K - 1) * (K) // 2) * c
        ans %= MOD
        c = 0
        for j in range(i+1, N):
            if A[i] > A[j]:
                c += 1
        ans += (K * (K + 1) // 2) * c
        ans %= MOD
    print(ans)
    return


if __name__ == "__main__":
    main()

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
cnt = 0
for i in range(n):
  rightsmallcnt = 0
  for j in range(i+1, n):
    if a[i] > a[j]:
      rightsmallcnt += 1
  allsmallcnt = 0
  for j in range(n):
    if a[i] > a[j]:
      allsmallcnt += 1
  cnt += rightsmallcnt * k + allsmallcnt * (((k-1)**2+(k-1))//2)
print(cnt % (10**9+7))

Solution (Python):
N, K = map(int, input().split())
Alist = list(map(int, input().split()))
ans = 0

for i in range(N):
    a, b = 0, 0
    A = Alist[i]
    for j in range(N):
        if Alist[j] < A:
            b += 1
            if j > i:
                a += 1
    ans += a * K + b * (K**2 - (K * (K + 1)) // 2)
    
print(ans % (10**9 + 7))

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
t, s = 0, 0

for i in range(n-1):
  for j in range(i+1, n):
    if a[i] > a[j]:
      t += 1
      
a.sort()
l = [ i for i in range(n) ]
for i in range(1, n):
  if a[i] == a[i-1]:
    l[i] = l[i-1]
s = sum(l)
    
print((k*t%1000000007 + s*k*(k-1)//2%1000000007)%1000000007)

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
t, s = 0, 0

for i in range(n-1):
  for j in range(i+1, n):
    if a[i] > a[j]:
      t += 1
      
a.sort()
l = [ i for i in range(n) ]
for i in range(1, n):
  if a[i] == a[i-1]:
    l[i] = l[i-1]
s = sum(l)
    
print((k*t%1000000007 + s*k*(k-1)//2%1000000007)%1000000007)

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))

cnt=0
a=a*2
l=[[0,0] for i in range(n)]
for i in range(n):
    for j in range(i+1,n+i):
        if a[i]>a[j] and j<=n-1:
            l[i][0]+=1
        elif a[i]>a[j] and j>n-1:
            l[i][1]+=1

for i in range(n):
    num=l[i][0]
    num1=l[i][1]
    cnt+=num*k*(k+1)//2+num1*(k-1)*k//2

print(cnt%(10**9+7))

Solution (Python):
div = 10 ** 9 + 7

N, K = map(int, input().split())
A = list(map(int, input().split()))
count = 0
ans = 0

for i in range(N):
    for j in range(N):
        if A[i] > A[j]:
            ans += (i < j)
            count += 1

ans *= K
ans += count * (K - 1) * K // 2
ans %= div

print(ans)

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))

x = 0
y = 0
for i in range(n):
    for j in range(n):
        if a[i] > a[j]:
            if i < j:
                x += 1
            else:
                y += 1

print((x*(k**2-((k-1)*k//2)) + y*(k**2-((1+k)*k//2))) % (10**9+7))

Solution (Python):
import numpy as np
N, K = [int(i) for i  in input().split()]
A= [int(i) for i  in input().split()]
A=np.array(A)
ans =0
for i in range(N):
    ans += np.count_nonzero(A[:i]<A[i])*K*(K-1)//2
    ans += np.count_nonzero(A[i+1:]<A[i])*K*(K+1)//2

print(ans%1000000007)

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))

s = 0
t = 0
for i in range(N):
    for j in range(N):
        if A[i] > A[j]:
            s += 1
            if i < j:
                t += 1

k = K * (K - 1) // 2
res = s * k + t * K
print(res % (10 ** 9 + 7))

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
mod = 10 ** 9 + 7
counter = 0
for i in range(N-1):
    for j in range(i+1, N):
        if A[i] > A[j]:
            counter += 1

counter2 = 0
for i in range(N):
    for j in range(N):
        if A[i] > A[j]:
            counter2 += 1

ans = 0
ans += counter * K
ans += counter2 * (K * (K - 1) // 2)
print(ans % mod)

Solution (Python):
import bisect

n,k = map(int,input().split())
lst = list(map(int,input().split()))
mod = 10**9+7

order = sorted(lst)

ans = 0
for i in range(n):
    cnt = 0
    x = lst[i]
    for j in range(i+1,n):
        y = lst[j]
        if x>y:
            cnt += 1
    ans += cnt*k + (k*(k-1)//2)*bisect.bisect_left(order, x)
    ans %= mod
print(ans%mod)

Solution (Python):
n, k = map(int, input().split())
A = list(map(int, input().split()))

cnt = [[0, 0] for i in range(n)]
for i in range(n):
    a = A[i]
    for af in A[:i]:
        if af < a:
            cnt[i][0] += 1
    for al in A[i:]:
        if al < a:
            cnt[i][0] += 1
            cnt[i][1] += 1

ans = 0
for c in cnt:
    ans += (c[1]*k + c[0]*k*(k-1)//2) % (10**9+7)

print(ans % (10**9+7))

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
MOD = 10 ** 9 + 7

inv = 0
sma = 0
for i in range(N):
    for j in range(N):
        if A[i] > A[j]:
            sma += 1
            if i < j:
                inv += 1

print((inv * K + sma * K * (K - 1) // 2) % MOD)

Solution (Python):
N,K=map(int,input().split())
A=list(map(int,input().split()))
a=0
b=0
for i in range(N):
  for j in range(i+1,N):
    if A[i]>A[j]:
      a+=1
for i in range(N):
  for j in range(N):
    if A[i]>A[j]:
      b+=1
a*=K
b*=K*(K-1)//2
a%=(10**9+7)
b%=(10**9+7)
print((a+b)%(10**9+7))

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))

cnt1 = 0
cnt2 = 0
mod = 10**9+7
for i in range(0, n):
    for j in range(i+1, n):
        if a[i] > a[j]:
            cnt1 += 1
        elif a[i] < a[j]:
            cnt2 += 1
print(((k*(k+1)//2 % mod) * cnt1 % mod + ((k-1)*k//2 % mod)*cnt2 % mod) % mod)

Solution (Python):
n, k = map(int, input().split(' '))
a_list = list(map(int, input().split(' ')))
MOD = 10 ** 9 + 7

cnt = 0
cnt_ = 0
for i in range(n):
  cnt_ += len(list(k for k in a_list if k > a_list[i]))
  for j in range(i+1,n):
    if a_list[i] > a_list[j]:
      cnt += 1

ans = (cnt_) * (k * (k-1) // 2) % MOD + (cnt * k) % MOD

print(ans % MOD)

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
cntin=0
cntot=0
mod=10**9+7
for i in range(n-1):
  for j in range(i+1,n):
    if a[i]<a[j]:
      cntot+=1
    elif a[i]>a[j]:
      cntin+=1
print((cntin*((k*(k+1))//2)%mod+cntot*((k*(k-1))//2)%mod)%mod)

Solution (Python):
from itertools import combinations, accumulate
n,m = map(int,input().split())
a = list(map(int,input().split()))
mod = 10**9+7
ans = 0
ls = [0]*2001
for i in a:
  ls[i] += 1
accls = list(accumulate(ls))
for i,j in combinations(a,2):
  if i>j:
    ans += 1
ans = ans*m%mod
for i in range(1,2001):
  if ls[i]>=1:
    ans = (ans + ls[i]*accls[i-1]*m*(m-1)//2)%mod
print(ans)

Solution (Python):
import sys
import math
import functools
stdin = sys.stdin
mod = 10**9 + 7

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
sa = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]

n, k = na()
a = na()
cl = 0
cr = 0
for i in range(n-1):
    for j in range(i+1,n):
        if a[i] < a[j]:
            cl += 1
        if a[i] > a[j]:
            cr += 1
ans = (cr*k %mod + (cl+cr) * k*(k-1)//2) % mod
print(ans)

Solution (Python):
n,k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10**9+7
inside = 0
outside = 0

for i in range(n-1):
	for j in range(i,n):
		if a[i] > a[j]:
			inside += 1

for i in range(n):
	for j in range(n):
		if a[i] < a[j]:
			outside += 1

inside %= mod
outside %= mod

ans = 0

ans += (inside*k)%mod
ans += ((1+k-1)*(k-1)*pow(2,mod-2,mod)*outside)%mod

print(ans % mod)

Solution (Python):
import sys
input = sys.stdin.readline


def main():
    mod = 10**9+7
    N, K = map(int, input().split())
    A = list(map(int, input().split()))

    x = 0
    for i in range(N-1):
        for j in range(i+1, N):
            if A[i] > A[j]:
                x += 1
    y = 0
    for i in range(N):
        for j in range(N):
            if A[i] > A[j]:
                y += 1
    ans = x * K + y * K * (K-1) // 2
    ans %= mod
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n,k = LI()
    a = LI()
    r = 0
    for i in range(n):
        for j in range(i+1,n):
            if a[i] > a[j]:
                r += 1
    r *= k
    r %= mod
    u = [0] * (max(a)+1)
    for c in a:
        u[c] += 1
    for i in range(len(u)-1):
        u[i+1] += u[i]
    kk = (k-1) * k // 2 % mod
    for c in a:
        r += u[c-1] * kk
        r %= mod

    return r


print(main())

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))


class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i


def inversion(a):
    "転倒数を求める"
    d = {av: i+1 for i, av in enumerate(sorted(a))}
    b = Bit(len(a))
    ans = 0
    for i, av in enumerate(a):
        ans += i-b.sum(d[av])
        b.add(d[av], 1)
    return ans


MOD = 10**9+7
L = len(set(A))

count = 0
for i in range(N):
    for j in range(N):
        if A[i] > A[j]:
            count += 1
ans = (inversion(A) * K) % MOD + (K*(K-1)//2) * count % MOD
ans %= MOD
print(ans)

Solution (Python):
from sys import exit, stdin
N, K = [int(_) for _ in stdin.readline().rstrip().split()]
A = [int(_) for _ in stdin.readline().rstrip().split()]

ans_one = 0
for i in range(N):
    for j in range(i+1, N):
        if A[i] > A[j]:
            ans_one += 1
ans_two = 0
for i in range(N):
    for j in range(N):
        if A[i] > A[j]:
            ans_two += 1

ans = (ans_one * K) + (K * (K-1) // 2 * ans_two)
print(ans % (10**9+7))

Solution (Python):
n,k = map(int,input().split())
al = list(map(int,input().split()))

onel = []

for i in range(n):
   p = 0
   for j in range(i+1, n):
      if al[i] > al[j]:
         p += 1
   onel.append(p)

alll = []
for i in range(n):
   p = 0
   for a in al:
      if al[i] > a:
         p += 1
   alll.append(p)

ans = 0
for i, j in zip(onel, alll):
   ans += k*i + (k-1)*k*j//2
   ans %= 10**9 + 7
print(ans)

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))

mod = 10 ** 9 + 7

ib = 0
for i in range(1,N):
    for j in range(0,i):
        if A[j] > A[i]:
            ib += 1

B = sorted(A)[::-1]
ob = 0
t = B[0]
c = 0
for i in range(N):
    if t > B[i]:
        t = B[i]
        c = i
        ob += c
    else:
        ob += c
        
ans = (((ib * K) % mod) + (ob * (((K * (K-1)) // 2) % mod)) % mod) % mod
print(ans)