Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))
import collections
from operator import mul
from functools import reduce

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under

def main():
    N = int(input())
    A = input_nums()
    C = collections.Counter(A)

    X = {}
    Y = {}

    for k, v in C.items():
        if v >= 2:
            X[k] = cmb(v, 2)
        else:
            X[k] = 0
        if v >= 3:
            Y[k] = cmb(v-1, 2)
        else:
            Y[k] = 0

    ANS = {}
    for k in X.keys():
        val = 0
        for k2 in X.keys():
            if k == k2:
                val += Y[k2]
            else:
                val += X[k2]
        ANS[k] = val
    for a in A:
        print(ANS[a])

if __name__ == '__main__':
    main()


Fixed submission:
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))
import collections
from operator import mul
from functools import reduce

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under

def main():
    N = int(input())
    A = input_nums()
    C = collections.Counter(A)

    X = {}
    Y = {}
    maxv = 0
    for k, v in C.items():
        if v >= 2:
            X[k] = cmb(v, 2)
            maxv += X[k]
        else:
            X[k] = 0
        if v >= 3:
            Y[k] = cmb(v-1, 2)
        else:
            Y[k] = 0

    ANS = {}
    for k in Y.keys():
        ANS[k] = maxv - (X[k] - Y[k])

    for a in A:
        print(ANS[a])

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))
import collections
from operator import mul
from functools import reduce

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under

def main():
    N = int(input())
    A = input_nums()
    C = collections.Counter(A)

    X = {}
    Y = {}

    for k, v in C.items():
        if v >= 2:
            X[k] = cmb(v, 2)
        else:
            X[k] = 0
        if v >= 3:
            Y[k] = cmb(v-1, 2)
        else:
            Y[k] = 0

    for i in range(N):
        val = 0
        for k in X.keys():
            if A[i] == k:
                val += Y[k]
            else:
                val += X[k]
        print(val)



if __name__ == '__main__':
    main()


Fixed submission:
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))
import collections
from operator import mul
from functools import reduce

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under

def main():
    N = int(input())
    A = input_nums()
    C = collections.Counter(A)

    X = {}
    Y = {}
    maxv = 0
    for k, v in C.items():
        if v >= 2:
            X[k] = cmb(v, 2)
            maxv += X[k]
        else:
            X[k] = 0
        if v >= 3:
            Y[k] = cmb(v-1, 2)
        else:
            Y[k] = 0

    ANS = {}
    for k in Y.keys():
        ANS[k] = maxv - (X[k] - Y[k])

    for a in A:
        print(ANS[a])

if __name__ == '__main__':
    main()