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.



Sample input:
2 2
2 1


Sample output:
3

Submission status: Wrong Answer

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

MOD = 10**9 + 7

ansR = 0
ansL = 0

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


ansR %= MOD
ansL %= MOD
Rn = int((k+1)*k)%MOD/2
Ln = int((k-1)*k)%MOD/2

ans = int(ansR*Rn)%MOD + int(ansL*Ln)%MOD

print(int(ans%MOD))

Submission status: Wrong Answer

Solution code:
MOD = 1e+9 + 7
N, K = map(int, input().split())
Alist = list(map(int, input().split()))

Alist_sorted = sorted(Alist)
tmp = 0
tmp2 = 0
ans = 0
KK = (K * (K-1) / 2) % MOD
for i in range(1, len(Alist)):
    if Alist_sorted[i] == Alist_sorted[i-1]:
        tmp2 += 1
    else:
        tmp2 = 0
    tmp = (i - (tmp2))%MOD
    ans += (tmp * KK) % MOD

Alist = Alist[::-1]
tmp = 0
for i in range(1, len(Alist)):
    for j in range(i):
        if Alist[i] > Alist[j]:
            tmp += 1

ans += tmp * K
ans = ans % MOD
print(int(ans))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = [int(i) for i in input().split()]

mod = 10**9+7

g = 0
for i in range(n):
    for j in range(n):
        if i == j:
            continue
        elif i >= j:
            continue
        if a[i] > a[j]:
            #print(i,j)
            g += 1

if k == 1:
    print(g)
    exit()
    
a2 = []

for i in range(2*n):
    a2.append(a[i%n])
    
f = 0

for i in range(n):
    for j in range(2*n):
        if i == j:
            continue
        elif i >= j:
            continue
        if a2[i] > a2[j]:
            #print(i,j)
            f += 1

t = int((f-g)%mod)
k = int(k%mod)

#print(t)
#exit()
#print(int((k*g%mod + t*(0.5*((k-1)*k))%mod)%mod))
#print(int(( (k%mod*g%mod)%mod + (t*(0.5*((k-1)%mod*k%mod)) ) %mod)%mod))
#print(int((k*g%mod + t*(0.5*((k-1)%mod*k%mod))%mod)%mod))
#print(int((k*g%mod + t*(0.5*((k-1)%mod*k%mod))%mod)%mod))
ans1 = int((k*g%mod)%mod)
#ans2 = t*(0.5* (k-1)%mod * k%mod ) %mod
#ans2 = t*(0.5*((k-1)%mod*k%mod))%mod
ans2 = int(t*(((k-1)%mod*k%mod)%mod)//2%mod)
print( int( (ans1%mod + ans2%mod)%mod ))
#print(int(((k*g) + (t*(0.5*((k-1)*k))))%mod))
exit()

Submission status: Wrong Answer

Solution code:
from itertools import groupby

[N, K] = map(int, input().split())
As = list(map(int, input().split()))

g = [len(list(x[1])) for x in groupby(sorted(As))]

tmp = 0
count1 = 0
for x in g:
    count1 += x * tmp
    tmp += x

count1 *= int((K - 1) * K / 2)
count1 %= 1000000007

count2 = 0
for i in range(len(As)):
    count2 += len(list(filter(lambda x: As[i] > x, As[i:])))

count2 *= K
count2 %= 1000000007

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

Submission status: Wrong Answer

Solution code:
from numpy import array
N, K = list(map(int, input().split()))
A = array(list(map(int, input().split())))
div = 1000000007
s = 0
s2 = 0
for i, n in enumerate(A):
    tmp = A < n
    s += int(sum(tmp[i+1:]))
    s2 += int(sum(tmp))

print(int((s * K % div + s2 * (K-1) * K / 2 % div) % div))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
ppp = list(map(int, input().split()))
ans_ = 0

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

mod = 10 ** 9 + 7
ans = ans_ * k

k_ = int(k * (k-1) / 2)

for i in ppp:
    tmp = 0
    for j in ppp:
        if i > j:
            tmp += 1
    ans += tmp * k_
    ans %= mod

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = input().split()
l = list(map(int,input().split()))
n = int(n)
k = int(k)

rn_0 , rn_2 = 0, 0

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

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

rn = rn_0 * k*(k-1)/2 + rn_2 * k
print(int(rn%(10**9+7)))

Submission status: Wrong Answer

Solution code:
import bisect
from collections import deque
import sys
MAX_INT = int(10e9)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N,K = IL()
a = IL()

cnt = 0
for left in range(N):
  for right in range(left+1,N):
    if a[left] > a[right]:
      cnt += 1
cnt = (cnt*K) %mod

a.sort()
tmp = 0
for i in range(N-1):
  if a[i+1] > a[i]:
    tmp = i+1
  cnt += (K-1)/2 * (2*tmp+(K-2)*tmp)   %mod

print(cnt %mod)

"""
2 1000000006
2 1

10 3
10 9 8 7 6 5 4 3 2 1

cnt += (K-1)/2 * (2*tmp+(K-2)*tmp)   %mod

"""

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
 
a=list(map(int,input().split()))
tentou1=0
for i in range(N):
  for j in range(i+1,N):
    if(a[i]>a[j]):
      tentou1+=1
stentou=0
for i in range(N):
  for j in range(N):
    if(a[i]>a[j]):
      stentou+=1
answer=int(tentou1*K + stentou*K*(K-1)/int(2))
print(answer%1000000007)

Submission status: Wrong Answer

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

MOD = 10**9 + 7

ansR = 0
ansL = 0

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


ansR %= MOD
ansL %= MOD
Rn = (k+1)*k%MOD/2
Ln = (k-1)*k%MOD/2

ans = int(ansR*Rn)%MOD + int(ansL*Ln)%MOD

print(int(ans%MOD))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

import numpy as np

MOD = 10**9 + 7

N,K = map(int,input().split())
A = np.array(input().split(), dtype=np.int64)

# 同じグループ内
def type_1():
    bl1 = A[:,None] > A[None,:]
    bl2 = np.arange(N,dtype=np.int64)[:,None] < np.arange(N,dtype=np.int64)[None,:]
    x = ((bl1 & bl2)*1).astype(np.int64).sum() % MOD
    return x*K%MOD

def type_2():
    # グループ間
    counter = np.bincount(A)
    cum = counter.cumsum()
    larger = N-cum
    x = (larger * counter % MOD).sum() % MOD
    y = int(K*(K-1)/2) % MOD
    return x*y%MOD

answer = (type_1() + type_2()) % MOD
print(answer)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
lis_a = list(map(int, input().split()))
mod = 1000000007
ans = 0
temp = 0
for i in range(len(lis_a)-1):
    for j in range(len(lis_a)):
        if(lis_a[i] > lis_a[j]):
            temp += 1
ans = (k*(temp+temp*k)//2) % mod
    
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys
from functools import lru_cache

def main():
    count = 0

    n,k = map(int, input().split(' '))
    a = [i for i in map(int, input().split(' '))]

    for i in range(len(a)):
        for j in range(len(a)):
            # print('--- ---', i,j)
            if i == j:
                continue
            if a[i] > a[j]:
                
                if i < j:
                    plus = int(k*(k+1)/2)
                    count += plus

                elif i > j:
                    plus = int(k*(k-1)/2)
                    count += plus


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



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
 
a=list(map(int,input().split()))
tentou1=0
for i in range(N):
  for j in range(i+1,N):
    if(a[i]>a[j]):
      tentou1+=1
stentou=0
for i in range(N):
  for j in range(N):
    if(a[i]>a[j]):
      stentou+=1
answer=int(tentou1*K + stentou*K*(K-1)/2)
print(answer%1000000007)

Submission status: Wrong Answer

Solution code:
import math, string, itertools, fractions, heapq, collections, re, \
array, bisect, sys, random, time, copy, functools, os, queue, pdb
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def _pf(s): return print(s, flush=True)

N, K = LI()
A = LI()

"""
2 1なら
2 1 2 1
点灯数は、左の数のほうが多いもの
2 1
2 1
2 1
の3種類
2回つなげてみたら決まる
2 1 2 1なら
K = 2で3
K=3 で 2 1 2 1 2 1で6
K=4 で2 1 2 1 2 1 2 1で10通り 4+3+2+1=
K=6なら 2 1 2 1 2 1 2 1 2 1 2 1で6+5+4+3+2+1 = 

偶奇でわける
modで割る

1 2なら
1 2 1 2で1しかない1
1 2 1 2 1 2なら3で2 + 1
1 2 1 2 1 2 1 2 なら6で3+2+1
"""
if len(set(A)) == 1:
    print(0)
    exit()

if N == 1:
    print(0)
    exit()
"""
各数字ごとに数える
"""
in_count = 0
for i in range(N):
    for j in range(i, N):
        if A[i] > A[j]:
            #  print('i, j', i,j, A[i], A[j])
            in_count += 1
#  print(in_count)
in_ans = in_count * K
in_ans %= mod

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

out_ans = (out_count * K * (K-1))/2
out_ans %= mod
#  print(in_ans, out_ans)
print(int((in_ans + out_ans)%mod))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A= list(map(int,input().split()))
a1 = 0
for i in range(len(A)):
  for j in range(len(A)):
    if (A[i] > A[j]) and (j>i):
      a1 += 1
    else :
      a1 += 0
ak = K*(K + 1)*(a1)/2
print(ak%1000000007)

Submission status: Wrong Answer

Solution code:
def count_inversion(sequence):
    count = 0
    for i in range(1,len(sequence),1):
        for j in range(i,len(sequence),1):
            if sequence[i-1] > sequence[j]:
                count += 1
    else:
        count = count%MAX
    return count%MAX

def count_bigger(sequence):
    count = 0
    for num1 in sequence:
        for num2 in sequence:
            if num1 > num2:
                count += 1
    else:
        count = count%MAX
    return count%MAX

MAX = 7 + 10**9
N, K = map(int,input().split())
A = list(map(int,input().split()))
ans = ((K%MAX)*count_inversion(A))%MAX + (((K*(K-1)/2)%MAX)*count_bigger(A))%MAX
print(int(ans%MAX))

Submission status: Wrong Answer

Solution code:
def main():
    def c(x,y):
        if x > y:
            return 1
        else:
            return 0
    n,k = map(int,input().split())
    x = 1000000007
    kk = int((k)%x*((k-1)/2)%x)
    a = list(map(int,input().split()))
    c1 = []
    c2 = []
    for i in range(n):
        l = [c(a[i],p) for p in a]
        c1.append(sum(l))
        c2.append(sum(l[i:]))
    ans = ((sum(c1)%x)*kk)%x+((sum(c2)%x)*k)%x
    print(ans%x)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,K =map(int,input().split())
a = list(map(int,input().split()))
# print(a*K)
count = 0
dis = 0
mod = (10**9+7)
for i in a:
    for k in a:
        if k < i:
            count +=1
        count %= mod
for i in range(N):
    for k in a[i:]:
        if k<a[i]:
            dis += 1
        dis %= mod
a = dis * K % mod
b = count*K*(K-1) % mod //2
ans = (a+b)% (10**9+7)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
N, K = map(int, input().split())
A_l = list(map(int, input().split()))
A = np.array(A_l)
count_ini = 0
for i in range(N-1):
    count_ini += np.count_nonzero(A[i::] < A[i])
        
count_r = 0
A_sorted = np.sort(A)
for i in range(N-1):
    count_r += np.count_nonzero(A_sorted < A[i])

count = (count_ini * K + K * (K - 1) // 2 * count_r) % (10 ** 9 + 7)

print(int(count))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A= list(map(int,input().split()))
a1 = 0
for i in range(len(A)):
  for j in range(len(A)):
    if (A[i] > A[j]) and (j>i):
      a1 += 1
    else :
      a1 += 0
ak = K*(K + 1)*(a1)/2
print(ak%100000007)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
A = list(map(int,input().split()))
def count_inversion(sequence):
    count = 0
    for i in range(N):
        for j in range(i,N):
            if sequence[i] > sequence[j]:
                count += 1
    return count
def cnt_bigger_elem(sequence):
    cnt = 0
    for i in range(N):
        for elem in sequence:
            if sequence[i] < elem:
                cnt += 1
    return cnt

BIG_NUM = 10**9+7

ans = int(((((count_inversion(A)%BIG_NUM) * (K%BIG_NUM))%BIG_NUM) + (((((K%BIG_NUM)*((K-1)%BIG_NUM))/2)%BIG_NUM)*(cnt_bigger_elem(A)%BIG_NUM))))%BIG_NUM

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
count1 = 0
count2 = 0
for j in range(n-1):
  count1 += len([i for i in a[j:] if i < a[j]])
  a2 = a*2
  count2 += len([i for i in a2[j:] if i < a2[j]])
sa = count2-count1*2
if count1>0:
  ans += count1*((1+k)*k//2)
  ans += sa*((1+(k-1))*(k-1)//2)
print(int(ans%(1000000007)))

Submission status: Wrong Answer

Solution code:
_, K = map(int, input().split())
a = list(map(int, input().split()))
s = [sum([b > c for c in a]) for b in a]
l = [sum([l < a[i] for l in a[:i]]) for i in range(1, len(a))]
l.insert(0, 0)
r = [b - c for b, c in zip(s, l)]
a = (sum(r)*(1+K) + sum(l)*(K-1))*K/2
print(int(a%(1e9+7)))

Submission status: Wrong Answer

Solution code:
import math

def A():
    temp = input().split()
    M = int(temp[0])
    D = int(temp[1])

    if D<22:
        print(0)
        return
    
    ans = 0

    for i in range(1, M+1):
        for j in range(22, D+1):
            if (j%10) >= 2 and (j%10)*int(j/10) == i:
                ans += 1
    
    print(ans)

def B():
    temp = input().split()
    N = int(temp[0])
    K = int(temp[1])
    A = [int(i) for i in input().split()]

    basic_inv = 0
    for i in range(0, len(A)):
        for j in range(i+1, len(A)):
            if A[i] > A[j]:
                basic_inv += 1
    
    if K == 1:
        print(basic_inv)
        return

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

    
    s = int((K*(K-1))/2)
    num = s
    k_mod = K
    print(int(k_mod*basic_inv + num*inv)%(10**9+7))

    # #check
    # full = A*K
    # sum = 0
    # for i in range(0, len(full)):
    #     for j in range(i+1, len(full)):
    #         if full[i] > full[j]:
    #             sum += 1
    #             sum %= (10**9+7)
    
    # print("check:", sum)

B()

Submission status: Wrong Answer

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

mod = 10 ** 9 + 7
b = [[0, 0] for _ in range(n)]
for i in range(n):
    for j in range(n):
        if a[i] > a[j]:
            if i > j:
                b[i][0] += 1
            else:
                b[i][1] += 1
ans = 0
sigmakp = int(k*(k+1)/2) % mod
sigmakn = int(k*(k-1)/2) % mod
for i in range(n):
    ans = (ans + sigmakp * b[i][1]) % mod
    ans = (ans + sigmakn * b[i][0]) % mod
print(ans%mod)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
import copy
asort = copy.copy(a)
asort.sort()
x = 0
y = 1
t = 0
for i in range(len(a) - 1):
    if asort[i] == asort[i + 1]:
        y += 1
    else:
        x += (len(a) - i - 1) * y
        y = 1
x1 = x * k * (k - 1) / 2#外部
for i in range(len(a) - 1):
    for j in range(len(a) - i - 1):
        if a[i] > a[i + j + 1]:
            t += 1#内部
x2 = x1 + (t * k)
x3 = x2 % (10 ** 9 + 7)
print(x3)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
ppp = list(map(int, input().split()))
ans_ = 0

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

mod = 10 ** 9 + 7
ans = (ans_ * k) % mod

k_ = int(k * (k-1) / 2) % mod

for i in ppp:
    tmp = 0
    for j in ppp:
        if j < i:
            tmp += 1
    ans += tmp * k_
    ans %= mod

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
import queue
#import numpy as np
Mod = 1000000007




def main(): #startline-------------------------------------------
    n,k = map(int,input().split())
    a = list(map(int,input().split()))
    ans = 0
    for i in range(n):
        for j in range(n-1,i,-1):
            if a[j] < a[j-1]:
                a[j],a[j-1] = a[j-1],a[j]
                ans += 1
    ans %= Mod
    u = Counter(a)
    ul = list(u.keys())
    ul.sort(reverse = True)
    p = len(ul)   
    ans *= k
    ans %= Mod
    temp = 0
    for i in range(p):
        temp += u[ul[i]]
        ans %= Mod
        ans += (n-temp)*(k*(k-1)//2)
        ans %= Mod
    print(ans)

if __name__ == "__main__":
    main() #endline===============================================





def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n < 2 is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))):
        if prime[i] == 1:
            for j in range(2*i,n):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n):
        if prime[i] == 1:
            res.append(i)
    return res

class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py

    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)

Submission status: Wrong Answer

Solution code:
mod=10**9+7
n, k=map(int, input().split())
a=list(map(int, input().split()))
jibun=0
for i in range(n-1):
    for j in range(i+1, n):
        if a[i]>a[j]:
            jibun=(jibun+1)%mod

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

ans=jibun*k+saki*int(k*(k-1)/2)
print(ans%mod)

Submission status: Wrong Answer

Solution code:
import numpy as np
N,K = map(int,input().split())
A = list(map(int,input().split()))


diff = []
npA = np.array(A)
lenA = len(A)
for a in A:
    temp = list(np.array([a for i in range(lenA)]) - npA)
    diff.append(temp)

D = np.array(diff)

upD = np.triu(D,k=1)

upC = np.count_nonzero(upD>0)
dwC = np.count_nonzero(upD<0)

ans = int((K*(K+1)/2)*(dwC+upC) - (K+1)*dwC)
print(ans%(1000000007))

Submission status: Wrong Answer

Solution code:
N,K =map(int,input().split())
a = list(map(int,input().split()))
# print(a*K)
count = 0
dis = 0
for i in a:
    for k in a:
        if k < i:
            count +=1
        count %= (10 ** 9 + 7)
for i in range(N):
    for k in a[i:]:
        if k<a[i]:
            dis += 1
        dis %= (10**9+7)
a = dis * K % (10**9+7)
b = count*K*(K-1)% (10**9+7)
ans = (a+b//2)% (10**9+7)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = [int(i) for i in input().split()]

mod = 10**9+7

g = 0
for i in range(n):
    for j in range(n):
        if i == j:
            continue
        elif i >= j:
            continue
        if a[i] > a[j]:
            #print(i,j)
            g += 1

if k == 1:
    print(g)
    exit()
    
a2 = []

for i in range(2*n):
    a2.append(a[i%n])
    
f = 0

for i in range(n):
    for j in range(2*n):
        if i == j:
            continue
        elif i >= j:
            continue
        if a2[i] > a2[j]:
            #print(i,j)
            f += 1

t = int((f-g)%mod)
k = int(k%mod)

#print(t)
#exit()
#print(int((k*g%mod + t*(0.5*((k-1)*k))%mod)%mod))
#print(int(( (k%mod*g%mod)%mod + (t*(0.5*((k-1)%mod*k%mod)) ) %mod)%mod))
#print(int((k*g%mod + t*(0.5*((k-1)%mod*k%mod))%mod)%mod))
#print(int((k*g%mod + t*(0.5*((k-1)%mod*k%mod))%mod)%mod))
ans1 = int((k*g%mod)%mod)
#ans2 = t*(0.5* (k-1)%mod * k%mod ) %mod
#ans2 = t*(0.5*((k-1)%mod*k%mod))%mod
ans2 = int(t*(0.5*((k-1)%mod*k%mod)%mod)%mod)
print( int( (ans1%mod + ans2%mod)%mod ))
#print(int(((k*g) + (t*(0.5*((k-1)*k))))%mod))
exit()

Submission status: Wrong Answer

Solution code:
import math

def A():
    temp = input().split()
    M = int(temp[0])
    D = int(temp[1])

    if D<22:
        print(0)
        return
    
    ans = 0

    for i in range(1, M+1):
        for j in range(22, D+1):
            if (j%10) >= 2 and (j%10)*int(j/10) == i:
                ans += 1
    
    print(ans)

def B():
    temp = input().split()
    N = int(temp[0])
    K = int(temp[1])
    A = [int(i) for i in input().split()]

    basic_inv = 0
    for i in range(0, len(A)):
        for j in range(i+1, len(A)):
            if A[i] > A[j]:
                basic_inv += 1
    
    if K == 1:
        print(basic_inv)
        return

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

    
    s = int((K*(K-1))/2)
    num = s
    k_mod = K
    print((k_mod*basic_inv + num*inv)%(10**9+7))

    # #check
    # full = A*K
    # sum = 0
    # for i in range(0, len(full)):
    #     for j in range(i+1, len(full)):
    #         if full[i] > full[j]:
    #             sum += 1
    #             sum %= (10**9+7)
    
    # print("check:", sum)

B()

Submission status: Wrong Answer

Solution code:
mod=10**9+7
n,K=map(int,input().split())
A=list(map(int,input().split()))
s=0
for i in range(1,n):
    k=i
    while A[k]<A[k-1]:
        A[k],A[k-1]=A[k-1],A[k]
        k-=1
        s+=1
        if k==0:
            break
j=0
l=1
S=0
while j<n-1:
    if A[j]==A[j+1]:
        l+=1
        j+=1
    else:
        S=S+l*(n-1-j)
        l=1
        j+=1
C=(s*K)%mod
D=(K-1)*K/2
D=int(D)%mod
S=S%mod
E=S*D%mod
print((C+E)%mod)

Submission status: Wrong Answer

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

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

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


n, k = map(int, input().split())
ppp = list(map(int, input().split()))
bit = Bit(max(ppp))
ans_ = 0

for i, p in enumerate(ppp):
    bit.add(p, 1)
    ans_ += i + 1 - bit.sum_(p)

mod = 10 ** 9 + 7
ans = (ans_ * k) % mod

k_ = int(k * (k-1) / 2) % mod

for i in ppp:
    tmp = 0
    for j in ppp:
        if j < i:
            tmp += 1
    ans += tmp * k_
    ans %= mod

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = [int(i) for i in input().split()]

mod = 10**9+7

g = 0
for i in range(n):
    for j in range(n):
        if i == j:
            continue
        elif i >= j:
            continue
        if a[i] > a[j]:
            #print(i,j)
            g += 1

if k == 1:
    print(g)
    exit()
    
a2 = []

for i in range(2*n):
    a2.append(a[i%n])
    
f = 0

for i in range(n):
    for j in range(2*n):
        if i == j:
            continue
        elif i >= j:
            continue
        if a2[i] > a2[j]:
            #print(i,j)
            f += 1

t = (f-g)%mod
k = k%mod

#print(t)
#exit()
#print(int((k*g%mod + t*(0.5*((k-1)*k))%mod)%mod))
#print(int(( (k%mod*g%mod)%mod + (t*(0.5*((k-1)%mod*k%mod)) ) %mod)%mod))
#print(int((k*g%mod + t*(0.5*((k-1)%mod*k%mod))%mod)%mod))
#print(int((k*g%mod + t*(0.5*((k-1)%mod*k%mod))%mod)%mod))
ans1 = (k*g%mod)%mod
#ans2 = t*(0.5* (k-1)%mod * k%mod ) %mod
#ans2 = t*(0.5*((k-1)%mod*k%mod))%mod
ans2 = t*(0.5*((k-1)%mod*k%mod)%mod)%mod
print( int( (ans1%mod + ans2%mod)%mod ))
#print(int(((k*g) + (t*(0.5*((k-1)*k))))%mod))
exit()

Submission status: Wrong Answer

Solution code:
# kleene Inversion

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


count_n = 0
count_k = 0

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

    for l in range(0, n):
        if a[i] > a[l]:
            count_k += 1

count = count_n * k + count_k * k*(k-1)//2

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

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
A = list(map(int, input().split()))
sorted_A = sorted(A)

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

inv_sorted_A = 0
for i in range(0,N):
  for j in range(0,N):
    if i<j and sorted_A[i] < sorted_A[j]:
      inv_sorted_A += 1

count = (inv_sorted_A * round((K * (K-1))/2)  + inv_A * K ) % 1000000007

print(count)

Submission status: Wrong Answer

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

MOD = 10**9 + 7

ansR = 0
ansL = 0

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


ansR %= MOD
ansL %= MOD
Rn = (k+1)*k%MOD/2
Ln = (k-1)*k%MOD/2

ans = (ansR*Rn)%MOD + (ansL*Ln)%MOD

print(int(ans%MOD))

Submission status: Wrong Answer

Solution code:
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
two_tento = 0
for i in range(n):
    for j in range(n):
        if array[i] > array[j]:
            two_tento += 1

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

Submission status: Wrong Answer

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

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

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


n, k = map(int, input().split())
ppp = list(map(int, input().split()))
bit = Bit(3000)
ans_ = 0

for i, p in enumerate(ppp):
    bit.add(p, 1)
    ans_ += i + 1 - bit.sum_(p)

mod = 10 ** 9 + 7
ans = (ans_ * k) % mod

k_ = int(k * (k-1) / 2) % mod

for i in ppp:
    tmp = 0
    for j in ppp:
        if j < i:
            tmp += 1
    ans += tmp * k_
    ans %= mod

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
A = list(map(int,input().split()))
def count_inversion(sequence):
    count = 0
    for i in range(N):
        for j in range(i,N):
            if sequence[i] > sequence[j]:
                count += 1
    return count
def cnt_bigger_elem(sequence):
    cnt = 0
    for i in range(N):
        for elem in sequence:
            if sequence[i] < elem:
                cnt += 1
    return cnt

def cnt_smaller_elem(sequence):
    cnt = 0
    for i in range(N):
        for elem in sequence:
            if sequence[i] > elem:
                cnt += 1
    return cnt


#A = [1,2,3,5,4]

#print(cnt_bigger_elem(A))
#print(count_inversion(A))
BIG_NUM = 10**9+7
#ans0 = int((count_inversion(A) * K)%BIG_NUM + (K*(K-1)*cnt_bigger_elem(A)/2)%BIG_NUM)
#ans1 = int((count_inversion(A) * K)%BIG_NUM + ((K*(K-1)%(BIG_NUM))*cnt_bigger_elem(A))%BIG_NUM)
ans2 = int(((((count_inversion(A)%BIG_NUM) * (K%BIG_NUM))%BIG_NUM) + (((((K%BIG_NUM)*((K-1)%BIG_NUM))/2)%BIG_NUM)*(cnt_bigger_elem(A)%BIG_NUM))))%BIG_NUM
#ans3= int(K*((K-1)*cnt_bigger_elem(A)/2)+count_inversion(A))%BIG_NUM
#ans2 = int((((count_inversion(A) * K)%BIG_NUM) + (((((K%BIG_NUM)*((K-1)%BIG_NUM))/2)%BIG_NUM)*(cnt_bigger_elem(A)%BIG_NUM))))%BIG_NUM
#print(ans2)## 惜しい
#ans = int(((count_inversion(A)*K)%BIG_NUM + (((((K*(K-1))%BIG_NUM)*(0.5))%BIG_NUM)*(cnt_bigger_elem(A)%BIG_NUM)%BIG_NUM)%BIG_NUM))
print(ans2)
#print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
import queue
#import numpy as np
Mod = 1000000007




def main(): #startline-------------------------------------------
    n,k = map(int,input().split())
    a = list(map(int,input().split()))
    ans = 0
    for i in range(n):
        for j in range(n-1,i,-1):
            if a[j] < a[j-1]:
                a[j],a[j-1] = a[j-1],a[j]
                ans += 1
    ans %= Mod
    u = Counter(a)
    ul = list(u.keys())
    ul.sort(reverse = True)
    p = len(ul)   
    ans *= k
    ans %= Mod
    temp = 0
    for i in range(p):
        temp += u[ul[i]]
        ans += (n-temp)*(k*(k-1)//2)
        ans %= Mod
    print(ans)

if __name__ == "__main__":
    main() #endline===============================================





def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n < 2 is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))):
        if prime[i] == 1:
            for j in range(2*i,n):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n):
        if prime[i] == 1:
            res.append(i)
    return res

class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py

    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)