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

n = int(input())
al = list(map(int, input().split()))
al_cnt = [0] * (n + 1)

for i in range(1, n + 1):
    al_cnt[i] = al.count(i)
max_cnt = 0

for j in range(1, n + 1):
    if al_cnt[j] >= 2:
        max_cnt += al_cnt[j] * (al_cnt[j] - 1) / 2

for l in range(n):
    if al_cnt[al[l]] >= 3:
        ans = max_cnt - (al_cnt[al[l]] * (al_cnt[al[l]] - 1) / 2) + ((al_cnt[al[l]] - 1) * (al_cnt[al[l]] - 2) / 2)
        print(int(ans) if ans > 0 else 0)
    else:
        ans = max_cnt - (al_cnt[al[l]] * (al_cnt[al[l]] - 1) / 2)
        print(int(ans) if ans > 0 else 0)


Fixed submission:
from collections import Counter

n = int(input())
al = list(map(int, input().split()))
al_cnt = Counter(al)
ans = sum([i * (i - 1) // 2 for i in al_cnt.values()])
for j in al:
    print(ans - al_cnt[j] + 1)

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

def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2, r + 1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p - 1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

n = int(input())
al = list(map(int, input().split()))
al_cnt = [0] * (n + 1)

for i in range(1, n + 1):
    al_cnt[i] = al.count(i)
max_cnt = 0

for j in range(1, n + 1):
    if al_cnt[j] >= 2:
        max_cnt += cmb(al_cnt[j], 2)

for l in range(n):
    if al_cnt[al[l]] >= 3:
        ans = max_cnt - cmb(al_cnt[al[l]], 2) + cmb(al_cnt[al[l]] - 1, 2)
        print(ans if ans > 0 else 0)
    else:
        ans = max_cnt - cmb(al_cnt[al[l]], 2)
        print(ans if ans > 0 else 0)



Fixed submission:
from collections import Counter

n = int(input())
al = list(map(int, input().split()))
al_cnt = Counter(al)
ans = sum([i * (i - 1) // 2 for i in al_cnt.values()])
for j in al:
    print(ans - al_cnt[j] + 1)

Buggy python submission (verdict: Time Limit Exceeded):
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2, r + 1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p - 1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

n = int(input())
al = list(map(int, input().split()))
al_cnt = [0] * (n + 1)

for i in range(1, n + 1):
    al_cnt[i] = al.count(i)
max_cnt = 0

for j in range(1, n + 1):
    if al_cnt[j] >= 2:
        max_cnt += cmb(al_cnt[j], 2)

for l in range(n):
    if al_cnt[al[l]] >= 3:
        ans = max_cnt - cmb(al_cnt[al[l]], 2) + cmb(al_cnt[al[l]] - 1, 2)
        print(ans if ans > 0 else 0)
    else:
        ans = max_cnt - cmb(al_cnt[al[l]], 2)
        print(ans if ans > 0 else 0)


Fixed submission:
from collections import Counter

n = int(input())
al = list(map(int, input().split()))
al_cnt = Counter(al)
ans = sum([i * (i - 1) // 2 for i in al_cnt.values()])
for j in al:
    print(ans - al_cnt[j] + 1)