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 = input().split()
N = int(N)
K = int(K)
mod = 1000000007
ans =0

A =[int(i) for i in input().split()]

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


firstcnt =  firstcnt % mod
seccnt = seccnt % mod
K = float(K)
a = float((firstcnt*K)%mod)
c = float(((K-1)*K/2)%mod)
b = float((seccnt * c)%mod)

ans = a + b
ans = ans % mod

print(int(ans))

Submission status: Wrong Answer

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

cnt = 0
ans = 0
KK = ((K * (K-1)) // 2) % MOD
for i in range(N):
    cnt = 0
    for j in range(N):
        if i != j:
            if Alist[i] > Alist[j]:
                cnt += 1
    ans += (cnt * 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) % MOD
ans = ans % MOD

print(int(ans))

Submission status: Wrong Answer

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

d0 = [0]*N
d1 = [0]*N
for i in range(N):
    d0[i] = sum(map(lambda x: x<A[i], A[i+1:N]))
    d1[i] = sum(map(lambda x: x<A[i], A))

ans = sum(d0)*(K)%MOD+sum(d1)*(K-1)*K/2%MOD
print(int(ans%MOD))

Submission status: Wrong Answer

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


li = []

for i in range(n):
    count=0
    for j in range(n):
        if List[i]>List[j]:
            count += 1
    li.append(count)

count2=0

for i in range(1,n):
    for j in range(i+1,n+1):
        if int(List[i-1]) > int(List[j-1]):
            count2 += 1

counted = k*count2 + sum(li)*k*(k-1)/2

ans = counted % 1000000007
print(int(ans))

Submission status: Wrong Answer

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

cnt = 0
ans = 0
KK = (K * (K-1)) // 2
for i in range(N):
    for j in range(N):
        if i != j:
            if Alist[i] > Alist[j]:
                cnt += 1

ans = (cnt * 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) % MOD
ans = ans % MOD

print(int(ans))

Submission status: Wrong Answer

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


li = []

for i in range(n):
    count=0
    for j in range(n):
        if List[i]>List[j]:
            count += 1
    li.append(count)

count2=0

for i in range(1,n):
    for j in range(i+1,n+1):
        if int(List[i-1]) > int(List[j-1]):
            count2 += 1

counted = k*count2 + sum(li)*k*(k-1)/2

ans = counted % 1000000007
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = input().split()
N = int(N)
K = int(K)
mod = 1000000007
ans =0

import sys

A =[int(i) for i in input().split()]

firstcnt = 0
seccnt = 0
for i in range(N-1):
    for j in range(i+1,N):
        if A[i]> A[j]:
            firstcnt += 1
for i in range(N):
    for j in range(N):
        if A[i]> A[j]:
            seccnt += 1
firstcnt =  firstcnt % mod
seccnt = seccnt % mod
a = float((firstcnt*K)%mod)
c = float(((K-1)*K/2)%mod)
b = float((seccnt * c)%mod)

ans = a + b
ans = ans % mod

print(int(ans))

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]
r = [sum([b > c for c in a[i:]]) for i, b in enumerate(a)]
b = (sum(r) + sum(s)*(K-1)/2)*K + sum(r)
print(int(b%(1e9+7)))

Submission status: Wrong Answer

Solution code:
from sys import stdin
import math
import fractions
from collections import deque
from collections import Counter
import itertools
 
INF = 10 ** 10
 
MOD = 10**9 + 7
 
N, K = [int(x) for x in stdin.readline().rstrip().split()]
A = [int(x) for x in stdin.readline().rstrip().split()]
 
 
cnt = 0
 
for i in range(N):
    for j in range(i+1, N):
        if A[i] > A[j]:
            cnt += 1
 
cnt = cnt % MOD
 
if K >= 2:
    cnt = (cnt * K) % MOD
 
cnt2 = 0
 
if K >= 2:
    for i in range(N):
        for j in range(N):
            if A[i] > A[j]:
                cnt2 += 1
    cnt2 = cnt2 % MOD
 
    tmp = (((K) * (K-1))%MOD) // 2
 
    cnt2 = (cnt2 * tmp) % MOD
 
cnt = (cnt + cnt2) % MOD
 
print(cnt)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

n, k = map(int, input().split())
a = list(map(int, input().split()))
a = a + a
print(a)
l = [0 for _ in range(2*n)]
for j in range(1, 2 * n):
	cnt = 0
	for i in range(j):
		if a[i] > a[j]:
			cnt += 1
	l[j] = cnt
base = sum(l[:n])
inc = sum(l[n:])
inc -= base
if k == 1:
	print(base)
else:
	print((base * k + inc * k * (k - 1) // 2) % (int(1e9 + 7)))

Submission status: Wrong Answer

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

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

x=10**9+7

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

ans1=a*k%x
ans2=n*(n-1)/2%x
ans3=k*(k-1)/2%x
ans=(ans1+ans2*ans3)%x

print(int(ans))

Submission status: Wrong Answer

Solution code:
#coding: utf-8
n,k = map(int, input().split())
stra = list(map(int,input().split()))
strb = []
# for i in range(k):
# 	strb = strb + stra
inner = 0
outer = 0
sss = 0
# print(stra)
# print(strb)

for i in range(len(stra)-1):
	for j in range(i+1,len(stra)):
		if stra[i] > stra[j]:
			inner += 1
	# print(inner)
inner = inner * k
# print('inner=' + str(inner))
for i in range(len(stra)):
	for j in range(len(stra)):
		if stra[i] > stra[j]:
			outer += 1
	# print(inner)
outer = outer * k * (k-1) /2
# print('outer=' + str(outer))


# for p in range(k):
# 	sss += p+1
# sss = outer * k * (k-1) /2
# print(sss)
print(round((inner + outer) % (1000000000 + 7)))

Submission status: Wrong Answer

Solution code:

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

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

i=0
temp1=0
while i<n-1:
  j=i+1
  while j<n:
    if r[i]>r[j]:temp1+=1
    j=j+1
  
  i=i+1

temp1=temp1%1000000007
temp2=(k*(k-1)/2)%1000000007
#print(k,temp1,temp2)

print(int((temp1*temp2)%(1000000007)))

Submission status: Wrong Answer

Solution code:
def getlist():
	return list(map(int, input().split()))

N, K = getlist()
A = getlist()

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

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

ans1x = int(((K - 1) * K) / 2) % 1000000007
ans1 = ans1x * s % 1000000007
ans2 = K * x % 1000000007
print((ans1 + ans2) % 1000000007)

Submission status: Wrong Answer

Solution code:
mod=1e9+7
n,k=map(int,input().split())

l=list(map(int,input().split()))
ans=0
for i in range(len(l)):
    for j in range(i):
        if l[j]>l[i]:
            ans+=1
ans*=k

l.sort()

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

Submission status: Wrong Answer

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

x = 0
for l in range(N - 1):
  for r in range(l + 1, N):
    if A[l] > A[r]:
      x += 1
# print(x)
      
AA = A + A
y = 0
for l in range(N - 1):
  for r in range(N):
    if AA[l] > AA[r]:
      y += 1
# print(y)
  
print(int((K * x + K * (K - 1) * y // 2) % (10 ** 9 + 7)))

Submission status: Wrong Answer

Solution code:
N,K = input().split()
N = int(N)
K = int(K)
mod = 1000000007
ans =0

A =[int(i) for i in input().split()]

firstcnt = 0
seccnt = 0
for i in range(N-1):
    for j in range(i+1,N):
        if A[i]> A[j]:
            firstcnt += 1
for i in range(N):
    for j in range(N):
        if A[i]> A[j]:
            seccnt += 1
firstcnt %= mod
seccnt %= mod
a = (firstcnt*K)%mod
c = ((K-1)*K/2)%mod
b = (seccnt * c)%mod

ans = a + b
ans = ans % mod

print(int(ans))

Submission status: Wrong Answer

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

cnt = 0
ans = 0
KK = ((K * (K-1)) % MOD) // 2
for i in range(N):
    for j in range(N):
        if i != j:
            if Alist[i] > Alist[j]:
                cnt += 1

ans = (cnt * 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) % MOD
ans = ans % MOD

print(int(ans))

Submission status: Wrong Answer

Solution code:
def count_inversion(sequence):
    count = 0
    for i in range(1,len(sequence)):
        for j in range(i,len(sequence)):
            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 = 10**9 +7
N, K = map(int,input().split())
A = list(map(int,input().split()))
ans = (count_bigger(A))%MAX

ans *= int((K*(K-1)/2))%MAX
ans += ((K%MAX)*count_inversion(A))%MAX
print(int(ans%MAX))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
mod=1e9+7
n,k=map(int,input().split())

l=list(map(int,input().split()))
ans=0
for i in range(len(l)):
    for j in range(i):
        if l[j]>l[i]:
            ans+=1
ans*=k

l.sort()

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

Submission status: Wrong Answer

Solution code:
def main():
    n, k = (int(i) for i in input().split())
    a = [int(i) for i in input().split()]
    MOD = 10**9 + 7
    ans = 0
    alpha = 0
    cur_min = a[-1]
    cur_l = [a[-1]]
    from bisect import bisect,insort
    for i in range(2,n+1):
        if a[n-i] <= cur_min:
            cur_min = a[n-i]
            cur_l.insert(0,a[n-i])
        else:
            in_idx = bisect(cur_l,a[n-i])
            cur_l.insert(in_idx,a[n-i])
            alpha += in_idx
    ans = ans + (alpha * k)%MOD

    n = len(set(a)) - 1
    ans = ans + (((k*(k-1)) // 2) * ( (n*(n+1)) // 2 )) %MOD
    ans = ans % MOD
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,K = input().split()
N = int(N)
K = int(K)
mod = 1000000007
ans =0

A =[int(i) for i in input().split()]

firstcnt = 0
seccnt = 0
for i in range(N-1):
    for j in range(i+1,N):
        if A[i]> A[j]:
            firstcnt += 1
for i in range(N):
    for j in range(N):
        if A[i]> A[j]:
            seccnt += 1
firstcnt %= mod
seccnt %= mod
a = (firstcnt*K)%mod
b = (seccnt * (K-1)*K/2)%mod

ans = a + b
ans = ans % mod

print(int(ans))

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(N-1):
    count2 += len(list(filter(lambda x: As[i] > x, As[i+1:])))

count2 *= K
# count2 %= 1000000007

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

Submission status: Wrong Answer

Solution code:
R = lambda: map(int, input().split())
n, k = R()
a = list(R())
r = sum(a[i] < a[j] for i in range(n) for j in range(i))
c = len(set(a))
print((r * k + k * (k - 1) * c * (c - 1) // 4) % (10**9 + 7))

Submission status: Wrong Answer

Solution code:
N,K = input().split()
N = int(N)
K = int(K)
mod = 1000000007
ans =0

A =[int(i) for i in input().split()]

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

a = (firstcnt*K)%mod
b = (seccnt * (K-1)*K/2)%mod

ans = a + b
ans = ans % mod

print(int(ans))

Submission status: Wrong Answer

Solution code:
def count_inversion(sequence):
    count = 0
    for i in range(1,len(sequence)):
        for j in range(i,len(sequence)):
            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 = 10**9 +7
N, K = map(int,input().split())
A = list(map(int,input().split()))
ans = (count_bigger(A))%MAX
if K>1:
    ans *= ((K*(K-1)/2)%MAX)

    
ans += ((K%MAX)*count_inversion(A))%MAX
print(int(ans%MAX))

Submission status: Wrong Answer

Solution code:
def getlist():
	return list(map(int, input().split()))

N, K = getlist()
A = getlist()

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

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

ans1 = int(((K - 1) * K) / 2)
ans = ans1 * s + K * x
print(ans % 1000000007)

Submission status: Wrong Answer

Solution code:
# スペース区切りの整数の入力
N, K = map(int, input().split())
an = list(map(int, input().split()))

# bi > bj の数
# 2 4 1  /  2 4 1  / 2 4 1
#
# 2 > 1 => 1（自身のなか）+  (N - 1) * (N - 1)
# 4 > 1 => 1（自身のなか）*  2 * (N-1) = 4
# 1 >

# N = an の長さ
# K = ブロック数

index = 0
count = 0
result_sum = 0
sorted_an = sorted(an)
for a in an:
    if index == 0:
        temp = 0
        temp = len(list(filter(lambda x: a > x, sorted(an[index+1:]))))
        # たしこみ
        result_sum += temp * (1 + K) * K / 2
    else:
        temp = 0
        # それぞれのブロック繰り返す
        count_in_self = len(list(filter(lambda x: a > x, sorted(an[index + 1:]))))
        count_in_total = len(list(filter(lambda x: a > x, sorted_an)))

        #  自身のブロックの中 K 回
        temp += count_in_self * K

        temp += count_in_total * (1 + (K - 1)) * (K - 1) / 2 #- (count_in_total) * (1 + K)  * K / 2


        result_sum += temp
    index += 1


print(int(result_sum) % (10**9 + 7))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
#1行で複数入力
input_list=list(map(int,input().split()))

#数列全体で自分より小さい数がいくつあるか
all_value = {i:0 for i in input_list}
for i in set(input_list):
    for j in input_list:
        if i> j:
            all_value[i]+=1

            
first_value = {i:0 for i in range(len(input_list))}

for i in range(len(input_list)-1):
    for j in range(i,len(input_list)):
        if input_list[i]>input_list[j]:
            first_value[i]+=1


# all_valueが公差
# first_valueがしょこう

count=0
for pos,i in enumerate(input_list):
    count += first_value[pos]*K+all_value[i]*(K-1)//2

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

Submission status: Wrong Answer

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

cnt = 0
ans = 0
KK = K * (K-1) // 2
for i in range(N):
    for j in range(N):
        if i != j:
            if Alist[i] > Alist[j]:
                cnt += 1

ans = (cnt * 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) % MOD
ans = ans % MOD

print(int(ans))

Submission status: Wrong Answer

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

count = 0
for i in range(n):
    for j in range(n-i-1):
        jj = j+1+i
        if la[i] > la[jj]:
            count += 1

count2 = 0
laa = sorted(la)
for i in range(n):
    for j in range(n-i-1):
        jj = j+1+i
      
        if laa[i] < laa[jj]:
            count2 += 1

a = count * k
b = count2 * ((1/2)*(k-1)*k)
result = int(a)+int(b)

print(result%(pow(10,9)+7))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
c = 0
mod = (int)(1e9+7)
num = [0 for _ in range(2001)]
for v in a:
  num[v]+=1
for i in range(1,2001):
  num[i]+=num[i-1]
s = set(a)
for v in s:
  c += num[v - 1]
nn = k-1
if (nn%2==0):
  mul = int((nn//2)*(nn-1))
  mul+=nn
else:
  mul = (nn//2 +1)*nn
now = mul*c
now%=mod
total = 0
for i in range(n-1):
  for j in range(i+1,n):
    if a[i]>a[j]:
      total+=1
total*=k
total%mod
final = total+now
print(final%mod)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

MOD = 10 ** 9 + 7

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

a_single = 0
a_multi = 0

for i, ai in enumerate(a):
    for j, aj in enumerate(a):
        if ai > aj:
            a_multi += 1
            if j > i:
                a_single += 1

ans = 0
ans = (a_single * k % MOD + ans) % MOD
ans = (k * (k - 1) % MOD // 2 % MOD * a_multi % MOD + ans) % MOD

print(ans)

Submission status: Wrong Answer

Solution code:
mod=1e9+7
n,k=map(int,input().split())

l=list(map(int,input().split()))
ans=0
for i in range(len(l)):
    for j in range(i):
        if l[j]>l[i]:
            ans+=1
ans*=k

l.sort()

a=0
f=0
c=1
for i in range(n-1):
    if l[i]==l[i+1]:
        a+=f
        c+=1
    else:
        f+=c
        c=1
        a+=f
a+=f
print((ans+a*k*(k-1)//2)%mod)

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

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

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)
cnt = 0
ans = 0
KK = K * (K-1) // 2
for i in range(N):
    for j in range(N):
        if i != j:
            if Alist[i] > Alist[j]:
                cnt += 1

ans = (cnt * KK)

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= 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(int(ak%1000000007))

Submission status: Wrong Answer

Solution code:
# kleene Inversion

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

ca = 0
cb = 0

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

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

count = ca * k + cb * (k*(k-1))//2
print(count % _mod)

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)

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

Submission status: Wrong Answer

Solution code:
mod=1e9+7
n,k=map(int,input().split())

l=list(map(int,input().split()))
ans=0
for i in range(len(l)):
    for j in range(i):
        if l[j]>l[i]:
            ans+=1
ans*=k

l.sort()

a=0
f=0
c=1
for i in range(n-1):
    if l[i]==l[i+1]:
        a+=f
        c+=1
    else:
        f+=c
        c=1
a+=f
print((ans+a*k*(k-1)//2)%mod)

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()

"""
各数字ごとに数える
"""
K %= mod
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_count %= mod
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_count %= mod
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:
M = 10**9+7
import sys
input = sys.stdin.readline
def main():
    N,K = map(int, input().split())
    A = list(map(int, input().split()))
    A_ = sorted(A, reverse = True)
    ans = 0
    ans_ = 0
    for i in range(0,N-1):
        for j in range(i+1,N):
            if A[i] > A[j]:
                ans += 1
            if A_[i] > A_[j]:
                ans_ += 1

    print(int((((K*(K-1)//2)%M)*ans_+ans*K)%M))

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_count %= mod
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())
box=list(map(int,input().split()))
count=0
for i in range(n-1):
    for j in box[i+1:]:
        if box[i]>j:
            count +=1

box_2=box*2
count_2=0
for i in range(0,n):
    for j in box_2[n:]:
        if box_2[i]>j:
            count_2 +=1
            
point=int(0.5*(k*(k-1)))
print((count_2*point+count*k)%(10**9+7))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10**9 + 7
ans = 0
fflag = 0
bflag = 0

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

ans = (int((fflag+bflag)*k*(k+1)/2) - fflag*k)%mod
print(ans)

Submission status: Wrong Answer

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

for i in range(n-1):
  if a[i] > a[i+1]:
    ans += 1
ans *= k

if a[-1] > a[0]:
  ans += k-1
ans %= 10**9 + 7
  
print(ans)

Submission status: Wrong Answer

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

for i in range(n):
    for j in range(i+1,n):
        if l[i] > l[j]:
            cnt += 1
            
a = len(set(l))
cnt_2 = a * (a-1)/2
            
cnt=(cnt*k)+(k*(k-1)*cnt_2/2)
print(int(cnt%(10**9+7)))

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

MOD = 10 ** 9 + 7

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

a_single = 0
a_multi = 0

for i, ai in enumerate(a):
    for j, aj in enumerate(a):
        if ai > aj:
            a_multi += 1
            if j > i:
                a_single += 1

ans = a_single * k % MOD
ans += k * (k - 1) % MOD // 2 % MOD * a_multi % MOD
ans %= MOD

print(ans)

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= (tentou1*K) + (K*(K-1)/2*stentou)
print(answer%(10**9+7))

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)*upC + ((K-1)*K/2)*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
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)% mod
while ans>=10**9+7:
    ans%=10**9+7
print(ans)

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_count %= mod
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_count %= mod
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:
MOD = 1e+9 + 7
N, K = map(int, input().split())
Alist = list(map(int, input().split()))

Alist_sorted = sorted(Alist)
cnt = 0
ans = 0
KK = (K * (K-1) // 2) % MOD
for i in range(N):
    for j in range(N):
        if i != j:
            if Alist[i] > Alist[j]:
                cnt += 1

ans = (cnt * 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())
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

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

tmp = 0
for i in ppp:
    for j in ppp:
        if i > j:
            tmp += 1

ans = ans_ * k + k_ * tmp
print(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)
cnt = 0
ans = 0
KK = (K * (K-1) / 2) % MOD
for i in range(N):
    for j in range(N):
        if i != j:
            if Alist[i] > Alist[j]:
                cnt += 1

ans = (cnt * 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))