Buggy python submission (verdict: Time Limit Exceeded):
import math

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

n=int(input())
a=list(map(int,input().split()))
cnt={}
comb={}
ans=0
for i in a:
    if i in cnt:
        cnt[i]+=1
    else:
        cnt[i]=1
        
for i in cnt.values():
    if i<2:
        continue
    if not i in comb:
        comb[i]=combinations_count(i,2)
    ans+=comb[i]

for i in range(n):
    pans=ans
    if cnt[a[i]] in comb:
        pans-=comb[cnt[a[i]]]
    c=cnt[a[i]]-1
    if c>1:
        if not c in comb:
            comb[c]=combinations_count(c,2)
        pans+=comb[c]
    print(pans)


Fixed submission:
import math

def combinations_count(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())
a=list(map(int,input().split()))
cnt={}
comb={}
ans=0
for i in a:
    if i in cnt:
        cnt[i]+=1
    else:
        cnt[i]=1
        
for i in cnt.values():
    if i<2:
        continue
    if not i in comb:
        comb[i]=combinations_count(i,2)
    ans+=comb[i]

for i in range(n):
    pans=ans
    if cnt[a[i]] in comb:
        pans-=comb[cnt[a[i]]]
    c=cnt[a[i]]-1
    if c>1:
        if not c in comb:
            comb[c]=combinations_count(c,2)
        pans+=comb[c]
    print(pans)

Buggy python submission (verdict: Time Limit Exceeded):
import math

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

n=int(input())
a=list(map(int,input().split()))
cnt={}
comb={}
ans=0
for i in a:
    if i in cnt:
        cnt[i]+=1
    else:
        cnt[i]=1
        
for i in cnt.values():
    if i<2:
        continue
    if not i in comb:
        comb[i]=combinations_count(i,2)
    ans+=comb[i]

for i in range(n):
    pans=ans
    if cnt[a[i]] in comb:
        pans-=comb[cnt[a[i]]]
    c=cnt[a[i]]-1
    if c>1:
        if c in comb:
            pans+=comb[c]
        else:
            pans+=combinations_count(c,2)
    print(pans)



Fixed submission:
import math

def combinations_count(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())
a=list(map(int,input().split()))
cnt={}
comb={}
ans=0
for i in a:
    if i in cnt:
        cnt[i]+=1
    else:
        cnt[i]=1
        
for i in cnt.values():
    if i<2:
        continue
    if not i in comb:
        comb[i]=combinations_count(i,2)
    ans+=comb[i]

for i in range(n):
    pans=ans
    if cnt[a[i]] in comb:
        pans-=comb[cnt[a[i]]]
    c=cnt[a[i]]-1
    if c>1:
        if not c in comb:
            comb[c]=combinations_count(c,2)
        pans+=comb[c]
    print(pans)