Buggy python submission (verdict: Time Limit Exceeded):
import collections
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()))
c = collections.Counter(A)
total_ans = 0
for k, v in c.items():
    if v > 1:
        total_ans += combinations_count(v, 2)

for k_ball in A:
    if c[k_ball] > 2:
        print(
            total_ans
            - (combinations_count(c[k_ball], 2) - combinations_count(c[k_ball] - 1, 2))
        )
    elif c[k_ball] > 1:
        print(total_ans - 1)
    else:
        print(total_ans)

Fixed submission:
# 解き直し
import collections

N = int(input())
A = list(map(int, input().split()))
c = collections.Counter(A)

total = 0
for v in c.values():
    total += v * (v - 1) // 2

for k_ball in A:
    cnt = c[k_ball]
    print(total - (cnt * (cnt - 1) // 2) + ((cnt - 1) * (cnt - 2) // 2))

Buggy python submission (verdict: Time Limit Exceeded):
import collections
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()))
c = collections.Counter(A)
ans = 0
for k, v in c.items():
    if v > 1:
        ans += combinations_count(v, 2)
total_ans = ans

for k_ball in A:
    if c[k_ball] > 2:
        print(
            total_ans
            - (combinations_count(c[k_ball], 2) - combinations_count(c[k_ball] - 1, 2))
        )
    elif c[k_ball] > 1:
        print(total_ans - 1)
    else:
        print(total_ans)

Fixed submission:
# 解き直し
import collections

N = int(input())
A = list(map(int, input().split()))
c = collections.Counter(A)

total = 0
for v in c.values():
    total += v * (v - 1) // 2

for k_ball in A:
    cnt = c[k_ball]
    print(total - (cnt * (cnt - 1) // 2) + ((cnt - 1) * (cnt - 2) // 2))

Buggy python submission (verdict: Time Limit Exceeded):
import collections
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()))
c = collections.Counter(A)
ans = 0
for k, v in c.items():
    if v > 1:
        ans += combinations_count(v, 2)
total_ans = ans

memo = {}
for k_ball in A:
    if k_ball in set(memo.keys()):
        print(memo[k_ball])
    elif c[k_ball] > 2:
        memo[k_ball] = total_ans - (
            combinations_count(c[k_ball], 2) - combinations_count(c[k_ball] - 1, 2)
        )
        print(memo[k_ball])
    elif c[k_ball] > 1:
        memo[k_ball] = total_ans - 1
        print(memo[k_ball])
    else:
        memo[k_ball] = total_ans
        print(memo[k_ball])

Fixed submission:
# 解き直し
import collections

N = int(input())
A = list(map(int, input().split()))
c = collections.Counter(A)

total = 0
for v in c.values():
    total += v * (v - 1) // 2

for k_ball in A:
    cnt = c[k_ball]
    print(total - (cnt * (cnt - 1) // 2) + ((cnt - 1) * (cnt - 2) // 2))

Buggy python submission (verdict: Runtime Error):
import collections
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()))
c = collections.Counter(A)
memo = {}
ans = 0
for k, v in c.items():
    if v > 1:
        memo[v] = combinations_count(v, 2)
        ans += memo[v]
total_ans = ans
for i in range(N):
    k_ball = A[i]
    if c[k_ball] > 2:
        if k_ball - 1 in set(memo.keys()):
            print(int(total_ans - (memo[c[k_ball]] - memo[c[k_ball - 1]])))
        else:
            print(
                int(
                    total_ans - (memo[c[k_ball]] - combinations_count(c[k_ball] - 1, 2))
                )
            )
    elif c[k_ball] > 1:
        print(total_ans - 1)
    else:
        print(total_ans)

Fixed submission:
# 解き直し
import collections

N = int(input())
A = list(map(int, input().split()))
c = collections.Counter(A)

total = 0
for v in c.values():
    total += v * (v - 1) // 2

for k_ball in A:
    cnt = c[k_ball]
    print(total - (cnt * (cnt - 1) // 2) + ((cnt - 1) * (cnt - 2) // 2))

Buggy python submission (verdict: Time Limit Exceeded):
import collections
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()))
c = collections.Counter(A)
ans = 0
for k, v in c.items():
    if v > 1:
        ans += combinations_count(v, 2)
total_ans = ans

for i in range(N):
    k_ball = A[i]
    if c[k_ball] > 2:
        print(
            total_ans
            - (combinations_count(c[k_ball], 2) - combinations_count(c[k_ball] - 1, 2))
        )
    elif c[k_ball] > 1:
        # 2
        print(total_ans - 1)
    else:
        print(total_ans)

Fixed submission:
# 解き直し
import collections

N = int(input())
A = list(map(int, input().split()))
c = collections.Counter(A)

total = 0
for v in c.values():
    total += v * (v - 1) // 2

for k_ball in A:
    cnt = c[k_ball]
    print(total - (cnt * (cnt - 1) // 2) + ((cnt - 1) * (cnt - 2) // 2))