Buggy python submission (verdict: Runtime Error):
from collections import Counter

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())
aaa = list(map(int, input().split()))
MOD = 10**9 + 7
bit = Bit(2001)
single = 0
for a in reversed(aaa):
  single += bit.sum(a)
  bit.add(a + 1, 1)
ans = single * k % MOD
coef = k * (k - 1) // 2 % MOD
cnt = Counter(aaa)
before = 0
for a,c in sorted(cnt.items()):
  ans = (ans + c * before * conf) % MOD
  before += c
print(ans)





Fixed submission:
N,K = map(int,input().split())
A = list(map(int,input().split()*2))
base1 = 0
base2 = 0
for i in range(N):
  count1=0
  count2=0
  for j in range(i+1,i+N+1):
    if A[i] > A[j]:
      if j < N:
        count1+=1
      else:
        count2+=1
  base1+=count1
  base2+=count2

ans = (base1*K*(K+1) + base2*K*(K-1))//2
div = 10**9 + 7
print(ans%div)

Buggy python submission (verdict: Runtime Error):
from collections import Counter

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 & -1
    return s
  
  def add(self, i, x):
    while i <= self.size:
      self.tree[i] += x
      i += i & -i

n,k = map(int, input().split())
aaa = list(map(int, input().split()))
MOD = 10**9 + 7
bit = Bit(2001)
single = 0
for a in reversed(aaa):
  single += bit.sum(a)
  bit.add(a + 1, 1)
ans = single * k % MOD
coef = k * (k - 1) // 2 % MOD
cnt = Counter(aaa)
before = 0
for a,c in sorted(cnt.items()):
  ans = (ans + c * before * conf) % MOD
  before += c
print(ans)





Fixed submission:
N,K = map(int,input().split())
A = list(map(int,input().split()*2))
base1 = 0
base2 = 0
for i in range(N):
  count1=0
  count2=0
  for j in range(i+1,i+N+1):
    if A[i] > A[j]:
      if j < N:
        count1+=1
      else:
        count2+=1
  base1+=count1
  base2+=count2

ans = (base1*K*(K+1) + base2*K*(K-1))//2
div = 10**9 + 7
print(ans%div)

Buggy python submission (verdict: Runtime Error):
from collections import Counter

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 & -1
    return s
  
  def add(self, i, x):
    while i <= self.size:
      i += i & -i

n,k = map(int, input().split())
aaa = list(map(int, input().split()))
MOD = 10**9 + 7
bit = Bit(2001)
single = 0
for a in reversed(aaa):
  single += bit.sum(a)
  bit.add(a + 1, 1)
ans = single * k % MOD
coef = k * (k - 1) // 2 % MOD
cnt = Counter(aaa)
before = 0
for a,c in sorted(cnt.items()):
  ans = (ans + c * before * conf) % MOD
  before += c
print(ans)





Fixed submission:
N,K = map(int,input().split())
A = list(map(int,input().split()*2))
base1 = 0
base2 = 0
for i in range(N):
  count1=0
  count2=0
  for j in range(i+1,i+N+1):
    if A[i] > A[j]:
      if j < N:
        count1+=1
      else:
        count2+=1
  base1+=count1
  base2+=count2

ans = (base1*K*(K+1) + base2*K*(K-1))//2
div = 10**9 + 7
print(ans%div)