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

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

n = int(input())
a = list(map(int,input().split()))
n_list = [[] for i in range(n)]
len_list = [0 for i in range(n)]

for i in range(n):
    n_list[a[i]-1].append(i)

for i in range(n):
    if len(n_list[i]) == 0:
        len_list[i] == 0
    elif len(n_list[i]) == 1:
        len_list[i] == 1
    else:
        len_list[i] = combinations_count(len(n_list[i]))

sum_len = sum(len_list)

for i in range(n):
    if len_list[a[i]-1] == 0:
        print(sum_len)
    elif len_list[a[i]-1] == 1 or len_list[a[i]-1] == 2:
        print(sum_len - 1)
    else:
        print(sum_len - len_list[a[i]-1] + combinations_count(len(n_list[a[i]-1]) - 1))


Fixed submission:
import math

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

n = int(input())
a = list(map(int,input().split()))
n_list = [0 for i in range(n)]
len_list = [0 for i in range(n)]

for i in range(n):
    n_list[a[i]-1] += 1

sum_len = 0

for i in range(n):
    if n_list[i] == 0:
        len_list[i] == 0
    elif n_list[i] == 1:
        len_list[i] == 1
    else:
        len_list[i] = combinations_count(n_list[i])
    sum_len += len_list[i]

for i in range(n):
    if len_list[a[i]-1] == 0:
        print(sum_len)
    elif len_list[a[i]-1] == 1 or len_list[a[i]-1] == 2:
        print(sum_len - 1)
    else:
        print(sum_len - (n_list[a[i]-1] - 1))

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

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

n = int(input())
a = list(map(int,input().split()))
n_list = [[] for i in range(n)]
len_list = [0 for i in range(n)]

for i in range(n):
    n_list[a[i]-1].append(i)

for i in range(n):
    if len(n_list[i]) == 0:
        len_list[i] == 0
    elif len(n_list[i]) == 1:
        len_list[i] == 1
    else:
        len_list[i] = combinations_count(len(n_list[i]))

sum_len = sum(len_list)
ans_list = [sum_len for i in range(n)]

for i in range(n):
    if len_list[a[i]-1] == 0:
        continue
    elif len_list[a[i]-1] == 1:
        ans_list[i] = ans_list[i] - 1
    elif len_list[a[i]-1] == 2:
        ans_list[i] = ans_list[i] - 1
    else:
        ans_list[i] = ans_list[i] - len_list[a[i]-1] + combinations_count(len(n_list[a[i]-1]) - 1)

for i in range(n):
    print(ans_list[i])


Fixed submission:
import math

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

n = int(input())
a = list(map(int,input().split()))
n_list = [0 for i in range(n)]
len_list = [0 for i in range(n)]

for i in range(n):
    n_list[a[i]-1] += 1

sum_len = 0

for i in range(n):
    if n_list[i] == 0:
        len_list[i] == 0
    elif n_list[i] == 1:
        len_list[i] == 1
    else:
        len_list[i] = combinations_count(n_list[i])
    sum_len += len_list[i]

for i in range(n):
    if len_list[a[i]-1] == 0:
        print(sum_len)
    elif len_list[a[i]-1] == 1 or len_list[a[i]-1] == 2:
        print(sum_len - 1)
    else:
        print(sum_len - (n_list[a[i]-1] - 1))