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):
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    
    return divisors
def examA():
    M, D = LI()
    c = D//10
    ans = 0
    for i in range(2,M+1):
        d = make_divisors(i)

        for j in range(len(d)//2):
            cur0 = d[j*2+1]; cur10 = d[j*2]
            if cur10==1:
                continue
            if cur0<=9:
                if cur10<c:
                    ans +=1

                elif cur10==c and cur0+cur10*10<=D:
                    ans +=1

        for j in range(len(d) // 2):
            cur0 = d[j * 2];cur10 = d[j * 2+1]
            if cur0 == 1:
                continue
            if cur10 <= 9:
                if cur10 < c:
                    ans += 1
                elif cur10 == c and cur0 + cur10 * 10 <= D:
                    ans += 1
        if len(d)%2==1:
            cur0 = d[-1]; cur10 = d[-1]
            if cur10==1:
                continue
            if cur0<=9:
                if cur10<c:
                    ans +=1

                elif cur10==c and cur0+cur10*10<=D:
                    ans +=1

    print(ans)
    return

class Bit():
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
        return
    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
        return
def examB():
    N, K = LI()
    A = LI()
    bit = Bit(max(A))
    cur = 0
    for i, a in enumerate(A):
        bit.add(a, 1)
        cur += i + 1 - bit.sum(a)
    As = 0
    A.sort()
    now = 0
    for i in range(1,N):
        if A[i]!=A[i-1]:
            now = i
        As += now

    ans = (As*(K**2) - (As-2*cur)*K)//2
    ans %=mod
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,inf
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examB()

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

MOD = 10 ** 9 + 7
in_A, out_A = 0, 0

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

print((in_A * K + out_A * K * (K - 1) // 2) % MOD)

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


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

Solution (Python):
from collections import defaultdict
from bisect import bisect_left
def bubble_sort(A):
    cnt = 0
    for i in range(len(A)):
        for j in range(len(A)-1, i, -1):
            if A[j] < A[j-1]:
                A[j], A[j-1] = A[j-1], A[j]
                cnt += 1
    return cnt

def main():
    N,K = map(int,input().split())
    A = [int(x) for x in input().split()]
    mod = 10**9 + 7
    
    falls = bubble_sort(A)
    rank = 0
    for i in A:
        rank += bisect_left(A,i)
        
    falls_all = (falls*K)%mod
    rank_all  = ((K*(K-1))//2)%mod
    rank_all  = (rank_all*rank)%mod

    print((falls_all+rank_all)%mod) 
    




if __name__ == "__main__":
    main()

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
mod = int(1e+9 + 7)
c, d = 0, 0
for i in range(N-1):
  for j in range(i+1, N):
    if A[i] > A[j]:
      d += 1
    elif A[i] < A[j]:
      c += 1
kip = ((K * (K + 1)) // 2) % mod
pip = ((K * (K - 1)) // 2) % mod
S = (d * kip) % mod + (c * pip) % mod
S %= mod
print(S)

Solution (Python):
import sys
from collections import Counter
from itertools import accumulate, combinations

read = sys.stdin.read
N, K, *A = map(int, read().split())
mod = 10 ** 9 + 7

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

answer = 0

if K > 1:
    b = Counter(A)
    cnt1 = [j for i, j in sorted(b.items())]
    n = list(accumulate(cnt1))
    n = sum(i * j for i, j in zip(cnt1[1:], n[:-1]))

    answer += n * (K * (K - 1) // 2 % mod)
    answer %= mod

answer += cnt * K
answer %= mod
print(answer)

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

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

val1 = num1*(K*(K+1)//2)%MOD
val2 = num2*(K*(K-1)//2)%MOD
print((val1+val2)%MOD)

Solution (Python):
import numpy as np

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

a = np.array(a)
c = np.sort(a)[::-1]
MOD = 10 ** 9 + 7
count = 1
ans = 0
d = 0
e = 0
for i in a:
    b = a[count:n]
    d += len(c[c < i])
    e += len(b[b < i])
    count += 1

ans = (e * k + k*(k-1)//2 * d)
ans %= MOD

print(int(ans))

Solution (Python):
import sys
import numpy as np

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))
rs = lambda: stdin.readline().rstrip()  

N, K = rl()
MOD = 10 ** 9 + 7
A = np.array(rl())
B = np.array([])
answer = 0
add = 0
for a in A:
    answer += (B > a).sum()
    add += (A > a).sum()
    B = np.append(B, a)
answer *= K
answer %= MOD
time = (K-1) * K // 2 % MOD
answer += add * time
print(answer%MOD)

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

Tin = 0
for i in range(len(A)):
  temp = A[i]
  for j in range(i+1, len(A)):
    if temp > A[j]:
      Tin += 1

Tinter = 0
B = [l for l in A]
B.extend(B)
for i in range(len(A)):
  temp = A[i]
  for j in range(len(A), len(A)*2):
    if temp > B[j]:
      Tinter += 1

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

Solution (Python):
import numpy as np

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

cnt1 = 0
cnt2 = 0
for i in range(n - 1):
    cnt1 += (a[i + 1:] < a[i]).sum()

for i in range(n):
    cnt2 += (a < a[i]).sum()

cnt1 = int(cnt1) * k % mod
cnt2 = int(cnt2) * (k * (k - 1) // 2) % mod
print((cnt1 + cnt2) % mod)

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



count2=0
for i in range(N-1):
    for m in range(i+1,N):
        count2+=A[i]<A[m]


count=count1*((K*(K+1)//2)%mod)
count%=mod
count+=count2*(((K-1)*K//2)%mod)
count%=mod


print(count)

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

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

cnt = 0
for i in range(n):
    for j in range(n):
        if i == j:
            continue
        if A[i] > A[j]:
            cnt += 1
ans += cnt*(k*(k-1)//2) % MOD
ans %= MOD

print(ans)

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

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

Solution (Python):
N, K = map(int, input().split())
A = [int(i) for i in input().split()]
mod = 10 ** 9 + 7

ret = 0
ca = [0] * (2001)
cb = [0] * (2001)

for i in range(N) :
    ca[A[i]] += 1
    ca[A[i]] %= mod
    
for i in range(2000) :
    ca[i + 1] = (ca[i + 1] + ca[i]) % mod

for i in range(N - 1, -1, -1) :
    ret += K * sum(cb[:A[i]]) + K * (K - 1) * ca[A[i] - 1] // 2
    ret %= mod
    cb[A[i]] += 1
    
print(ret)

Solution (Python):

import sys

MOD = 1000000007  


def solve(N: int, K: int, A: "List[int]"):
    
    tentousuu = dict()
    all_tentousuu = dict()
    for a in range(0,2001):
        tentousuu[a] = 0
        all_tentousuu[a] = 0 

    for i in range(0,N):
        for j in range(0,N):
            if j<i and A[j]>A[i]:
                tentousuu[A[i]]+=1
            
            if A[i]<A[j]:
                all_tentousuu[A[i]] += 1
    
    one_to_k_sum = (K)*(K-1)//2
    answer = 0
    for a in range(0,2001):
        answer += tentousuu[a]*K+all_tentousuu[a]*one_to_k_sum
    
    print(answer%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()))

mod=10**9+7
cnt_lst_before=[0]*n
cnt_lst_after=[0]*n
cnt_a=0
for i in range(n):
    for j in range(n):
        if i<j and a[i]>a[j]:
            cnt_lst_after[i]+=1
        elif i>j and a[i]>a[j]:
            cnt_lst_before[i]+=1
if k==1:
    print(sum(cnt_lst_after))
else:
    print((sum(cnt_lst_after)*(k*(k+1)//2)+sum(cnt_lst_before)*(k*(k-1)//2)%mod)%mod)

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

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

Solution (Python):
n,k = map(int,input().split())
arr = list(map(int,input().split()))
mod = 10**9 + 7
cnt_list = [[0]*2 for i in range(n)]
for i in range(n):
    for j in range(n):
        if arr[i] > arr[j]:
            if i > j:
                cnt_list[i][0] += 1
            else:
                cnt_list[i][1] += 1
ans = 0
for l,r in cnt_list:
    ans += -l * k + (l+r)*k*(k+1)//2
    ans %= mod
print(ans)

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

one_tento = 0
for i in range(n-1):
    for j in range(i+1,n):
        if array[i] > array[j]:
            one_tento += 1
one_tento = int(one_tento)

two_tento = 0
for i in range(n):
    for j in range(n):
        if array[i] > array[j]:
            two_tento += 1
two_tento = int(two_tento)

result1 = (one_tento * k) % (7 + 10**9)
result2 = ((two_tento % (7 + 10**9)) * ((k * (k - 1) // 2) % (7 + 10**9))) % (7 + 10 **9)
result3 = (result1 + result2) % (7 + 10**9)
print(int(result3))

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
ans=0
ans1=0
for i in range(n):
  for j in a:
    if a[i]<j:
      ans+=1
for i in range(n-1):
  for j in range(i+1,n):
    if a[i]>a[j]:
      ans1+=1
print((ans1*k+ans*(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()))

B=A+A
C=[0]*N
D=[0]*N

for i in range(N):
    for j in range(i,2*N):
        if(B[i]>B[j]):
            C[i]+=1
for i in range(N):
    for j in range(i,N):
        if(B[i]>B[j]):
            D[i]+=1

c=sum(C)%MOD
d=sum(D)%MOD
c=(c-d)%MOD
t=((K*(2*d+((K-1)*c)))//2)%MOD

print(t)

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):
  count=0
  for j in range(i+1,N):
    if A[i]>A[j]:
      count+=1
  ans+=count
count=0
for i in range(N):
  for j in range(N):
    if A[i]>A[j]:
      count+=1
ans*=K
ans+=(count%mod)*(K*(K-1)//2)%mod
print(ans%mod)

Solution (Python):
n,m=map(int,input().split())
l=list(map(int,input().split()))
count=[0 for i in range(n)]
for i in range(n):
  count1=0 
  if i==n-1:
    pass
  else:
    for j in range(i+1,n):
      if l[i]>l[j]:
        count1+=1
  count[i]=count1
l1=sorted(l)
count2=[0 for i in range(n)]
countsub=0
for i in range(1,1+n-1):
  if l1[i]==l1[i-1]:
    count2[i]=count2[i-1]
    countsub+=1
  else:
    count2[i]=count2[i-1]+countsub+1
    countsub=0
ans=0
for i in range(n):
  ans=ans+count2[i]*(m-1)*m//2
  ans=ans+(count[i])*m
  ans%=10**9+7
print(ans)

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

cnt_in = 0
for i in range(N-1):
    for j in range(i+1, N):
        if A[i] > A[j]:
            cnt_in += 1
ans = (cnt_in * K) % MOD

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

ans += (cnt_next * (K*(K-1))//2) % MOD
ans %= MOD

print(int(ans))

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)
mod = 10**9 + 7
import numpy as np
n, k = map(int,readline().split())
a = np.array(readline().split(),np.int64)
f = 0
r = 0
for i in range(n):
    for j in range(i+1, n):
        if a[i] > a[j] :
            r += 1
        elif a[i] < a[j]:
            f += 1
print((f * k * (k-1) // 2 % mod + r * k * (k+1) // 2 % mod) % mod)

Solution (Python):
def solve():
    N, K, *a_l = map(int, open(0).read().split())
    MOD = 10**9+7
    count_a, count_b = 0, 0
    inversion = 0

    for i in range(N):
        for j in range(N):
            if a_l[i] > a_l[j]:
                if i < j:
                    count_a += 1
                count_b += 1

    inversion = count_a * K + count_b * (K * (K - 1)) // 2
    print(inversion%MOD)



if __name__ == '__main__':
    solve()

Solution (Python):
def solve():
    N, K, *a_l = map(int, open(0).read().split())
    MOD = 10**9+7
    count_a, count_b = 0, 0
    inversion = 0

    for i in range(N):
        for j in range(N):
            if a_l[i] > a_l[j]:
                if i < j:
                    count_a += 1
                count_b += 1

    inversion = count_a * K + count_b * (K * (K - 1)) // 2
    print(inversion%MOD)


if __name__ == '__main__':
    solve()

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

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

countInA = 0
countBetA = 0
for i in range(N):
  for j in range(N):
    if i <= j and A[i] > A[j]:
      countInA += 1
    if A[i] > A[j] :
      countBetA += 1

print( (countInA*K + (countBetA * (K * (K-1) // 2))) % defi)

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
a_below = []
count2 = 0
for i in range(n):
    count1 = 0
    for j in range(n):
        if a[j] < a[i]:
            count1 += 1
        if j > i and a[j] < a[i]:
            count2 += 1
    a_below.append(count1)
a_slip = count2

print(((sum(a_below)*k*(k-1))//2 + a_slip*k)%1000000007)

Solution (Python):
import bisect
n,k=map(int,input().split())
mod=10**9+7
a=list(map(int,input().split()))
c=sorted(a)
d=[0]*n
e=[0]*n
for i in range(n):
  d[i]=bisect.bisect_left(c,a[i])
for i in range(n):
  cnt=0
  for j in range(i,n):
    if a[i]>a[j]:
      cnt+=1
  e[i]=cnt
st=sum(e)*k%mod
ad=sum(d)%mod
print((st+(ad*(k-1)*k//2)%mod)%mod)

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

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

a = 0
for i in range(N):
  for j in range(i + 1,N):
    if A[i] > A[j]:
      a += 1
      
b = 0
A.sort()
for k in A:
  b += A.index(k) * K * (K - 1) // 2
  
print((a * K + b) % 1000000007)

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

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
usiro=0
for i in range(n):
    for j in range(i,n-1):
        if a[i]>a[j+1]:
            usiro+=1            
mae=0
for i in range(n):
    for j in range(0,i):
        if a[i]>a[j]:
            mae+=1


def f(x):
  return x*(x+1)//2
ans=mae*f(k-1)+usiro*f(k)
print(ans%(10**9+7))

Solution (Python):


n,k = map(int,input().split(" "))
a = list(map(int,input().split(" ")))
a_inv,b_inv = 0,0
mod = 10**9+7

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

s_inv = a_inv*k+b_inv*(k*(k-1))//2

print(s_inv%mod)

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

Solution (Python):
import sys
N, K = map(int,sys.stdin.readline().split())
X = list(map(int, sys.stdin.readline().split()))
firstInv = 0
inv = 0

for i in range(N):
  for j in range(N):
    if X[i] > X[j]:
      inv += 1
      if i < j:
        firstInv += 1

ans = inv * ((K-1) * K // 2) + firstInv*K
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()))
N1=0
N2=0
for i in range(N):
  for j in range(N):
    if(i<j and A[i]>A[j]):
      N1+=1
    if(A[i]>A[j]):
      N2+=1
print((N1*K+K*(K-1)//2*N2)%(10**9+7))

Solution (Python):
n,k = map(int,input().split())
lis = list(map(int,input().split()))
yakisoba = [0 for i in range(n)]
yakisaba = [0 for i in range(n)]
for i in range(n):
  for j in range(n):
    if lis[i] > lis[j]:
      yakisoba[i] += 1
      if i < j:
        yakisaba[i] += 1
ans = sum(yakisaba)
num = sum(yakisoba)
nu = sum(yakisaba)
print(((nu*k + num * (k * (k-1)) // 2)%(10 ** 9 + 7)))

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

one_cnt = 0

for i in range(N):
    for j in range(i+1, N):
        if A[i] > A[j]:
            one_cnt+=1
two_cnt = 0
B = A*2

for i in range(N):
    for j in range(N, 2*N):
        if B[i] > B[j]: two_cnt+=1


one_cnt = one_cnt % 1000000007
two_cnt = two_cnt % 1000000007
ans = one_cnt * K + two_cnt * (K-1)*K//2

print(ans%1000000007)

Solution (Python):
N,K = list(map(int,input().split(" ")))
As = list(map(int,input().split(" ")))
ans = 0
MOD = 10 ** 9 + 7
for i in range(N): 
  left = 0
  right = 0
  temp = As[i]
  for j in range(0,i): 
    if temp <As[j]:
      left += 1
  ans += left*(K)*(K+1)//2
  ans %= MOD
  for j in range(i + 1,N):
    if temp <As[j]:
      right += 1
  ans += right*(K)*(K - 1)//2
  ans %= MOD
print(ans)

Solution (Python):
def merge(l, r, a):
    l1 = len(l)
    r1 = len(r)
    i,j,k=0,0,0
    while j<l1 and k<r1:
        if l[j]<=r[k]:
            a[i]=l[j]
            j+=1
        else:
            cnt[0] += l1-j
            a[i]=r[k]
            k+=1
        i+=1
    while j<l1:
        a[i]=l[j]
        j+=1
        i+=1
    while k<r1:
        a[i]=r[k]
        k+=1
        i+=1
    return a
def merge_sort(a):
    n = len(a)
    if n==1:
        return a
    else:
        mid = n//2
        left = [0]*mid
        right = [0]*(n-mid)
        for i in range(mid):
            left[i] = a[i]
        for i in range(mid,n):
            right[i-mid]=a[i]
    merge_sort(left)
    merge_sort(right)
    merge(left,right,a)
    return a

n,k = map(int,input().split())
a = list(map(int,input().split()))
cnt = [0,0]
ans4 = 0
a = merge_sort(a)
for i in range(1,n):
    if a[i]==a[i-1]:
        cnt[1]+=1
        if i == n-1:
            ans4+=(1+cnt[1])*cnt[1]//2
    else:
        ans4+=(1+cnt[1])*cnt[1]//2
        cnt[1]=0

ans2 = (n*(n-1))//2 - cnt[0]-ans4
print((cnt[0]*(1+k)*k//2+ans2*k*(k-1)//2)%(10**9+7))

Solution (Python):
n,k = map(int,input().split())
a = list(map(int,input().split()))
inv = lambda w:sum([sum([a[i]>a[j] for j in range(i+1,n)]) for i in range(n-1)])
inv1 = lambda w:sum([sum([a[i]==a[j] for j in range(i+1,n)]) for i in range(n-1)])
ans1 = inv(a)
ans3 = inv1(a)
ans2 = (n*(n-1))//2 - ans1 - ans3
print((ans1*((1+k)*k)//2+ans2*(k*(k-1))//2)%(10**9+7))

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

Solution (Python):
import sys
import os

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

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

print((K*cnt1+K*(K-1)*cnt2//2)%mod)

Solution (Python):
n,k=map(int,input().split())
lists=list(map(int,input().split()))
cnt=0
for i in range(n):
    for j in range(i+1,n):
        if lists[i]>lists[j]:
            cnt+=1
cnt=(cnt*k)%(10**9+7)
additional=0
for some in lists:
    count=0
    for other in lists:
        if some>other:
            count+=1
    additional+=count
    
            

an=(((k*(k-1)*additional)//2)%(10**9+7))
print((cnt+an)%(10**9+7))

Solution (Python):
n,k = map(int,input().split())
a = list(map(int,input().split()))
res1 = 0
res2 = 0
for i in range(0,n):
    res1 += len([sa for sa in a if sa < a[i] ])
    res2 += len([sa for sa in a[i:] if sa < a[i] ])

print((res2 *  k + res1 * k*(k-1) // 2) % (10** 9 + 7))

Solution (Python):
n, k = map(int,input().split( ))
a = list(map(int, input().split( )))
lst=[0 for _ in range(2010)]
mod = 10**9+7
unit = 0

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

lst2 = [0 for _ in range(n)]
for i in range(n):
    for j in range(n):
        if a[i]>a[j]:
            
"""

"""
新規
"""
unit = 0
multi = 0
for i in range(n):
    for j in range(i+1,n):
        if a[i]>a[j]:
            unit += 1

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



ans = 0
"""
for i in range(n):
    ans += (lst[a[i]] * lst2[i] * (k-1) * k) //2
"""
ans += multi*(k-1)*k//2
    
ans += unit*k

print(ans%mod)

Solution (Python):

MOD = 10**9 + 7


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    a0 = 0

    for i in range(N):
        for j in range(i+1, N):
            if A[i] > A[j]:
                a0 += 1
    a1 = 0
    A2 = A * 2
    for i in range(N*2):
        for j in range(i+1, N*2):
            if A2[i] > A2[j]:
                a1 += 1
    a2 = 0
    A3 = A * 3
    for i in range(N*3):
        for j in range(i+1, N*3):
            if A3[i] > A3[j]:
                a2 += 1

    d = (a2 - a1) - (a1 - a0)
    
    
    print((a0 + d * ((K * (K - 1)) // 2) + (K - 1) * a0) % MOD)


if __name__ == '__main__':
    main()

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

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

s=[0]*N
t=[0]*N

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


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


for i in range(N):
  s[i]=(s[i]*K)%mod
  t[i]=(t[i]*(K-1)*K//2)%mod



print((sum(s)+sum(t))%mod)

Solution (Python):
def count_inv(lst):
    ret = 0
    for i in range(len(lst)):
        for j in range(i + 1, len(lst)):
            if lst[i] > lst[j]:
                ret += 1
    return ret


def count_smaller_elements(lst):
    ret = 0
    for i in range(len(lst)):
        for j in range(len(lst)):
            if i != j and lst[i] > lst[j]:
                ret += 1
    return ret


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

    a = count_inv(lst_A)
    num1 = a * K % MOD

    b = count_smaller_elements(lst_A)
    num2 = b * (K * (K - 1) // 2) % MOD

    ans = (num1 + num2) % MOD
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
def count_inv(lst):
    ret = 0
    for i in range(len(lst)):
        for j in range(i + 1, len(lst)):
            if lst[i] > lst[j]:
                ret += 1
    return ret


def count_smaller_elements(lst):
    ret = 0
    for i in range(len(lst)):
        tmp = 0
        for j in range(len(lst)):
            if i != j and lst[i] > lst[j]:
                tmp += 1
        ret += tmp
    return ret


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

    a = count_inv(lst_A)
    num1 = a * K % MOD

    b = count_smaller_elements(lst_A)
    num2 = b * (K * (K - 1) // 2) % MOD

    ans = (num1 + num2) % MOD
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
INF = 10**9+7
ans = 0
for i in range(N):
    B = [1 if a < A[i] else 0 for a in A]
    ans = (ans + sum(B[i+1:])*K + sum(B) * (K*(K-1)//2)) % INF
print(ans)

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






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

tmp1 = (K*(K+1)//2)%(10**9 +7)
tmp2 = ((K-1)*K//2)%(10**9 +7)
ans = (p*tmp1 + q*tmp2)%(10**9 +7)
print(ans)

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

for i in range(N):
  for j in range(i,N):
    if i == j:
      pass
    else:
      if A[i] > A[j]:
        tmp += 1
  for k in range(N):
    if A[i] > A[k]:
      tmp2 += 1  
  ans += tmp2*K*(K-1)//2+tmp*K
  ans %= MOD
  tmp = 0
  tmp2 = 0
print(ans)

Solution (Python):
n,k = (int(i) for i in input().split())
a = [int(i) for i in input().split()]
b,m,num,mod = sorted(a),0,0,10**9+7
for i in range(1,n):
    if b[i]==b[i-1]: num+=m
    else: num,m = num+i,i
d,ans = [0]*2001,0
def add(i,x):
	while i<=2000:
		d[i] += x
		i += i & -i
def get(i):
	s = 0
	while i:
		s += d[i]
		i -= i & -i
	return s
for i in range(n):
    add(a[i],1)
    ans = (ans-get(a[i])+i+1)%mod
print((ans*k+num*k*(k-1)//2)%mod)

Solution (Python):
import bisect
m = 10**9+7
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = sorted(a)
cc = 0
ca = 0
for i in range(n):
  c = 0
  for j in range(i+1,n):
    if a[i] > a[j]:
      c += 1
  cc += c
  ca += bisect.bisect_left(b,a[i])
cc %= m; ca %= m
print(((cc * k) % m + (((((k-1) * k)//2) % m) * ca) % m) % m)

Solution (Python):
import bisect
m = 10**9+7
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = sorted(a)
cc = [0] * n
ca = [0] * n
for i in range(n):
  c = 0
  for j in range(i+1,n):
    if a[i] > a[j]:
      c += 1
  cc[i] = c
  ca[i] = bisect.bisect_left(b,a[i])
s = 0
for i in range(n):
  s = ((cc[i]*k)%m + ((k-1)*k*ca[i]//2)%m + s)%m
print(s)

Solution (Python):


N,K=map(int,input().split())

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

A=A+A

count=[0]*2*N

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

Solution (Python):
a,b=map(int,input().split())
l = list(map(int,input().split()))
dp = [0]*a
ep = [0]*a
s = 0
MOD = 10**9 + 7
for n,x in enumerate(l):
    for m,y in enumerate(l[n+1:]):
        if x > y:dp[n]+=1
    
for n,x in enumerate(l):
    for m,y in enumerate(l[:n]):
        if x > y:ep[n]+=1
for n,x in enumerate(dp):
    ep[n]+=x


s  = (sum(dp)*b)%MOD
s  += (sum(ep)*(b-1)*b//2)%MOD
print(s%MOD)

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

cnt=0
a=0

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

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

      
  print(int(cnt*k+a*(k*(k-1)//2))%(10**9+7))

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

count = 0

for j in range(0,n):
  for i in range(0,n):
    if a[j]>a[i]:

      if j >= i:
        count += (k-1)*k//2
      else:
        count += k*(k+1)//2

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

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

cnt_array = [0 for x in range(2000+1)]
a_array2 = []
ans = 0
for a in a_array:
    cnt_array[a] += 1
    a_array2.append(sum(cnt_array[a+1:]))
a_array3 = []
for i in a_array:
    tmp = 0
    for j in a_array[::-1]:
        if j > i:
            tmp += 1
    a_array3.append(tmp)
for cnt, d in zip(a_array2, a_array3):
    ans += (k*(2*cnt+(k-1)*d))//2
    ans = ans % INF
print(ans)

Solution (Python):


maxs = 10 ** 9 +7
N, K = [int(x) for x in raw_input().split(' ')]
A = [int(x) for x in raw_input().split(' ')]
B = sorted(A)
l1, l2 = [], []
for i in xrange(N):
    l1.append(len([A[j] for j in range(0, N) if A[j] < A[i]]))
    l2.append(len([A[j] for j in range(i, N) if A[j] < A[i]]))
ans = sum(l1)  * K * (K-1) / 2 + sum(l2) * K
print(ans % maxs)