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

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()))

a_dic = collections.Counter(A)


default = {}
for key, value in a_dic.most_common():
  if value >= 2:
    default[key] = combinations_count(value, 2)
  else:
    default[key] = 0
default_amount = sum(default.values())

for k in range(N):
  key = A[k]
  value = a_dic[key] - 1
  if value >= 2:
    diff = combinations_count(value, 2)
  else:
    diff = 0
  print(default_amount - default[key] + diff)


Fixed submission:
import math
import collections

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()))

a_dic = collections.Counter(A)


default = {}
for key, value in a_dic.most_common():
  if value >= 2:
    default[key] = combinations_count(value, 2)
  else:
    default[key] = 0
default_amount = sum(default.values())

for k in range(N):
  key = A[k]
  value = a_dic[key] - 1
  if value >= 2:
    diff = combinations_count(value, 2)
  else:
    diff = 0
  print(default_amount - default[key] + diff)

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

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()))

a_dic = collections.Counter(A)


default = {}
for key, value in a_dic.most_common():
  if value >= 2:
    default[key] = combinations_count(value, 2)
  else:
    default[key] = 0
default_amount = sum(default.values())

for k in range(N):
  key = A[k]
  value = a_dic[key] - 1
  if value >= 2:
    diff = combinations_count(value, 2)
  else:
    diff = 0
  print(default_amount - default[key] + diff)


Fixed submission:
import math
import collections

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()))

a_dic = collections.Counter(A)


default = {}
for key, value in a_dic.most_common():
  if value >= 2:
    default[key] = combinations_count(value, 2)
  else:
    default[key] = 0
default_amount = sum(default.values())

for k in range(N):
  key = A[k]
  value = a_dic[key] - 1
  if value >= 2:
    diff = combinations_count(value, 2)
  else:
    diff = 0
  print(default_amount - default[key] + diff)