Buggy python submission (verdict: Time Limit Exceeded):
def bubble_sort(a):
    cnt = 0
    for i in range(len(a)):
        for j in range(len(a) - 1, i, -1):
            if a[j] < a[j-1]:
                a[j], a[j - 1] = a[j - 1], a[j]
                cnt += 1
    return cnt

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

Fixed submission:
SENTINEL = 10 ** 9 +1
cnt = 0
def merge(A, left, mid, right):
    global cnt

    L = A[left:mid] + [SENTINEL]
    R = A[mid:right] + [SENTINEL]
    
    i = j = 0
    for k in range(left, right):
        if L[i] <= R[j]:
            A[k] = L[i]
            i += 1
        else:
            A[k] = R[j]
            j += 1
            cnt += mid - left - i  # ☆

def merge_sort(A, left, right):
    if left + 1 < right:
        mid = (left + right) // 2
        merge_sort(A, left, mid)
        merge_sort(A, mid, right)
        merge(A, left, mid, right)

n = input()
A = list(map(int, input().split()))
merge_sort(A,0,len(A))
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
def bubble_sort_cnt(a):
    cnt = 0
    for i in range(len(a) - 1):
        for j in range(i + 1, len(a)):
            if a[i] > a[j]:
                cnt += 1
    return cnt

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

Fixed submission:
SENTINEL = 10 ** 9 +1
cnt = 0
def merge(A, left, mid, right):
    global cnt

    L = A[left:mid] + [SENTINEL]
    R = A[mid:right] + [SENTINEL]
    
    i = j = 0
    for k in range(left, right):
        if L[i] <= R[j]:
            A[k] = L[i]
            i += 1
        else:
            A[k] = R[j]
            j += 1
            cnt += mid - left - i  # ☆

def merge_sort(A, left, right):
    if left + 1 < right:
        mid = (left + right) // 2
        merge_sort(A, left, mid)
        merge_sort(A, mid, right)
        merge(A, left, mid, right)

n = input()
A = list(map(int, input().split()))
merge_sort(A,0,len(A))
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect, sys
n = sys.stdin.readline()
cnt = 0
a = []
for ai in map(int, sys.stdin.readline().split()):
    pos = bisect.bisect_left(a, ai)
    cnt += len(a) - pos
    a.insert(pos, ai)
 
print(cnt)

Fixed submission:
SENTINEL = 10 ** 9 +1
cnt = 0
def merge(A, left, mid, right):
    global cnt

    L = A[left:mid] + [SENTINEL]
    R = A[mid:right] + [SENTINEL]
    
    i = j = 0
    for k in range(left, right):
        if L[i] <= R[j]:
            A[k] = L[i]
            i += 1
        else:
            A[k] = R[j]
            j += 1
            cnt += mid - left - i  # ☆

def merge_sort(A, left, right):
    if left + 1 < right:
        mid = (left + right) // 2
        merge_sort(A, left, mid)
        merge_sort(A, mid, right)
        merge(A, left, mid, right)

n = input()
A = list(map(int, input().split()))
merge_sort(A,0,len(A))
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect, sys
n = sys.stdin.readline()


cnt = 0
a = []
for ai in map(int, sys.stdin.readline().split()):
    pos = bisect.bisect_left(a, ai)
    cnt -= pos
    a.insert(pos, ai)

print(((len(a) - 1) * len(a)) // 2 + cnt)

Fixed submission:
SENTINEL = 10 ** 9 +1
cnt = 0
def merge(A, left, mid, right):
    global cnt

    L = A[left:mid] + [SENTINEL]
    R = A[mid:right] + [SENTINEL]
    
    i = j = 0
    for k in range(left, right):
        if L[i] <= R[j]:
            A[k] = L[i]
            i += 1
        else:
            A[k] = R[j]
            j += 1
            cnt += mid - left - i  # ☆

def merge_sort(A, left, right):
    if left + 1 < right:
        mid = (left + right) // 2
        merge_sort(A, left, mid)
        merge_sort(A, mid, right)
        merge(A, left, mid, right)

n = input()
A = list(map(int, input().split()))
merge_sort(A,0,len(A))
print(cnt)