Buggy python submission (verdict: Runtime Error):
def num_of_inversions(A, n, cnt = 0):
    """Return the number of inversions in the sequence A.

    A is a list of numbers.
    n is the length of A.
    cnt is a accumulator.
    """

    if n == 1:
        return cnt
    t_cnt = 0
    t_lt = A[-1]
    for i in reversed(A[:n - 1]):
        if i > t_lt:
            t_cnt += 1
    return num_of_inversions(A[:n - 1], n - 1, cnt + t_cnt)


import sys

n = int(sys.stdin.readline())

A = list(map(int, sys.stdin.readline().split()))

ans = num_of_inversions(A, n)

print(ans)

Fixed submission:
sentinel = 1000000001

def merge(A, left, mid, right):
    L = A[left:mid]
    n1 = len(L)
    L.append(sentinel)
    R = A[mid:right]
    R.append(sentinel)
    i = 0
    j = 0
    cnt = 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 += n1 - i
    return cnt

def mergeSort(A, left, right):
    if left + 1 < right:
        mid = (left + right) // 2
        cnt1 = mergeSort(A, left, mid)
        cnt2 = mergeSort(A, mid, right)
        cnt3 = merge(A, left, mid, right)
        return cnt1 + cnt2 + cnt3
    else:
        return 0


n = int(input())
S = list(map(int, input().split()))

cnt = 0

n_ivs =  mergeSort(S, 0, n)

print(n_ivs)

Buggy python submission (verdict: Time Limit Exceeded):
def num_of_inversions(A, n):
    """Print the number of inversions in the sequence A.

    A is a list of numbers.
    n is the length of A.
    """

    cnt = 0
    for i in range(n - 1):
        flag = 0
        for j in range(n - 1, i, -1):
            if A[j] < A[j - 1]:
                A[j], A[j - 1] = A[j - 1], A[j]
                cnt += 1
                flag = 1
        if flag == 0:
            break
    print(cnt)


import sys

n = int(sys.stdin.readline())

A = list(map(int, sys.stdin.readline().split()))

num_of_inversions(A, n)

Fixed submission:
sentinel = 1000000001

def merge(A, left, mid, right):
    L = A[left:mid]
    n1 = len(L)
    L.append(sentinel)
    R = A[mid:right]
    R.append(sentinel)
    i = 0
    j = 0
    cnt = 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 += n1 - i
    return cnt

def mergeSort(A, left, right):
    if left + 1 < right:
        mid = (left + right) // 2
        cnt1 = mergeSort(A, left, mid)
        cnt2 = mergeSort(A, mid, right)
        cnt3 = merge(A, left, mid, right)
        return cnt1 + cnt2 + cnt3
    else:
        return 0


n = int(input())
S = list(map(int, input().split()))

cnt = 0

n_ivs =  mergeSort(S, 0, n)

print(n_ivs)

Buggy python submission (verdict: Time Limit Exceeded):
def num_of_inversions(A, left, right):
    """Return the number of inversions in the sequence A.
    Using Recursion/Divide and Conquer algorithm.

    A is a list of integers.
    left is the starting index, initially 0.
    right is the value added 1 to the last index, initially the length of A.
    """

    global cnt
    if left + 1 < right:
        mid = (left + right) // 2
        L = A[left:mid]
        R = A[mid:right]
        for r in R:
            for l in L:
                if r < l:
                    cnt += 1
        num_of_inversions(A, left, mid)
        num_of_inversions(A, mid, right)


import sys

cnt = 0

n = int(sys.stdin.readline())

A = list(map(int, sys.stdin.readline().split()))

num_of_inversions(A, 0, n)

print(cnt)

Fixed submission:
sentinel = 1000000001

def merge(A, left, mid, right):
    L = A[left:mid]
    n1 = len(L)
    L.append(sentinel)
    R = A[mid:right]
    R.append(sentinel)
    i = 0
    j = 0
    cnt = 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 += n1 - i
    return cnt

def mergeSort(A, left, right):
    if left + 1 < right:
        mid = (left + right) // 2
        cnt1 = mergeSort(A, left, mid)
        cnt2 = mergeSort(A, mid, right)
        cnt3 = merge(A, left, mid, right)
        return cnt1 + cnt2 + cnt3
    else:
        return 0


n = int(input())
S = list(map(int, input().split()))

cnt = 0

n_ivs =  mergeSort(S, 0, n)

print(n_ivs)