Buggy python submission (verdict: Runtime Error):
import collections
n = int(input())
*a, = map(int, input().split())
counter = collections.Counter(a)
d = {key: (value * (value - 1)) // 2 for key, value in counter.items()}
#print(d)
all_sum = sum(d.values())
#print(all_sum)

sum_d = {}
for k in range(n):
    if a[k] not in sum_d:
        if d[a[k]] > 0:
            tmp = ((counter[a[k]] - 1) * (counter[a[k]] - 2)) // 2
        sum_d[a[k]] = all_sum  - d[a[k]] + tmp
        print(sum_d[a[k]])
    else:
        print(sum_d[a[k]])

Fixed submission:
import collections
n = int(input())
*a, = map(int, input().split())
counter = collections.Counter(a)
d = {key: (value * (value - 1)) // 2 for key, value in counter.items()}
#print(d)
all_sum = sum(d.values())
#print(all_sum)

sum_d = {}
for k in range(n):
    if a[k] not in sum_d:
        if d[a[k]] > 0:
            tmp = ((counter[a[k]] - 1) * (counter[a[k]] - 2)) // 2
            sum_d[a[k]] = all_sum  - d[a[k]] + tmp
            print(sum_d[a[k]])
        else:
            print(all_sum)
    else:
        print(sum_d[a[k]])

Buggy python submission (verdict: Runtime Error):
import collections
n = int(input())
*a, = map(int, input().split())
counter = collections.Counter(a)
d = {}
for key, value in counter.items():
    d[key] = (value * (value - 1)) // 2

sum_d = {}
for k in range(n):
    if a[k] not in sum_d:
        if d[a[k]] > 0: tmp = ((counter[a[k]] - 1) * (counter[a[k]] - 2)) // 2
        sum_d[a[k]] = sum(d.values())  - d[a[k]] + tmp
        print(sum_d[a[k]])
    else:
        print(sum_d[a[k]])

Fixed submission:
import collections
n = int(input())
*a, = map(int, input().split())
counter = collections.Counter(a)
d = {key: (value * (value - 1)) // 2 for key, value in counter.items()}
#print(d)
all_sum = sum(d.values())
#print(all_sum)

sum_d = {}
for k in range(n):
    if a[k] not in sum_d:
        if d[a[k]] > 0:
            tmp = ((counter[a[k]] - 1) * (counter[a[k]] - 2)) // 2
            sum_d[a[k]] = all_sum  - d[a[k]] + tmp
            print(sum_d[a[k]])
        else:
            print(all_sum)
    else:
        print(sum_d[a[k]])

Buggy python submission (verdict: Time Limit Exceeded):
import collections
n = int(input())
*a, = map(int, input().split())
counter = collections.Counter(a)
#print(counter)
d = {}
for key, value in counter.items():
    d[key] = (value * (value - 1)) // 2
#print(counter)

#print('d:', d, end='\n\n')

for k in range(n):
    d2 = d.copy()
    '''
    print('d2:', d2)
    print(f'a[{k}]:', a[k], end=', ')
    print(f'd[{a[k]}]:', d[a[k]])
    print('計算')
    print(f'(counter[a[k]] - 1):{(counter[a[k]] - 1)}')
    print(f'(counter[a[k]] - 2): {(counter[a[k]] - 2)}')
    '''
    
    if d[a[k]] > 0: d2[a[k]] = ((counter[a[k]] - 1) * (counter[a[k]] - 2)) // 2
    #print(f'd2({a[k]}除く):', d2)
    print(sum(d2.values()))

Fixed submission:
import collections
n = int(input())
*a, = map(int, input().split())
counter = collections.Counter(a)
d = {key: (value * (value - 1)) // 2 for key, value in counter.items()}
#print(d)
all_sum = sum(d.values())
#print(all_sum)

sum_d = {}
for k in range(n):
    if a[k] not in sum_d:
        if d[a[k]] > 0:
            tmp = ((counter[a[k]] - 1) * (counter[a[k]] - 2)) // 2
            sum_d[a[k]] = all_sum  - d[a[k]] + tmp
            print(sum_d[a[k]])
        else:
            print(all_sum)
    else:
        print(sum_d[a[k]])