Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**6)
N,k= map(int,input().split())
A= list(map(int,input().split()))
tree = [0] * (N+1)
M=10**9+7
def BIT_update(tree,x):
    while x <= N:
        tree[x] += 1
        x += x&(-x)
def BIT_sum(tree,x):
    s = 0
    while x:
        s += tree[x]
        x -= x&(-x)
    return s

answer = N*(N-1)//2
c={}
for x in A:
    answer -= BIT_sum(tree,x)
    BIT_update(tree,x)
    if x not in c:
        c[x]=1
    else:
        c[x]+=1
c=sorted(c.items(),key=lambda x:x[0])
d,e=0,0
for j in c:
    d+=e
    e+=j[1]
print(((answer*k+(d%M)*(((((k-1)*k)//2))%M))%M))

Fixed submission:
n,k=map(int,input().split())
a,b=list(map(int,input().split())),0
for i,j in enumerate(a):
    for h in a[i:]:
        if j<h:b+=k*(k-1)//2
        elif j>h:b+=k*(k+1)//2
print(b%(10**9+7))

Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**6)
N,k= map(int,input().split())
A= list(map(int,input().split()))
tree = [0] * (N+1)
M=10**9+7
def BIT_update(tree,x):
    while x <= N:
        tree[x] += 1
        x += x&(-x)
def BIT_sum(tree,x):
    s = 0
    while x:
        s += tree[x]
        x -= x&(-x)
    return s

answer = N*(N-1)//2
c={}
for x in A:
    answer -= BIT_sum(tree,x)
    BIT_update(tree,x)
    if x not in c:
        c[x]=1
    else:
        c[x]+=1
c=sorted(c.items(),key=lambda x:x[0])
d,e=0,0
for j in c:
    d+=e
    e+=j[1]
print(((answer*k+d*(((((k-1)*k)//2))%M))%M))

Fixed submission:
n,k=map(int,input().split())
a,b=list(map(int,input().split())),0
for i,j in enumerate(a):
    for h in a[i:]:
        if j<h:b+=k*(k-1)//2
        elif j>h:b+=k*(k+1)//2
print(b%(10**9+7))

Buggy python submission (verdict: Runtime Error):
N,k= map(int,input().split())
A= list(map(int,input().split()))
tree = [0] * (N+1)
M=10**9+7
def BIT_update(tree,x):
    while x <= N:
        tree[x] += 1
        x += x&(-x)
def BIT_sum(tree,x):
    s = 0
    while x:
        s += tree[x]
        x -= x&(-x)
    return s

answer = N*(N-1)//2
c={}
for x in A:
    answer -= BIT_sum(tree,x)
    BIT_update(tree,x)
    if x not in c:
        c[x]=1
    else:
        c[x]+=1
c=sorted(c.items(),key=lambda x:x[0])
d,e=0,0
for j in c:
    d+=e
    e+=j[1]
print(((answer*k+d*(((((k-1)*k)//2))%M))%M))

Fixed submission:
n,k=map(int,input().split())
a,b=list(map(int,input().split())),0
for i,j in enumerate(a):
    for h in a[i:]:
        if j<h:b+=k*(k-1)//2
        elif j>h:b+=k*(k+1)//2
print(b%(10**9+7))

Buggy python submission (verdict: Runtime Error):
N,k= map(int,input().split())
A= list(map(int,input().split()))
tree = [0] * (N+1)
M=10**9+7
def BIT_update(tree,x):
  while x <= N:
    tree[x] += 1
    x += x&(-x)
def BIT_sum(tree,x):
  s = 0
  while x:
    s += tree[x]
    x -= x&(-x)
  return s

answer = N*(N-1)//2
c={}
for x in A:
    answer -= BIT_sum(tree,x)
    BIT_update(tree,x)
    if x not in c:
        c[x]=1
    else:
        c[x]+=1
c=sorted(c.items(),key=lambda x:x[0])
d,e=0,0
for j in c:
    d=(d+(((k*(k-1))//2))*e)%M
    e+=j[1]
print((answer*k+d)%M)

Fixed submission:
n,k=map(int,input().split())
a,b=list(map(int,input().split())),0
for i,j in enumerate(a):
    for h in a[i:]:
        if j<h:b+=k*(k-1)//2
        elif j>h:b+=k*(k+1)//2
print(b%(10**9+7))