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 = map(int, input().split())
a = list(map(int, input().split()))
cnt = 0
mod = 10 ** 9 + 7
for i in range(N):
    print(cnt)
    for j in range(N):
        if a[i] > a[j]:
            cnt += 1
ans = cnt * ((K - 1) * K) % mod // 2 % mod
for i in range(N - 1):
    for j in range(i + 1, N):
        if a[i] > a[j]:
            ans += K
print(ans % mod)

Submission status: Wrong Answer

Solution code:
import itertools

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

# print(A * K)

ans = 0
for perm in itertools.permutations(A, 2):
    if perm[0] > perm[1]:
        ans += 1

ans *= K

meets = [0] * N
for i in range(N):
    for j in range(N):
        meets[i] += int(A[i] > A[j])

# print(meets)

# print("sum(meets):{},  K*(K-1)//2:{}".format(sum(meets), K*(K-1)//2))
ans += sum(meets) * K * (K - 1) // 2

print(ans % MOD)

Submission status: Wrong Answer

Solution code:
import itertools

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

ans = 0
for perm in itertools.permutations(A, 2):
    if perm[0] < perm[1]:
        ans += 1

ans *= K

meets = [0] * N
for i in range(N):
    for j in range(N):
        meets[i] += int(A[i] > A[j])

# print(meets)

# print("sum(meets):{},  K*(K-1)//2:{}".format(sum(meets), K*(K-1)//2))
ans += sum(meets) * K * (K - 1) // 2

print(ans % MOD)

Submission status: Wrong Answer

Solution code:
import itertools

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

ans = 0
for perm in itertools.permutations(A, 2):
    if perm[0] < perm[1]:
        ans += 1

ans *= K

meets = [0] * N
for i in range(N):
    for j in range(N):
        meets[i] += int(A[i] > A[j])

print(meets)

# print("sum(meets):{},  K*(K-1)//2:{}".format(sum(meets), K*(K-1)//2))
ans += sum(meets) * K * (K - 1) // 2

print(ans % MOD)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
mod = 10**9 + 7
n, k = map(int, input().split())
a = list(map(int, input().split()))
h = [0 for _ in range(2001)]
ans_1 = []
mi = 2001
for ai in reversed(a):
    h[ai] += 1
    mi = min(mi, ai)
    if mi == ai: continue
    ans_1.append(sum(h[:ai]))
h = [0 for _ in range(2001)]
ans_2 = []
mi = 2001
for ai in a:
    h[ai] += 1
    mi = min(mi, ai)
    if mi == ai: continue
    ans_2.append(sum(h[:ai]))
print(int((sum(ans_1) * (k * (k+1) % mod / 2)) +
          (sum(ans_2) * (k * (k-1) % mod / 2))) % mod)

Submission status: Wrong Answer

Solution code:
mod = 10**9 + 7
n, k = map(int, input().split())
a = list(map(int, input().split()))
h = [0 for _ in range(2001)]
ans_1 = []
mi = 2001
for ai in reversed(a):
    h[ai] += 1
    mi = min(mi, ai)
    if mi == ai: continue
    ans_1.append(sum(h[:ai]))
h = [0 for _ in range(2001)]
ans_2 = []
mi = 2001
for ai in a:
    h[ai] += 1
    mi = min(mi, ai)
    if mi == ai: continue
    ans_2.append(sum(h[:ai]))
print(int(((sum(ans_1) * (k * (k+1) % mod / 2)) +
          (sum(ans_2) * (k * (k-1) % mod / 2))) % mod))

Submission status: Wrong Answer

Solution code:
mod = 10**9 + 7
n, k = map(int, input().split())
a = list(map(int, input().split()))
h = [0 for _ in range(2001)]
ans_1 = []
mi = 2001
for ai in reversed(a):
    h[ai] += 1
    mi = min(mi, ai)
    if mi == ai: continue
    ans_1.append(sum(h[:ai]))
h = [0 for _ in range(2001)]
ans_2 = []
mi = 2001
for ai in a:
    h[ai] += 1
    mi = min(mi, ai)
    if mi == ai: continue
    ans_2.append(sum(h[:ai]))
print(int(((sum(ans_1) * (k * (k+1) / 2 % mod) % mod) +
          (sum(ans_2) * (k * (k-1) / 2 % mod) % mod)) % mod))

Submission status: Wrong Answer

Solution code:
mod = 10**9 + 7
n, k = map(int, input().split())
a = list(map(int, input().split()))
h = [0 for _ in range(2001)]
ans_1 = []
mi = 2001
for ai in reversed(a):
    h[ai] += 1
    mi = min(mi, ai)
    if mi == ai: continue
    ans_1.append(sum(h[:ai]))
h = [0 for _ in range(2001)]
ans_2 = []
mi = 2001
for ai in a:
    h[ai] += 1
    mi = min(mi, ai)
    if mi == ai: continue
    ans_2.append(sum(h[:ai]))
print(int(((sum(ans_1) * (k * (k+1) % mod / 2) % mod)+
          (sum(ans_2) * (k * (k-1) % mod / 2) % mod)) % mod))

Submission status: Wrong Answer

Solution code:
mod = 10**9 + 7
n, k = map(int, input().split())
a = list(map(int, input().split()))
h = [0 for _ in range(2001)]
ans_1 = []
mi = 2001
for ai in reversed(a):
    h[ai] += 1
    mi = min(mi, ai)
    if mi == ai: continue
    ans_1.append(sum(h[:ai]))
h = [0 for _ in range(2001)]
ans_2 = []
mi = 2001
for ai in a:
    h[ai] += 1
    mi = min(mi, ai)
    if mi == ai: continue
    ans_2.append(sum(h[:ai]))
print(int(((sum(ans_1) * (k * (k+1) % mod / 2)) % mod +
          (sum(ans_2) * (k * (k-1) % mod / 2))) % mod) % mod)

Submission status: Wrong Answer

Solution code:
mod = 10**9 + 7
n, k = map(int, input().split())
a = list(map(int, input().split()))
h = [0 for _ in range(2001)]
ans_1 = []
mi = 2001
for ai in reversed(a):
    h[ai] += 1
    mi = min(mi, ai)
    if mi == ai: continue
    ans_1.append(sum(h[:ai]))
h = [0 for _ in range(2001)]
ans_2 = []
mi = 2001
for ai in a:
    h[ai] += 1
    mi = min(mi, ai)
    if mi == ai: continue
    ans_2.append(sum(h[:ai]))
print(int(((sum(ans_1) * (k * (k+1) % mod / 2)) % mod +
          (sum(ans_2) * (k * (k-1) % mod / 2))) % mod))

Submission status: Wrong Answer

Solution code:
mod = 10**9 + 7
n, k = map(int, input().split())
a = list(map(int, input().split()))
h = [0 for _ in range(2001)]
ans_1 = []
mi = 2001
for ai in reversed(a):
    h[ai] += 1
    mi = min(mi, ai)
    if mi == ai: continue
    ans_1.append(sum(h[:ai]))
h = [0 for _ in range(2001)]
ans_2 = []
mi = 2001
for ai in a:
    h[ai] += 1
    mi = min(mi, ai)
    if mi == ai: continue
    ans_2.append(sum(h[:ai]))
print(int((sum(ans_1) % mod) * (k * (k+1) % mod) / 2 +
          (sum(ans_2) % mod) * (k * (k-1) % mod) / 2 % mod))

Submission status: Wrong Answer

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


# Aの中で，各位置の文字が何個逆転数を生むか
# それが N * (N + 1) / 2個存在する
counter1 = [0] * len(A)
counter2 = [0] * len(A)
for i in range(N):
    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

# print(A)
# print(counter1)
# print(counter2)
fact = (K * (K - 1) % MOD) // 2

ans = 0
for i in range(N):
    c1 = counter1[i]
    c2 = counter2[i]
    ans = (ans + (c1 * K ) + (c2 * fact)) % MOD 

print(ans)

Submission status: Wrong Answer

Solution code:
import math
n,k = map(int,input().split())
li = list(map(int,input().split()))
li.reverse()
count = 0
#print(li)
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
#print(li)
for i,l in enumerate(li):
    if i != len(li)-1: 
        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) """
#print(al)
all_count = (k+1)*(k)//2
#print(all_count-k,count,next_count)
al = (all_count-k)*(next_count)+k*count
print(al%(10**9+7))
#print(count,next_count)

Submission status: Wrong Answer

Solution code:
import math
n,k = map(int,input().split())
li = list(map(int,input().split()))
li.reverse()
count = 0
#print(li)
for i,l in enumerate(li):
    if i != len(li)-1: 
        a = len(list(filter(lambda x:x > l, li[i+1:])))
        count += a
print(count*(k+1)*(k)//2%(10**9+7))

Submission status: Wrong Answer

Solution code:
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)%mod
print(ans)

Submission status: Wrong Answer

Solution code:
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

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

Submission status: Wrong Answer

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

# Kを無視して店頭数がいくつなのかを数える
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)

Submission status: Wrong Answer

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

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

print((v*(K*(K+1)//2))%mod)

Submission status: Wrong Answer

Solution code:
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
#print(l2,A)
for i in range(N):
    for j in range(i+1,N):
        #print(i,j)
        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
print(tentou1,tentou2)
n=(tentou2-tentou1)-tentou1
#for i in range(10):
#    print((i*(3*tentou1-tentou2)+n*(i*(i+1))//2)%p)

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

Submission status: Wrong Answer

Solution code:
MOD = 1**9+7

def resolve():
  ans = 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(n):
      if a[i] > a[j]:
        c[i] += 1
        if i < j:    # A 内に存在する転倒数
          ans += k
          ans %= MOD

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

  for v in c:
      ans += (v * s) % MOD
      ans %= MOD
  print(int(ans % MOD))
  return  

if __name__ == "__main__":
  resolve()

Submission status: Wrong Answer

Solution code:
MOD = 1e9+7

def resolve():
  ans = 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(n):
      if a[i] > a[j]:
        c[i] += 1
        if i < j:    # A 内に存在する転倒数
          ans += k
          ans %= MOD

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

  for v in c:
      ans += (v * s) % MOD
      ans %= MOD
  print(int(ans % MOD))
  return  

if __name__ == "__main__":
  resolve()

Submission status: Wrong Answer

Solution code:
MOD = 1e9+7

def resolve():
  ans = 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(n):
      if a[i] > a[j]:
        c[i] += 1
        if i < j:
          ans += k
          ans %= MOD

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

  for v in c:
      ans += v * s
      ans %= MOD
  print(int(ans % MOD))
  return  

if __name__ == "__main__":
  resolve()

Submission status: Wrong Answer

Solution code:
MOD = 1e9+7

def resolve():
  ans = 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(n):
      if a[i] > a[j]:
        c[i] += 1
        if i < j:
          ans += k

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

  for v in c:
      ans += v * s
      ans %= MOD
  print(int(ans % MOD))
  return  

if __name__ == "__main__":
  resolve()

Submission status: Wrong Answer

Solution code:
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:
            #ans+=1
            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
x=A[0]-A[-1]
if x<0:
    #ans+=1
    C[A[0]]+=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)

Submission status: Wrong Answer

Solution code:
MOD = 1e9+7

def resolve():
  n, k = map(int, input().split())
  a = list(map(int, input().split()))
  b = [0] * n  # 自分より後に自分より小さい値が何個あるか
  c = [0] * n  # 自分より小さい値が全体に何個あるか
  for i in range(n):
    for j in range(n):
      if a[i] > a[j]:
        c[i] += 1
        if (i < j):
          b[i] += 1
  ans = 0
  for v in b:
    ans += (v*k)
    ans %= MOD

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

  for v in c:
      ans += v * s
      ans %= MOD
  print(int(ans))
  return  


if __name__ == "__main__":
  resolve()

Submission status: Wrong Answer

Solution code:
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
            q1 %= mod

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

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

Submission status: Wrong Answer

Solution code:
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):
    multi_one = int(((k-1)*k%mod)/2)
    multi_two = int((k*(k+1)%mod)/2)
    plus = multi_one*Before[i]%mod + multi_two*After[i]%mod
    ans = (ans + plus) % mod

print(ans)

Submission status: Wrong Answer

Solution code:
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):
    now = ((k-1)*k%mod)/2*Before[i] + (k*(k+1)%mod)*After[i]/2
    ans = int((ans + now) % mod)

print(ans)

Submission status: Wrong Answer

Solution code:
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):
    ans += int(Before[i]*(k-1)*k/2 + After[i]*k*(k+1)/2)
    ans %= mod

print(ans)

Submission status: Wrong Answer

Solution code:
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,n):
        if now > A[l]:
            after += 1
    Before.append(before)
    After.append(after)

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

print(ans)

Submission status: Wrong Answer

Solution code:
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+1,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()

Submission status: Wrong Answer

Solution code:
mod=(10**9)+7
def main():
    n,k=map(int,input().split())
    arr=[int(x) for x in input().split()]
    t1=t2=0
    for i in range(1,len(arr)):
        for j in range(i):
            if arr[j] > arr[i]:
                t1+=1
    arr.sort()
    for i in range(1,len(arr)):
        for j in range(i):
            if arr[j] <arr[i]:
                t2+=1
    ans1=(t1*k)%mod
    base=(k*(k-1)/2)%mod
    ans2=(t2*base)%mod
    ans=(ans1+ans2)%mod
    print(int(ans))
main()

Submission status: Wrong Answer

Solution code:
mod=(10**9)+7
def main():
    n,k=map(int,input().split())
    arr=[int(x) for x in input().split()]
    t1=t2=0
    for i in range(len(arr)):
        for j in range(len(arr)):
            if i <j:
                if arr[i] > arr[j]:
                    t1+=1
    narr=arr*2
    for i in range(len(arr)):
        for j in range(len(arr),len(narr)):
            if narr[i] > narr[j]:
                t2+=1
                
    ans=(t1*k)%mod+(t2*k*(k-1)/2)%mod
    print(ans%mod)
main()

Submission status: Wrong Answer

Solution code:
mod=(10**9)+7
def main():
    n,k=map(int,input().split())
    arr=[int(x) for x in input().split()]
    t1=t2=0
    for i in range(len(arr)):
        for j in range(len(arr)):
            if i <j:
                if arr[i] > arr[j]:
                    t1+=1
    narr=arr*2
    for i in range(len(arr)):
        for j in range(len(arr),len(narr)):
            if narr[i] > narr[j]:
                t2+=1
                
    ans=(t1*k)+(t2*k*(k-1)/2)
    print(ans%mod)
main()

Submission status: Wrong Answer

Solution code:
n,k= [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
res = []
MOD = 10**9+7
for i in range(n):
  b=0
  if i==0:
    for j in range(1,n):
      if a[0]<a[j]:
        b+=1
    res.append(((k*(k-1))//2*b)%MOD)
    #res.append(b)
    continue
  for j in range(i):
    if a[i]<a[j]:
      b+=1
  res.append(((k*(k+1))//2*b)%MOD)
  #res.append(b)
print(sum(res)%MOD)
#print(res)

Submission status: Wrong Answer

Solution code:
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))

Submission status: Wrong Answer

Solution code:
import math
n,k = map(int,input().split())
a = list(map(int,input().split()))
a2 = a+a
count1 = 0
for i in range(len(a)):
    for j in range(i,len(a)):
        if a[i] > a[j]:
            count1 +=1
count2 = 0
for i in range(len(a)):
    for j in range(len(a),len(a)*2):
        if a2[i] > a2[j]:
            count2 +=1
s = 1/2*k*(k-1)
print(math.floor(count1 * k + count2 * s) % (10 ** 9 + 7))

Submission status: Wrong Answer

Solution code:
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)

Submission status: Wrong Answer

Solution code:
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)

Submission status: Wrong Answer

Solution code:
import math
n,k = map(int,input().split())
a = list(map(int,input().split()))
count = 0
for i in range(len(a)):
    for j in range(i,len(a)):
        if a[i] > a[j]:
            count +=1
s = 1/2*k*(k+1)
print(math.floor(s * count % (10**9+7)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
*a,=map(int,input().split())
p=10**9+7
cnt=0
ans1=0
for i in range(n):
    x=[j for j in a[:i] if j>a[i]]
    ans1+=len(x)
un=(n*(n-1))//2
k%=p
ans=ans1*k+(k*(k-1)//2)*un
print(ans%p)

Submission status: Wrong Answer

Solution code:
# まずふつうにAに含まれる転倒数をかぞえる これがK回ある
# 次にA内でAi<Ajとなる数をかぞえる これがK-1回ある
n, k = map(int, input().split())
a = list(map(int, input().split()))
x, y = 0, 0
for i in range(n - 1):
    for j in range(i + 1, n):
        if a[i] > a[j]:
            x += 1
for i in range(n - 1, 0, -1):
    for j in range(i - 1, -1, -1):
        if a[j] < a[i]:
            print("yes i, j={}, {}".format(i, j))
            y += 1
    print()
print(x, y)
MOD = 10 ** 9 + 7

print((x * k) % MOD + (y * (k - 1)) % MOD)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = [int(i) for i in input().split()]
ans = 0
m = max(a)+2
cnt1 = [0]*m
cnt2 = [0]*m
mod = 10**9+7

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

for i in range(1,len(cnt1)):
    cnt1[i] += cnt1[i-1]

for i in range(n):
    ans = (ans + cnt1[a[i]]*(k*(k-1))//2%mod + cnt2[a[i]]*k%mod)%mod

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from itertools import combinations, accumulate
n,m = map(int,input().split())
a = list(map(int,input().split()))
mod = 10**9+7
ans = 0
ls = [0]*2001
for i in a:
  ls[i] += 1
accls = list(accumulate(ls))
for i,j in combinations(a,2):
  if i>j:
    ans += 1
ans = ans*m%mod
for i in range(1,2001):
  if ls[i]>=1:
    ans = (ans + accls[i-1]*m*(m-1)//2)%mod
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import combinations, accumulate
n,m = map(int,input().split())
a = list(map(int,input().split()))
mod = 10**9+7
ans = 0
ls = [0]*2001
for i in a:
  ls[i] += 1
accls = list(accumulate(ls))
for i,j in combinations(a,2):
  if i>j:
    ans += 1
tmp = ans
for i in range(1,2001):
  if ls[i]>=1:
    ans = ans + accls[i-1]
ans = (tmp+ans*(m-1))%mod
print(ans)

Submission status: Wrong Answer

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


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


def inversion(a):
    "転倒数を求める"
    d = {av: i+1 for i, av in enumerate(sorted(a))}
    b = Bit(len(a))
    ans = 0
    for i, av in enumerate(a):
        ans += i-b.sum(d[av])
        b.add(d[av], 1)
    return ans


MOD = 10**9+7
L = len(set(A))

ans = (inversion(A) * K) % MOD + (K*(K-1)//2) * (L*(L-1)//2) % MOD
ans %= MOD
print(ans)

Submission status: Wrong Answer

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


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


def inversion(a):
    "転倒数を求める"
    d = {av: i+1 for i, av in enumerate(sorted(a))}
    b = Bit(len(a))
    ans = 0
    for i, av in enumerate(a):
        ans += i-b.sum(d[av])
        b.add(d[av], 1)
    return ans


MOD = 10**9+7
ans = inversion(A) % MOD
ans %= MOD
ans *= K
ans %= MOD

L = len(set(A))

rest = K*(K-1)//2
rest %= MOD
rest *= L*(L-1)//2
rest %= MOD
ans += rest
ans %= MOD
print(ans)

Submission status: Wrong Answer

Solution code:
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    # divisors.sort()
    return divisors
def examA():
    M, D = LI()
    c = D//10
    ans = 0
    for i in range(2,M+1):
        d = make_divisors(i)
#        print(d)
        for j in range(len(d)//2):
            cur0 = d[j*2+1]; cur10 = d[j*2]
            if cur10==1:
                continue
            if cur0<=9:
                if cur10<c:
                    ans +=1
#                    print(cur10,cur0)
                elif cur10==c and cur0+cur10*10<=D:
                    ans +=1
#                    print(cur10,cur0)
        for j in range(len(d) // 2):
            cur0 = d[j * 2];cur10 = d[j * 2+1]
            if cur0 == 1:
                continue
            if cur10 <= 9:
                if cur10 < c:
                    ans += 1
                elif cur10 == c and cur0 + cur10 * 10 <= D:
                    ans += 1
        if len(d)%2==1:
            cur0 = d[-1]; cur10 = d[-1]
            if cur10==1:
                continue
            if cur0<=9:
                if cur10<c:
                    ans +=1
#                    print(cur10,cur0)
                elif cur10==c and cur0+cur10*10<=D:
                    ans +=1
#                    print(cur10,cur0)
    print(ans)
    return

class Bit():
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
        return
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
        return
def examB():
    N, K = LI()
    A = LI()
    d = defaultdict(int)
    bit = Bit(max(A))
    cur = 0
    for i, a in enumerate(A):
        bit.add(a, 1)
        cur += i + 1 - bit.sum(a)
    As = 0
    for i in A:
        d[i] +=1
    A.sort()
    for i in range(N):
        As += max(0,i-d[A[i]]+1)
#    print(cur,As)
    ans = (As*(K**2) - (As -2*cur)*K)//2
    ans =int(ans)%mod
    print(ans)
    return

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

if __name__ == '__main__':
    examB()

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter
from itertools import accumulate, combinations

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

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

answer = 0

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

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

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

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter
from itertools import accumulate, combinations

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

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

answer = 0

if K > 1:
    b = Counter(A)
    n = list(accumulate(b[i] for i in sorted(b.keys())))
    n = sum(n[:-1])

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

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
import numpy as np

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

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

ans %= MOD
print(int(ans))

Submission status: Wrong Answer

Solution code:
import numpy as np

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

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

    count += 1

ans %= MOD
print(int(ans))