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):
n,k=map(int,input().split())
a=list(map(int,input().split()))
after=0
before=0
for i in range(n):
  for j in range(i+1,n):
    if a[i] > a[j]:
      after += 1
    elif a[i] < a[j]:
      before += 1
print((after*k*(k+1)//2 + before*k*(k-1)//2)%(10**9+7))

Solution (Python):
import sys

readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

MOD = 10 ** 9 + 7

N, K = map(int, readline().split())
A = list(map(int, readline().split()))

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

ans = K * tento1 + (K * (K - 1)) // 2 * tento2
ans = ans % MOD
print(ans)

Solution (Python):
n, k = map(int, input().split())
a = [int(_) for _ in input().split()]
MOD = 10**9+7

cnt = 0
for i in range(n):
    for j in range(i+1, n):
        if a[i] > a[j]:
            cnt += 1
ans = cnt * k

dic = dict()
for i in range(n):
    if a[i] in dic:
        dic[a[i]] += 1
    else:
        dic.setdefault(a[i], 1)
dic = sorted(dic.items())
cnt = 0
s = dic[0][1]
for i in range(1, len(dic)):
    cnt += s * dic[i][1]
    s += dic[i][1]
rep = k * (k-1) // 2
ans = (ans + cnt * rep) % MOD
print(ans)

Solution (Python):
import sys

import math
from fractions import Fraction
import itertools

input=sys.stdin.readline
n,k=map(int,input().split())
a=list(map(int,input().split()))
p=[0]*n
q=[0]*n
ans=0
mod=10**9+7
for i in range(n):
    for j in range(n):
        if a[i]>a[j]:
            if i>j:
                p[i]+=1
            elif i<j:
                q[i]+=1
                
for i in range(n):
    ans+=(k*q[i]+((p[i]+q[i])*k*(k-1)//2)%mod)%mod
    ans=ans%mod
print(int(ans%mod))

Solution (Python):

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

print(ans)

Solution (Python):
def resolve():
    '''
    code here
    '''
    N, K = [int(item) for item in input().split()]
    A_list = [int(item) for item in input().split()]

    mod = 10**9+7

    base_n = 0

    for i in range(N):
        for j in range(i+1,N):
            if A_list[i] > A_list[j]:
                base_n += 1
    
    all_n = 0
    for i in range(N):
        for j in range(N):
            if A_list[i] > A_list[j]:
                all_n += 1


    print(((base_n*K)+all_n*(K-1)*K//2)%mod)





if __name__ == "__main__":
    resolve()

Solution (Python):
n,k,*a=map(int,open(0).read().split());s=t=0;r=range(n)
for i in r:
 for j in r:s+=j>i and a[j]<a[i];t+=a[j]<a[i]
print((s*k+k*~-k//2*t)%(10**9+7))

Solution (Python):
MOD = 10**9+7
n, k, *a = map(int, open(0).read().split())
s = t = 0
for i in range(n):
  s += sum(j>i and a[j]<a[i] for j in range(n))
  t += sum(a[j]<a[i] for j in range(n))
print((s*k + k*~-k//2*t) % MOD)

Solution (Python):
n,k = map(int,input().split())
l = list(map(int,input().split()))
mod = 10**9+7
a = 0
for i in range(n):
  b = l[i]
  t = (len([1 for m in l if  m<b])%mod)*(int( (k-1)%mod*k//2)%mod)%mod
 
  s = (len([1 for m in l[i:] if m<b])%mod)*k%mod

  a += (s+t)%mod
print(a%mod)

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
MOD = int(1e9) + 7


def wa(n, a):
    return ((n*(n+1))//2 * a) % MOD


ans = 0

for n in range(N):
    
    a = 0
    nA = A[n+1:]
    t = A[n]
    for na in nA:
        if t > na:
            a += 1
    temp = wa(K, a)
    ans += temp
    ans = ans % MOD

    
    b = 0
    nA = A[:n+1]
    for na in nA:
        if t > na:
            b += 1
    temp = (b * K*(K-1)//2)%MOD
    ans += temp
    ans = ans % MOD

print(ans)

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

mod = 10**9 + 7

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

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

v = (K * (K-1) // 2) % mod * pair % mod
print((cnt * K % mod + v) % mod)

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

ans = 0

for i, a in enumerate(A):
    L, R = 0, 0
    for j, b in enumerate(A):
        if b < a:
            if j < i:
                L += 1
            else:
                R += 1
    ans += (L + R) * K * (K - 1) // 2
    ans += R * K

ans = ans % N
print(int(ans))

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

A = list(map(int, input().split()))

mod = 1000000007
count = 0
for i in range(len(A)-1):
  for j in range(i, len(A)):
    if A[i] > A[j]:
      count += 1

A.sort(reverse=True)
count_ = 0
for i in range(len(A)-1):
  for j in range(i, len(A)):
    if A[i] > A[j]:
      count_ += 1

sum1 = count * K

K = K-1
sum = K*(K+1)//2

sum2 = count_ * sum
print((sum1 + sum2)%mod)

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

Solution (Python):
from bisect import bisect_left
N, K = map(int, input().split())
A = list(map(int, input().split()))
B = sorted(A)
p = 10**9+7

list1 = [bisect_left(B,A[i]) for i in range(N)]
list2 = [bisect_left(sorted(A[i+1:]),A[i]) for i in range(N)]
ans = 0
for i in range(N):
  ans += list2[i] * K
  ans %= p
  tmp = K * (K-1) // 2
  ans += list1[i] * tmp
  ans %= p
print(ans)

Solution (Python):
n,k = map(int,input().split())
a_list = list(map(int,input().split()))
a_list_2 = a_list + a_list
first_count = 0
p = 10**9 + 7
for i in range(n-1):
  for j in range(i+1,n):
     if  a_list[j] < a_list[i]:
      first_count += 1
second_count = 0    
for a in a_list:
  for i in range(n):
    if a > a_list[i]:
      second_count += 1
ans = first_count * k  +  (k-1) * k * second_count //2
print(ans % p)

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
cnt1 = 0
cnt2 = 0
seen = []
for i in a:
    for j in seen:
        if i<j:
            cnt1 += 1
    seen.append(i)
for i in a:
    for j in seen:
        if i<j:
            cnt2 += 1
ans = cnt1*k+cnt2*k*(k-1)//2
print(ans%(10**9+7))

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

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = a + a
c = 0
for i in range(len(a)):
    for j in range(i + 1, len(a)):
        if a[i] > a[j]:
            c += 1
d = 0
for i in range(len(b)):
    for j in range(i + 1, len(b)):
        if b[i] > b[j]:
            d += 1
d -= c * 2
def modpow(n, p, mod):
    r = 1
    while p > 0:
        if p & 1 == 1:
            r = r * n % mod
        n = n * n % mod
        p >>= 1
    return r
def modinv(n, mod):
    return modpow(n, mod - 2, mod)
MOD = 10 ** 9 + 7
ans = (k - 1) * d % MOD
ans += 2 * c
ans %= MOD
ans *= k
ans %= MOD
ans *= modinv(2, MOD)
ans %= MOD
print(ans)

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = a + a
c = 0
for i in range(len(a)):
    for j in range(i + 1, len(a)):
        if a[i] > a[j]:
            c += 1
d = 0
for i in range(len(b)):
    for j in range(i + 1, len(b)):
        if b[i] > b[j]:
            d += 1
d -= c * 2
def modpow(n, p, mod):
    r = 1
    while p > 0:
        if p & 1 == 1:
            r = r * n % mod
        n = n * n % mod
        p >>= 1
    return r
def modinv(n, mod):
    return modpow(n, mod - 2, mod)
MOD = 10 ** 9 + 7
ans = (k - 1) * d % MOD
ans += 2 * c
ans %= MOD
ans *= k
ans %= MOD
ans *= modinv(2, MOD)
ans %= MOD
print(ans)

Solution (Python):
n,k=map(int,input().split())
A = list(map(int,input().split()))
lA = len(A)
mod=10**9+7
cnt1=0
for i in range(lA):
    for j in range(i+1,lA):
        if A[i] > A[j]:
            cnt1+=1
cnt2=0
for i in range(lA):
    for j in range(lA):
        if A[i] > A[j]:
            cnt2+=1
print((cnt1*k+cnt2*(k*(k-1)//2))%mod)

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

Solution (Python):
import sys

N, K = map(int, input().split())
A = list(map(int, sys.stdin.readline().rsplit()))
mod = 10 ** 9 + 7

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

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

mod = 10 ** 9 + 7

ai = 0
bi = 0

for i in range(N-1):
    for j in range(i+1, N):
        if(A[i] > A[j]):
            ai += 1
            ai %= mod

for i in range(N):
    for j in range(N):
        if (A[i] < A[j]):
            bi += 1
            bi %= mod

ansa = ai * K % mod
ansb = K * (K-1) // 2 % mod
ansb = ansb * bi % mod
ans = (ansa + ansb) % mod
print(ans)

Solution (Python):
import sys


def main():
    N, K = map(int, sys.stdin.readline().split())
    A = list(map(int, sys.stdin.readline().split()))

    count1 = 0
    count2 = 0

    for i in range(N - 1):
        for j in range(i + 1, N):
            if A[i] > A[j]:
                count1 = count1 + 1
            if A[i] != A[j]:
                count2 = count2 + 1
    count1 = count1 * K
    count2 = count2 * (K * (K - 1) // 2)

    ans = (count1 + count2) % 1000000007
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

"""
リストをk回繰り返したものの、転倒数の個数を求めよ
先ず純粋に与えられたリスト内での転倒数を求めた後、
それを1~k倍したものを足し合わせる
"""
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0
mod = 10**9+7

n,k = map(int,readline().split())
lst1 = list(map(int,readline().split()))
fall = 0
fall_al = 0
for i in range(n-1):
    for j in range(i+1,n):
        if lst1[i] > lst1[j]:
            fall += 1
lst1.sort(reverse=True)
for i in range(n-1):
    for j in range(i+1,n):
        if lst1[i] > lst1[j]:
            fall_al += 1

def reydeoro(n):
    return n*(n+1)//2
print((fall*k+fall_al*reydeoro(k-1))%mod)

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

Solution (Python):
import sys
import bisect

MOD = pow(10, 9)+7
readline = sys.stdin.readline

def main():
    N, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    init = []
    for i in range(N):
        cnt = 0
        for j in range(0, i):
            if A[j] < A[i]:
                cnt += 1
        init.append(cnt)
    
    Atmp = A.copy()
    Atmp.sort()
    large = []
    for i in range(N):
        large.append(bisect.bisect_left(Atmp, A[i]))

    ans = 0
    for i in range(N):
        a0 = large[i]*K-init[i]
        b = large[i]
        ans += K*a0 - ((K*(K-1))//2)*large[i]
        ans %= MOD
    print(ans%MOD)

if __name__ == "__main__":
    main()

Solution (Python):
MOD = 10 ** 9 + 7
def main():
    N, K = (int(_) for _ in input().split())
    A = [int(_) for _ in input().split()]

    R, L = [], []
    for i in range(N):
        cr = cl = 0
        for j in range(N):
            if i == j: continue
            if A[i] > A[j]:
                if i > j: cl += 1
                if i < j: cr += 1
        R.append(cr)
        L.append(cl)

    output = 0
    for i in range(N):
        output += (K*R[i] + K*(K-1)*(R[i]+L[i])//2)
        output %= MOD
    print(output)
    return

if __name__ == '__main__':
    main()

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

C1 = [0]*n
C2 = [0]*n

for i in range(n):
    for j in range(n):
        if A[i] > A[j]:
            if j >= i:
                C1[i] += k*(k+1)//2
            else:
                C2[i] += (k-1)*k//2
ans = 0


for i in range(n):
    ans += C1[i]+C2[i]
print(ans%mod)

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

C1 = [0]*n
C2 = [0]*n

for i in range(n):
    for j in range(n):
        if A[i] > A[j]:
            if j >= i:
                C1[i] += k*(k+1)//2
            else:
                C2[i] += (k-1)*k//2
ans = 0


for i in range(n):
    ans += C1[i]+C2[i]
print(ans%mod)

Solution (Python):
import sys
import re
import math
import collections
import decimal
import bisect
import itertools

import copy






INF = sys.maxsize
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))





def main():
    n, k = ns()
    a = na()

    total = 0
    for i in range(n):
        for j in range(n):
            if a[i] < a[j]:
                total += 1
                total %= MOD

    ans = total * (k * (k - 1) // 2) % MOD

    cnt = 0
    for i in range(n):
        for j in range(n-i-1):
            if a[j] > a[j+1]:
                a[j], a[j+1] = a[j+1], a[j]
                cnt += 1
                cnt %= MOD
    cnt = cnt * k % MOD

    res = (ans + cnt) % MOD

    print(res)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import re
import math
import collections
import decimal
import bisect
import itertools

import copy






INF = sys.maxsize
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))





def main():
    n, k = ns()
    a = na()

    total = 0
    for i in range(n):
        for j in range(n):
            if a[i] < a[j]:
                total += 1
                total %= MOD

    ans = total * (k * (k - 1) // 2) % MOD

    cnt = 0
    for i in range(n):
        for j in range(i):
            if a[j] > a[i]:
                cnt += 1
                cnt %= MOD
    cnt = cnt * k % MOD

    res = (ans + cnt) % MOD

    print(res)


if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np
import functools
import math
import scipy
import fractions
import itertools

def solve():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    hoge, huga = 0, 0
    piyo = 10**9+7
    for i in range(n-1):
        for j in range(i, n):
            if a[i] > a[j]:
                hoge += 1
    hoge *= k
    for i in range(n):
        for j in range(n):
            if i != j:
                if a[i] > a[j]:
                    huga += 1
    huga *= (k*(k-1))//2
    print((hoge+huga)%piyo)
    return 0

if __name__ == "__main__":
    solve()

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

for i in range(n):
    for j in range(i+1,n):
        if a[i] > a[j]:
            inner += 1
            inner %= mod
inner *= k
inner %=mod

outer = 0

for i in range(n):
    for j in range(n):
        if a[i] > a[j]:
            outer +=1
            outer %= mod
outer *= (k*(k-1)//2)
outer %= mod

print((inner + outer) % mod )

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

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

cmb_cnt = 0
for a in A:
   cmb_cnt += len([x for x in A if x < a]) 

kC2 = (K*(K-1))//2
ans = (cnt*K)+(cmb_cnt*kC2)
print(ans%(10**9+7))

Solution (Python):
MOD = 10**9 + 7
N, K = map(int, input().split())
arr = list(map(int, input().split()))
tmp = 0
for i in range(N):
    for k in range(i,N):
        if i == k:
            continue
        if arr[i] > arr[k]:
            tmp += 1
tmp2 = 0
for i in range(N):
    for k in range(N):
        if i == k:
            continue
        if arr[i] > arr[k]:
            tmp2 += 1
print((tmp*K+(((K-1)*K)//2)*tmp2)%MOD)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():

    n, k = map(int, input().split())

    def count_bigger(seq):
        cnt = 0
        for i in seq:
            for j in seq:
                if i < j:
                    cnt += 1
        return cnt

    
    

    
    
    
    
    def getSum(BITree, index):
        sum = 0  

        
        while (index > 0):

            
            sum += BITree[index]

            
            index -= index & (-index)

        return sum

    
    
    
    
    def updateBIT(BITree, n, index, val):

        
        while (index <= n):

            
            BITree[index] += val

            
            
            index += index & (-index)

    
    def getInvCount(arr, n):

        invcount = 0  

        
        maxElement = max(arr)

        
        
        
        
        BIT = [0] * (maxElement + 1)
        for i in range(1, maxElement + 1):
            BIT[i] = 0
        for i in range(n - 1, -1, -1):

            invcount += getSum(BIT, arr[i] - 1)
            updateBIT(BIT, maxElement, arr[i], 1)
        return invcount

    
    A = list(map(int, input().split()))
    tot1 = getInvCount(A, n)

    tot2 = count_bigger(A)
    mod = 10**9+7
    print((tot1*k + tot2*(k*(k-1)//2)) % mod)

resolve()

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

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

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

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

Solution (Python):
from sys import stdin
from collections import defaultdict, deque, Counter
import sys
from bisect import bisect_left
import heapq
import math

sys.setrecursionlimit(10000000)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18



n,k = map(int, stdin.readline().rstrip().split())

A = list(map(int, stdin.readline().rstrip().split()))



ans = 0
for i in range(n):
    right = 0
    left = 0
    for j in range(i):
        if A[i] > A[j]:
            left += 1
    for j in range(i+1, n):
        if A[i] > A[j]:
            right += 1
    ans += int(right*k*(k+1)//2 + left*k*(k-1)//2)

print(ans % MOD)

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

a = list(map(int, input().split()))

inside = [0] * n
outside = [0] * n

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

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

ans = 0
ans += sum(inside) * k
ans %= mod
ans += sum(outside) * k * (k - 1) // 2
ans %= mod
print(ans)

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

L = list(map(int, input().split()))

t = 0
for i in range(n):
    for j in range(i + 1, n):
        if L[i] > L[j]:
            t += 1

t = (t * k) % (10**9 + 7)

L = sorted(L)
s = 0
for i in range(n):
    for j in range(i + 1, n):
        if L[i] < L[j]:
            s += 1
k = k - 1
sum = (k * (k + 1)//2)% (10**9 + 7)
s = (s * sum) % (10**9 + 7)
print((t + s) % (10**9 + 7))

Solution (Python):
import numpy as np
n,k = map(int,input().split())
a = np.array(list(map(int,input().split())))


af = [0]*n
al = [0]*n

for i in range(n):
    b = a[i+1:]
    af[i] = len(b[b < a[i]])
    al[i] = len(a[a < a[i]])
s = 0

for i in range(n):
    s += al[i] * (k**2 - k*(k+1)//2) + af[i] * k
    s %= 1000000007
print(s)

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

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

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

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

inter = 0
for i in range(n):
    for j in range(i+1,n):
        if a[i] > a[j]:
            inter += 1
outer = 0
for i in range(n):
    for j in range(n):
        if a[i] > a[j]:
            outer += 1

ans = inter*k + outer*k*(k-1)//2
ans %= 10**9+7
print(ans)

Solution (Python):
import sys

def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))

def DD2(d1,d2,init=0): return [[init]*d2 for _ in range(d1)]
def DD3(d1,d2,d3,init=0): return [DD2(d2,d3,init) for _ in range(d1)]

def to_bin(x: int) -> str: return format(x, 'b') 
def to_oct(x: int) -> str: return format(x, 'o') 
def to_hex(x: int) -> str: return format(x, 'x') 
MOD=10**9+7
def divc(x,y) -> int: return -(-x//y)
def divf(x,y) -> int: return x//y
def gcd(x,y):
    while y: x,y = y,x%y
    return x
def lcm(x,y): return x*y//gcd(x,y)
def enumerate_divs(n):
    """Return a tuple list of divisor of n"""
    return [(i,n//i) for i in range(1,int(n**0.5)+1) if n%i==0]
def get_primes(MAX_NUM=10**3):
    """Return a list of prime numbers n or less"""
    is_prime = [True]*(MAX_NUM+1)
    is_prime[0] = is_prime[1] = False
    for i in range(2, int(MAX_NUM**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i*2, MAX_NUM+1, i):
            is_prime[j] = False
    return [i for i in range(MAX_NUM+1) if is_prime[i]]

from itertools import accumulate as acc, combinations as combi, product, combinations_with_replacement as combi_dup
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left


def main():
    n, k = MII()
    aa = MII()
    cnt = 0
    for i in range(n):
        a = aa[i]
        for j in range(i,n):
            if a > aa[j]:
                cnt += 1
    cnt_s = 0
    aa.sort(reverse=True)
    for i in range(n):
        a = aa[i]
        for j in range(i,n):
            if a > aa[j]:
                cnt_s += 1
    print((cnt*k+cnt_s*(k*(k-1))//2)%MOD)

if __name__ == '__main__':
    main()

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

A=[int(N) for N in A]

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

a = (K*(K-1)//2)%(10**9+7)
b = foll*K%(10**9+7)
u= (prev+foll)*a+b
print(u%(10**9+7))

Solution (Python):
N, K = map(int, input().split())
A = [int(a) for a in input().split()]

p = 10**9 + 7

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

print((all * K * (K-1)//2 + K * aft) % p)

Solution (Python):
def main():
    n,k = map(int,input().split())
    a = list(map(int,input().split()))
    MOD = 10 ** 9 + 7
    sm1 = [0] * n
    sm2 = [0] * n
    for i in range(n):
        now = a[i]
        for j in range(i+1,n):
            if now > a[j]:
                sm1[i] += 1
    for i in range(n):
        now = a[i]
        for j in range(i):
            if now > a[j]:
                sm2[i] += 1
    a1 = sum(sm1) % MOD
    d = (sum(sm2) + a1) % MOD

    ans = k * (2*a1 + (k-1)*d)
    ans %= MOD
    inv_2 = power_func(2,MOD-2,MOD)
    ans = ans *inv_2
    print(ans%MOD)

def power_func(a,n,m):
  bi=str(format(n,"b"))
  ans=1
  for i in range(len(bi)):
    ans=(ans*ans) %m
    if bi[i]=="1":
      ans=(ans*a) %m
  return ans

if __name__ == '__main__':
    main()

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    import bisect
    
    N,K=MI()
    A=LI()
    

    rev0=0
    for i in range(N):
        for j in range(N):
            if A[i]>A[j]:
                if j>=i:
                    rev0+=(K*(K+1))//2
                else:
                    rev0+=(K*(K-1))//2
            rev0%=mod
                

        
    print(rev0)
        
               
    

main()

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

ans = 0
MOD = 10**9+7
for i in range(N):
    l = 0
    r = 0
    for j in range(N):
        if i<j:
            if A[i]>A[j]:
                r += 1
            else:
                pass
        elif i>j:
            if A[i]>A[j]:
                l += 1
            else:
                pass
    ans += l*max(0,(K-1)*(K)//2)
    ans %= MOD
    ans += r*(K+1)*K//2
    ans %= MOD
print(ans)

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

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

y = 0
for a1 in a:
    for a2 in a:
        if a1 > a2: y += 1

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

Solution (Python):
N, K = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
total = 0
B = [0]*N
for n in range(N):
  count = 0
  for m in range(N):
    if A[m]<A[n]:
      count += 1
  B[n] = count
for n in range(N):
  for m in range(n,N):
    if A[m]<A[n]:
      total+=1
total *= K
K = K*(K-1)//2
for b in B:
  total += b*K
  total %= 1000000007
print(total)

Solution (Python):
n, k = map(int, input().split())
lis = list(map(int, input().split()))
c1, c2 = 0, 0
for i in range(n):
        for j in range(n):
                if lis[i] > lis[j]:
                        c2 += 1
                        if j > i:
                                c1 += 1
ans = c1*k + c2*k*(k-1)//2
print(ans % 1000000007)

Solution (Python):
N, K = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]


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

y = 0
for a1 in A:
    for a2 in A:
        if a1 > a2:
            y+=1

mod = 10**9 + 7

ans = z * (K) + y * (K) * (K-1)//2
print(ans % mod)

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


def read():
    N, K = map(int, input().strip().split())
    A = list(map(int, input().strip().split()))
    return N, K, A


class ModInt(int):
    
    MOD = 10**9+7

    def __new__(cls, x, *args, **kwargs):
        return super().__new__(cls, x % ModInt.MOD)

    def __add__(self, other):
        return ModInt(super().__add__(other) % ModInt.MOD)

    def __radd__(self, other):
        return ModInt(super().__radd__(other) % ModInt.MOD)

    def __mul__(self, other):
        return ModInt(super().__mul__(other) % ModInt.MOD)

    def __rmul__(self, other):
        return ModInt(super().__rmul__(other) % ModInt.MOD)

    def __pow__(self, other, *args):
        return ModInt(super().__pow__(other, ModInt.MOD))

    def __rpow__(self, other, *args):
        raise NotImplementedError()

    def __truediv__(self, other):
        return ModInt(super().__mul__(pow(other, ModInt.MOD-2, ModInt.MOD)))

    def __floordiv__(self, other):
        return self.__truediv__(other)

    def __rtruediv__(self, other):
        return ModInt(self.__pow__(ModInt.MOD-2).__mul__(other))

    def __rfloordiv__(self, other):
        return self.__rtruediv__(other)


def solve(N, K, A):
    ModInt.MOD = 10**9+7
    N = len(A)
    k = ModInt(K)
    t0 = k * (k+1) // 2  
    t1 = (k-1) * k // 2

    c0 = 0
    c1 = 0
    for i in range(N):
        for j in range(i+1, N):
            if A[i] > A[j]:
                c0 += 1
            elif A[i] < A[j]:
                c1 += 1
    return c0 * t0 + c1 * t1


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

Solution (Python):

SOSU = (10**9 + 7)

n, k = list(map(int, input().split(' ')))
A = list(map(int, input().split(' ')))



cntall = 0
cntusiro = 0

for indexi, i in enumerate(A):
    for indexj, j in enumerate(A):
        if i > j :
            cntall += 1
            if indexi < indexj:
                cntusiro += 1
        

anst = (k*(k -1))*cntall//2 + k * cntusiro 


ans = anst % SOSU



print(int(ans) )