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):
MOD = 10**9 + 7

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

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

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

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

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

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

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

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

ans = (down2 * ((k-1) * k // 2)) % d
ans = (ans + down1 * k) % d
print(ans)

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

after_sum = 0
whole_sum = 0
for i in range(N):
    before = 0
    after = 0
    for j in range(N):
        if A[i] > A[j]:
            if i < j:
                after += 1
            elif j < i:
                before += 1
    
    whole = before + after
    after_sum = (after_sum + after) % MOD
    whole_sum = (whole_sum + whole) % MOD

a = (K * after_sum) % MOD
b = ((K - 1) * K // 2) % MOD
b = (b * whole_sum) % MOD
print((a + b) % MOD)

Solution (Python):
import sys


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


sys.setrecursionlimit(10 ** 9)


def main():
    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
    cnt_1 = 0
    for i in range(N):
        for j in range(N):
            if A[i] > A[j]:
                cnt_1 += 1
    a = cnt * K % MOD
    b = (cnt_1 * K * (K - 1) // 2) % MOD
    answer = (a + b) % MOD
    print(answer)


if __name__ == "__main__":
    main()

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

ans = 0
for i in range(N):
    c1 = counter1[i]
    c2 = counter2[i]
    ans = (ans + (c1 * K + c2 * K * (K - 1) // 2)) % mod
print(ans)

Solution (Python):
import math
n,k = map(int,input().split())
li = list(map(int,input().split()))
li.reverse()
count = 0

for i,l in enumerate(li):
    if i != len(li)-1: 
        a = len(list(filter(lambda x:x > l, li[i+1:])))
        count += a

next_count = 0

for i,l in enumerate(li):
    a = len(list(filter(lambda x:x > l, li)))
    next_count += a
""" 
al = 0
for x in range(1,k+1):
    if x == 1:
        al += count
    else:
        al += count
        al += next_count*(x-1) """

all_count = (k+1)*(k)//2

al = (all_count-k)*(next_count)+k*count
print(al%(10**9+7))

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

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):
    a=0
    b=0
    for j in range(min(i+1,n-1),n):
        if A[i]>A[j]:
            b+=1
    for j in range(i):
        if A[i]>A[j]:
            a+=1

    ans+=(b*k+(a+b)*k*(k-1)//2)
print(ans%mod)

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

    MOD = 10**9 + 7
    count = 0
    for i in range(N-1):
        for j in range(i+1, N):
            if A[i] == A[j]:
                continue
            elif A[i] > A[j]:
                count += K + K*(K-1) // 2
            else:
                k = K - 1
                count += k + k*(k-1) // 2
        count %= MOD

    print(count)
    



main()

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

    MOD = 10**9 + 7
    count = 0
    for i in range(N-1):
        for j in range(i+1, N):
            if A[i] == A[j]:
                continue
            elif A[i] > A[j]:
                count += K + K*(K-1) // 2
            else:
                k = K - 1
                count += k + k*(k-1) // 2
            count %= MOD

    print(count)
    



main()

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

    MOD = 10**9 + 7
    count = 0
    for i in range(N-1):
        for j in range(i+1, N):
            if A[i] == A[j]:
                continue
            elif A[i] > A[j]:
                count += K + K*(K-1) // 2
            else:
                k = K - 1
                count += k + k*(k-1) // 2

    print(count % MOD)
    



main()

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

    MOD = 10**9 + 7
    count = 0
    for i in range(N-1):
        for j in range(i, N):
            if A[i] == A[j]:
                continue
            elif K == 1 and A[i] > A[j]:
                count += 1
            else:
                if A[i] > A[j]:
                    count += K + K*(K-1) // 2
                else:
                    k = K - 1
                    count += k + k*(k-1) // 2

    print(count % MOD)
    



main()

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):
    cnt = 0
    cnt2  =0
    for j in range(i):
        if a[i]>a[j]:
            cnt += 1
    for j in range(i+1,n):
        if a[i]>a[j]:
            cnt2+=1
    ans = (ans+cnt2*k*(k+1)//2+(cnt*k*(k-1)//2)%mod)%mod
print(ans)

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

Solution (Python):
from bisect import bisect_left

n, k = map(int,input().split())
a = list(map(int,input().split()))
assert len(a) == n
sa = sorted(a)
M = 10**9 + 7

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

inv_all = [0] * n
for i in range(n):
    inv_all[i] = bisect_left(sa, a[i])

def solve():
    ans = 0
    for i in range(n):
        ans += (k * inv_before[i]) % M
        ans %= M
        ans += (k * (k-1) // 2 * inv_all[i]) % M
        ans %= M
    return ans

print(solve())

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

mod = 10**9 + 7

right_numbers = [0]*N
left_numbers = [0]*N
right_ans = [0]*N
left_ans = [0]*N

right_total = K*(K+1)//2
left_total = K*(K-1)//2

for i in range(N):
    for r in range(i+1,N):
        if A[i] > A[r]:
            right_numbers[i] += 1
    for l in range(0,i):
        if A[i] > A[l]:
            left_numbers[i] += 1
    
    
    right_ans[i] = right_numbers[i] * right_total
    left_ans[i] = left_numbers[i] * left_total

"""
print(right_numbers)
print(left_numbers)
print(right_ans)
print(left_ans)
print(sum(right_ans))
print(sum(left_ans))
"""
print((sum(right_ans) + sum(left_ans)) % mod)

Solution (Python):
from collections import Counter

N, K = map(int, input().split())
A = list(map(int, input().split()))
MOD = 10 ** 9 + 7
c = Counter(A)
c = sorted(c.items(), key=lambda x: x[0])

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

out_num = 0
cnt = 0
for k, v in c:
    cnt += v
    out_num += (N - cnt) * v

ans = in_num * K + out_num * (K * (K - 1) // 2)
print(ans % MOD)

Solution (Python):
N,K = map(int,input().split())
A = list(map(int,input().split()))
cnt = [[0,0] for _ in range(N)]
for i,a in enumerate(A):
    for j,b in enumerate(A):
        if a > b:
            if i < j:
                cnt[i][1] += 1
            if i > j:
                cnt[i][0] += 1

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

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


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

cnt2 = 0
A.extend(A)
for i in range(N*2):
    for j in range(i+1, N*2):
        if A[i] > A[j]:
            cnt2 += 1
            cnt2 %= MOD

cnt2 -= cnt * 2

ans = 0
ans += cnt * K % MOD
ans += cnt2 * (K * (K-1) // 2) % MOD
print(ans % MOD)

Solution (Python):

import sys,os
from collections import defaultdict, deque
from fractions import gcd
from math import ceil, floor
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    YN = lambda c : print('Yes') if c else print('No')
    MOD = 10**9+7

    N, K = LMIIS()
    A = LMIIS()

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

    print(ans)
    

if __name__ == '__main__':
    main()

Solution (Python):








import sys
read = sys.stdin.readline
ra = range
enu = enumerate
MOD = 10**9 + 7


def read_ints():
    return list(map(int, read().split()))


def ret_n_inv(ls):
    n_inv = 0
    N = len(ls)
    for i in ra(N):
        for j in ra(i + 1, N):
            if ls[i] > ls[j]:
                n_inv += 1
    return n_inv


N, K = read_ints()
A = read_ints()


n_A = ret_n_inv(A)
if K == 1:
    print(n_A)
    exit()
n_2A = ret_n_inv(A + A)
mul = (K - 1) * K // 2 % MOD
n_skip = (n_2A - 2 * n_A) * mul % MOD
print((n_A * K + n_skip) % MOD)

Solution (Python):


























n, k = map(int, input().split())
a = list(map(int, input().split()))
MOD = 10**9 + 7
first_subarr = [0] * n
rest_arr = [0] * n
for i in range(n):
    for j in range(i, n):
        if a[i] > a[j]:
            first_subarr[i] += 1

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

diffs = [0] * n
for i in range(n):
    diffs[i] = first_subarr[i] + rest_arr[i]

ans = 0
for i in range(n):
    
    
    tmp = k * (2 * first_subarr[i] + (k - 1) * diffs[i]) // 2
    
    ans += tmp
    ans %= MOD
print(ans)

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

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

print((v1*K+v2*(K*(K-1)//2))%mod)

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

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

print((v1*K+v2*(K*(K-1)//2))%mod)

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

mod = 1000000007

ans = 0

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

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

    ans += cnt * k
    ans %= mod
    ans += cnt2 * ((k - 1) * k // 2)
    ans %= mod

print(ans)

Solution (Python):
from sys import stdin

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

a = list(a) 
count = 0 
for i in range(1, n):
    for j in range(i):
        if a[j] > a[i]:
            count += 1
import copy
b = copy.copy(a)
b.sort()
count *= k
import bisect
ans2 = 0
for i in range(n):
  ans2 += bisect.bisect_left(b, a[i])
print((count + ans2 * (((k-1)* k)//2))%(1000000007))

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

def sum_n(n):
    return (n * (n+1))//2

ans = 0
left = [0] * N
right = [0] * N
for i in range(N):
    a = Alist[i]
    in_left = True
    for j in range(N):
        if i == j:
            in_left = False
            continue
        else:
            if Alist[j] < a:
                if in_left:
                    left[i] += 1
                else:
                    right[i] += 1
    ans += right[i] * sum_n(K)
    ans += left[i] * sum_n(K-1)
    ans %= mod
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(300000)
from collections import defaultdict

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


N, K = MI()
A = LMI()



P = [0] * N
U = [0] * N
d = defaultdict(int)
for i, a in enumerate(reversed(A)):
    i = N - 1 - i
    cnt = 0
    for j in range(a):
        cnt += d[j]
    P[i] = cnt
    d[a] += 1
for i, a in enumerate(A):
    cnt = 0
    for j in range(a):
        cnt += d[j]
    U[i] = cnt

ans = 0
for i in range(N):
    v = K * P[i] + U[i] * (K * (K - 1) // 2)
    ans += v
    ans %= MOD
print(ans)

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

Solution (Python):
def count_section_by_zero(data):
  count = 0
  flg = False
  start = 0
  for i, d in enumerate(data):
    if flg is False and d != 0:
      count += 1
      flg = True
      
    if d == 0:
      flg = False
  return count

def input_list():
  return list(map(int, input().split()))

def input_list_str():
  return list(map(str, input().split()))

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


def divide_two(arg):
  c = 0
  while True:
    if c >= 2:
      break
    if arg % 2 != 0:
      break
    arg //= 2
    c += 1
  return c


def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

def main():
  n,k = input_list()
  a = input_list()
  mod = 10**9+7
  ans = 0
  for i in range(n):
    for j in range(n):
      if i < j and a[i] > a[j]:
        ans += k*(k-1) // 2 + k
        ans %= mod
      elif i > j and a[i] > a[j]:
        ans += k*(k-1) // 2
        ans %= mod
  print(ans)
import math
import fractions
import collections
import itertools
from functools import reduce
main()

Solution (Python):
import copy
N,K=map(int,input().split())
A=list(map(int,input().split()))
tentou1=0
tentou2=0
l2=copy.deepcopy(A)
l2.extend(A)
p=10**9+7

for i in range(N):
    for j in range(i+1,N):
        
        if A[i]>A[j]:
            tentou1=tentou1+1
for i in range(2*N):
    for j in range(i+1,2*N):
        if l2[i]>l2[j]:
            tentou2=tentou2+1

n=(tentou2-tentou1)-tentou1



print((K*(3*tentou1-tentou2)+n*(K*(K+1))//2)%p)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from bisect import bisect_left


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

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

    def range(self, l, r):  
        return sum(r) - sum(l - 1)

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


n, k = map(int, readline().split())
a = list(map(int, readline().split()))
s_a = sorted(a)
mod = 10 ** 9 + 7
bit = BIT(n)
res = [0] * n
ans = 0
for i, b in enumerate(a):
    j = bisect_left(s_a, b)
    res[i] = i - bit.sum(j)
    bit.add(j, 1)
for i, b in enumerate(a):
    ans += k * res[i]
    ans %= mod
    j = bisect_left(s_a, b)
    ans += (k * (k - 1) // 2 % mod) * (n - bit.sum(j))
    ans %= mod
print(ans)

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

N, K = map(int, input().split())
A = [int(i) for i in input().split()]

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

y1 = (K * (K+1) // 2) % P
y2 = ((K-1) * K // 2) % P

ans = (x1*y1 + x2*y2) % P
print(ans)

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

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

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

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

def resolve():
  ans = 0
  q1 = 0
  n, k = map(int, input().split())
  a = list(map(int, input().split()))
  c = [0] * n  
  for i in range(n):
    for j in range(i, n):
      if a[i] > a[j]:
        q1 += 1  

  ans = q1 * k % MOD

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

  s = (k * (k - 1)) // 2
  s %= MOD

  ans += (s * q2) % MOD

  print(int(ans % MOD))
  return  

if __name__ == "__main__":
  resolve()

Solution (Python):
N,K=map(int,input().split())
A=list(map(int,input().split()))
MOD=10**9+7
ans=0
C=[0 for _ in range(max(A)+1)]
for i in range(N-1):
    for j in range(i+1,N):
        a=A[j]-A[i]
        if a<0:
            
            C[A[i]]+=1
D=[0 for i in range(max(A)+1)]
for i in range(N):
    for j in range(N):
        if i==j:
            continue
        a=A[i]-A[j]
        if a>0:
            D[A[i]]+=1

for i in range(len(C)):
    a=K*C[i]%MOD
    ans+=a
k=(K*(K-1))//2
k=k%MOD
for i in range(len(D)):
    b=k*D[i]%MOD
    ans+=b
print(ans%MOD)

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

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

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

tmp1 = (K-1)*K//2 %mod * q2 %mod
tmp2 = q1*K%mod
ans = (tmp1 + tmp2)%mod
print(int(ans))

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

Before = []
After = []

for i in range(n):
    now = A[i]
    before = 0
    after = 0
    for j in range(i):
        if now > A[j]:
            before += 1
    for l in range(i+1,n):
        if now > A[l]:
            after += 1
    Before.append(before)
    After.append(after)

ans = 0
mod = 10**9+7
for i in range(n):
    if k%2 == 0:
        multi_one = int(k/2) * (k-1) % mod
        multi_two = int(k/2) * (k+1) % mod 
    else:
        multi_one = int((k-1)/2) * k % mod
        multi_two = int((k+1)/2) * k % mod
    plus = multi_one*Before[i]%mod + multi_two*After[i]%mod
    ans = (ans + plus) % mod

print(ans)

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

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

if __name__ == '__main__':
    solve()

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

def main():
    N,M = map(int,input().split())
    a = list(map(int,input().split()))
    MOD = 10**9+7
    
    num = 0
    for i in range(N):
        for j in range(i+1,N):
            if a[i] > a[j]:
                num += 1

    if M == 1:
        print(num%MOD)
    else:
        num *= M
        num %= MOD
        a += a
        count = 0
        for i in range(N):
            for j in range(N,2*N):
                if a[i] > a[j]:
                    count += 1

        count *= ((M*(M-1))//2)
        count %= MOD
        num += count
        print(num%MOD)
    
if __name__ == "__main__":
    main()

Solution (Python):
n,k = map(int,input().split())
a = list(map(int,input().split()))
a = a
b = a*2
a1 = 0
a2 = 0
m = 10**9+7
for i in range(n):
    a1 += len([j for j in a[i:] if j < a[i]])
for i in range(2*n):
    a2 += len([j for j in b[i:] if j < b[i]])

ans =((a1*k*(k+1)//2) + ((a2 - 3*a1) * (k-1)*k//2 % m) % m) % m

print(ans)

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

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

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

Solution (Python):
def main():
    length, repeat = map(int, input().split())
    sequence = list(map(int, input().split()))
    mod = 10 ** 9 + 7
    base_inversion = 0
    inter_inversion = 0
    count = [0] * 2001
    for i in range(length):
        count[sequence[i]] += 1
        for j in range(i + 1, length):
            if sequence[i] > sequence[j]:
                base_inversion += 1
    for i in range(length):
        inter_inversion += sum(count[:sequence[i]])
    print((base_inversion * repeat + inter_inversion * repeat * (repeat - 1) // 2) % mod)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from collections import Counter
def input():
    return sys.stdin.readline()[:-1]


def main():

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

    fall_count_in = 0
    fall_count_out = 0
    for i,ai in enumerate(A):
        for j,aj in enumerate(A):
            if i == j:
                continue
            if ai > aj:
                fall_count_out += 1
                if i < j:
                    fall_count_in += 1

    ans = fall_count_out * K * (K-1) // 2 + fall_count_in * K
    print(ans % MOD)

if __name__ == "__main__":
    main()

Solution (Python):
n,k=map(int,input().split())
l=list(map(int,input().split()))
sl=sorted(l)
ten=0
jiten=0
for i in range(n):
    for j in range(n):
        if l[i]>l[j]and i<j:ten+=1
        if l[i]<l[j]:jiten+=1
print((ten*k+jiten*(k*(k-1))//2)%1000000007)

Solution (Python):
n,k=map(int,input().split())
l=list(map(int,input().split()))
sl=sorted(l)
ten=0
for i in range(n):
    for j in range(n):
        if l[i]>l[j]and i<j:ten+=1
pre=sl[0]
jiten=0
c=0
for i,x in enumerate(sl):
    if x==pre:jiten+=c
    else:
        jiten+=i
        c=i
        pre=x

print((ten*k+jiten*(k*(k-1))//2)%(10**9+7))

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

if __name__ == "__main__":
    main()

Solution (Python):
n, k, *a = map(int, open(0).read().split())

MOD = int(1e9+7)
m = MOD -2
re = 1
s = 2
while m:
        if m&1: re = re*s % MOD
        s = s * s % MOD
        m >>= 1
f = (k+1) * k * re
g = k * (k-1) * re

l = r = 0
for i in range(n):
        for j in range(n):
                if a[i]>a[j]:
                        if i>j: l += 1
                        else: r += 1
print((l*g+r*f) % MOD)

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

    
    bit = BinaryIndexedTree(2001)
    t1 = Mint()
    for i,a in enumerate(reversed(A)):
        t1 += bit.query(a-1)
        bit.add(a,1)
    
    
    t2 = Mint()
    for a in A:
        t2 += bit.query(a-1)

    K2 = K * (K - 1) // 2
    ans = t1 * K + t2 * K2
    print(ans)

class BinaryIndexedTree:
    def __init__(self, n=None, f=lambda x,y:x+y, zero=0, initial_values=None):
        assert(n or initial_values)
        self.__f, self.__z, = f, zero
        self.__n = n if n else len(initial_values)
        self.__dat = [zero] * (self.__n + 1)
        if initial_values:
            for i in range(1, self.__n + 1): self.add(i, initial_values[i-1]) 

    def add(self, i, v):
        i += 1
        while i <= self.__n:
            self.__dat[i] = self.__f(self.__dat[i], v)
            i += -i&i

    def query(self, r):
        r += 1
        ans = self.__z
        while r:
            ans = self.__f(ans, self.__dat[r])
            r -= -r&r
        return ans
    
class Mint:
    def __init__(self, value=0, mod=10**9+7):
        self.value = ((value % mod) + mod) % mod
        self.mod = mod

    @staticmethod
    def get_value(x): return x.value if isinstance(x, Mint) else x

    def inverse(self):
        a, b = self.value, self.mod
        u, v = 1, 0
        while b:
            t = a // b
            b, a = a - t * b, b
            v, u = u - t * v, v
        return (u + self.mod) % self.mod

    def __repr__(self): return str(self.value)
    def __eq__(self, other): return self.value == other.value
    def __neg__(self): return Mint(-self.value, self.mod)
    def __hash__(self): return hash(self.value)
    def __bool__(self): return self.value != 0

    def __iadd__(self, other):
        self.value = (self.value + Mint.get_value(other)) % self.mod
        return self
    def __add__(self, other):
        new_obj = Mint(self.value, self.mod)
        new_obj += other
        return new_obj
    __radd__ = __add__

    def __isub__(self, other):
        self.value = (self.value - Mint.get_value(other) + self.mod) % self.mod
        return self
    def __sub__(self, other):
        new_obj = Mint(self.value, self.mod)
        new_obj -= other
        return new_obj
    def __rsub__(self, other):
        new_obj = Mint(Mint.get_value(other), self.mod)
        new_obj -= self
        return new_obj

    def __imul__(self, other):
        self.value = self.value * Mint.get_value(other) % self.mod
        return self
    def __mul__(self, other):
        new_obj = Mint(self.value, self.mod)
        new_obj *= other
        return new_obj
    __rmul__ = __mul__

    def __ifloordiv__(self, other):
        other = other if isinstance(other, Mint) else Mint(other, self.mod)
        self *= other.inverse
        return self
    def __floordiv__(self, other):
        new_obj = Mint(self.value, self.mod)
        new_obj //= other
        return new_obj
    def __rfloordiv__(self, other):
        new_obj = Mint(Mint.get_value(other), self.mod)
        new_obj //= self
        return new_obj

if __name__ == '__main__':
    main()

Solution (Python):
from fractions import gcd
from math import sqrt,asin,degrees
from collections import deque,defaultdict,Counter
from heapq import heapify,heappop,heappush
from copy import deepcopy
from itertools import accumulate,permutations
from bisect import bisect_left,bisect_right

N, K = map(int,input().split())
A = [int(i) for i in input().split()]

MOD = 10**9 + 7

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

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

tmp = (K * (K - 1)) // 2 % MOD

ans = (K * B) % MOD
ans = (ans + tmp * C) % MOD

print(ans)

Solution (Python):

mod = 1000000007

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

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

    print(ans)


if __name__ == '__main__':
    main()