Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict


def num_combinations(_n):
    return _n * (_n - 1) // 2


if __name__ == '__main__':
    n = int(input())
    xs = [int(s) for s in input().split()]
    freq = defaultdict(int)

    for n in xs:
        freq[n] += 1

    res = 0

    for k in range(len(xs)):
        res = 0
        for num in freq.keys():
            res += num_combinations(freq[num] - (1 if xs[k] == num else 0))
        print(res)


Fixed submission:
from collections import defaultdict


def num_combinations(_n):
    return _n * (_n - 1) // 2


if __name__ == '__main__':
    n = int(input())
    xs = [int(s) for s in input().split()]
    freq = defaultdict(int)

    for n in xs:
        freq[n] += 1

    total = sum([num_combinations(n) for n in freq.values()])
    for x in xs:
        print(total - (freq[x] - 1))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict

mem = {}


def binomialCoef(n, k):
    if n < k:
        return 0
    if n in mem.keys():
        return mem[n]
    C = [[0 for x in range(k + 1)] for x in range(n + 1)]

    # Calculate value of Binomial Coefficient in bottom up manner
    for i in range(n + 1):
        for j in range(min(i, k) + 1):
            # Base Cases
            if j == 0 or j == i:
                C[i][j] = 1

            # Calculate value using previously stored values
            else:
                C[i][j] = C[i - 1][j - 1] + C[i - 1][j]

    mem[n] = C[n][k]
    return mem[n]


if __name__ == '__main__':
    n = int(input())
    xs = [int(s) for s in input().split()]
    freq = defaultdict(int)
    xs_res = defaultdict(int)

    for n in xs:
        freq[n] += 1
        if freq[n] > 2:
            xs_res[n] *= 2
        if freq[n] > 1:
            xs_res[n] += 1

    res = 0

    for k in range(len(xs)):
        res = 0
        for num in freq.keys():
            res += binomialCoef(freq[num] if num != xs[k] else freq[num] - 1, 2)
        print(res)


Fixed submission:
from collections import defaultdict


def num_combinations(_n):
    return _n * (_n - 1) // 2


if __name__ == '__main__':
    n = int(input())
    xs = [int(s) for s in input().split()]
    freq = defaultdict(int)

    for n in xs:
        freq[n] += 1

    total = sum([num_combinations(n) for n in freq.values()])
    for x in xs:
        print(total - (freq[x] - 1))

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

mem = {}


def binomialCoeff(n, k):
    if n < k:
        return 0
    if k == 0 or k == n:
        return 1
    if n in mem.keys():
        return mem[n]

    # Recursive Call
    mem[n] = binomialCoeff(n - 1, k - 1) + binomialCoeff(n - 1, k)
    return mem[n]


if __name__ == '__main__':
    n = int(input())
    xs = [int(s) for s in input().split()]
    freq = defaultdict(int)
    xs_res = defaultdict(int)

    for n in xs:
        freq[n] += 1
        if freq[n] > 2:
            xs_res[n] *= 2
        if freq[n] > 1:
            xs_res[n] += 1

    res = 0

    for k in range(len(xs)):
        res = 0
        for num in freq.keys():
            res += binomialCoeff(freq[num] if num != xs[k] else freq[num] - 1, 2)
        print(res)


Fixed submission:
from collections import defaultdict


def num_combinations(_n):
    return _n * (_n - 1) // 2


if __name__ == '__main__':
    n = int(input())
    xs = [int(s) for s in input().split()]
    freq = defaultdict(int)

    for n in xs:
        freq[n] += 1

    total = sum([num_combinations(n) for n in freq.values()])
    for x in xs:
        print(total - (freq[x] - 1))