Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

import math
import collections

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

def main():
    n = int(input())
    a_li = list(map(int, input().rstrip().split()))
    c = collections.Counter(a_li)
    total = 0
    for k, v in c.items():
        if v > 1:
            total += combinations_count(v, 2)

    for a in a_li:
        n = c[a]
        if n > 1 and n-1 > 1:
            ans = total - combinations_count(n, 2) + combinations_count(n-1, 2)
            print(ans)
        elif n > 1:
            ans = total - combinations_count(n, 2)
            print(ans)
        else:
            ans = total
            print(ans)

if __name__ == '__main__':
    main()


Fixed submission:
import sys
input = sys.stdin.readline

import collections

from operator import mul
from functools import reduce

def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

def main():
    n = int(input())
    a_li = list(map(int, input().rstrip().split()))
    c = collections.Counter(a_li)
    total = 0
    dict_1 = {}
    dict_2 = {}
    for k, v in c.items():
        if v > 1:
            num = combinations_count(v, 2)
            total += num
            dict_1[k] = num
            if v - 1 > 1:
                dict_2[k] = combinations_count(v-1, 2)
            else:
                dict_2[k] = 0
        else:
            dict_1[k] = 0
            dict_2[k] = 0

    for a in a_li:
        ans = total - dict_1[a] + dict_2[a]
        print(ans)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

import math
import collections

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

def main():
    n = int(input())
    a_li = list(map(int, input().rstrip().split()))

    for i in range(n):
        li = a_li.copy()
        li.pop(i)
        c = collections.Counter(li)
        ans = 0
        for k, v in c.items():
            if v > 1:
                ans += combinations_count(v, 2)

        print(ans)

if __name__ == '__main__':
    main()


Fixed submission:
import sys
input = sys.stdin.readline

import collections

from operator import mul
from functools import reduce

def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

def main():
    n = int(input())
    a_li = list(map(int, input().rstrip().split()))
    c = collections.Counter(a_li)
    total = 0
    dict_1 = {}
    dict_2 = {}
    for k, v in c.items():
        if v > 1:
            num = combinations_count(v, 2)
            total += num
            dict_1[k] = num
            if v - 1 > 1:
                dict_2[k] = combinations_count(v-1, 2)
            else:
                dict_2[k] = 0
        else:
            dict_1[k] = 0
            dict_2[k] = 0

    for a in a_li:
        ans = total - dict_1[a] + dict_2[a]
        print(ans)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

import math
import collections

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

def main():
    n = int(input())
    a_li = list(map(int, input().rstrip().split()))

    for i in range(n):
        li = a_li.copy()
        li.pop(i)
        li.sort()
        c = collections.Counter(li)
        ans = 0
        for k, v in c.items():
            if v > 1:
                ans += combinations_count(v, 2)

        print(ans)

if __name__ == '__main__':
    main()


Fixed submission:
import sys
input = sys.stdin.readline

import collections

from operator import mul
from functools import reduce

def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

def main():
    n = int(input())
    a_li = list(map(int, input().rstrip().split()))
    c = collections.Counter(a_li)
    total = 0
    dict_1 = {}
    dict_2 = {}
    for k, v in c.items():
        if v > 1:
            num = combinations_count(v, 2)
            total += num
            dict_1[k] = num
            if v - 1 > 1:
                dict_2[k] = combinations_count(v-1, 2)
            else:
                dict_2[k] = 0
        else:
            dict_1[k] = 0
            dict_2[k] = 0

    for a in a_li:
        ans = total - dict_1[a] + dict_2[a]
        print(ans)

if __name__ == '__main__':
    main()