Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
import sys
import math
import collections

def comb(n, r):
    if n < r:
        return 0
    else:
        return math.factorial(n)/math.factorial(n-r)/math.factorial(r)

def solve(N: int, A: "List[int]"):

    count_dict = collections.Counter(A)

    comb_list = []
    for i in range(0, N+1):
        comb_list.append(int(comb(i, 2)))

    ans_dic = {}
    sum_all = 0
    for key, value in count_dict.items():
        tmp = comb_list[value]
        ans_dic[key] = tmp
        sum_all += tmp

    for k in range(N):
        ans = sum_all - ans_dic[A[k]] \
            + comb_list[count_dict[A[k]]-1]
        print(ans)

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()


Fixed submission:
#!/usr/bin/env python3
import sys
import math
import collections

def comb(n, r):
    if n ==0 or n == 1: return 0
    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

def solve(N: int, A: "List[int]"):

    count_dict = collections.Counter(A)

    comb_list = []
    for i in range(0, N+1):
        comb_list.append(int(comb(i, 2)))

    ans_dic = {}
    sum_all = 0
    for key, value in count_dict.items():
        ans_dic[key] = comb_list[value]
        sum_all += comb_list[value]

    for k in range(N):
        ans = sum_all - ans_dic[A[k]] \
            + comb_list[count_dict[A[k]]-1]
        print(ans)

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
import sys
import math
import collections

def comb(n, r):
    if n < r:
        return 0
    else:
        return math.factorial(n)/math.factorial(n-r)/math.factorial(r)

def solve(N: int, A: "List[int]"):

    count_dict = collections.Counter(A)

    del_list = []
    for key, value in list(count_dict.items()):
        if value == 1:
            count_dict.pop(key)
            del_list.append(key)


    ans_dic = {}
    sum_all = 0
    for key, value in count_dict.items():
        tmp = int(comb(value, 2))
        ans_dic[key] = tmp
        sum_all += tmp


    for k in range(N):
        if A[k] in del_list:
            ans = sum_all
        else:
            ans = sum_all - ans_dic[A[k]] \
                + int(comb(count_dict[A[k]]-1, 2))
        print(ans)

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()


Fixed submission:
#!/usr/bin/env python3
import sys
import math
import collections

def comb(n, r):
    if n ==0 or n == 1: return 0
    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

def solve(N: int, A: "List[int]"):

    count_dict = collections.Counter(A)

    comb_list = []
    for i in range(0, N+1):
        comb_list.append(int(comb(i, 2)))

    ans_dic = {}
    sum_all = 0
    for key, value in count_dict.items():
        ans_dic[key] = comb_list[value]
        sum_all += comb_list[value]

    for k in range(N):
        ans = sum_all - ans_dic[A[k]] \
            + comb_list[count_dict[A[k]]-1]
        print(ans)

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
import sys
import math
import collections

def comb(n, r):
    if n < r:
        return 0
    else:
        return math.factorial(n)/math.factorial(n-r)/math.factorial(r)

def solve(N: int, A: "List[int]"):

    count_dict = collections.Counter(A)

#    for key, value in count_dict.items():
#        if value == 1:
#            count_dict.pop(key)

    ans_dic = {}
    sum_all = 0
    for key, value in count_dict.items():
        ans_dic[key] = int(comb(value, 2))
        sum_all += int(comb(value, 2))


    for k in range(N):
        ans = sum_all - ans_dic[A[k]] \
            + int(comb(count_dict[A[k]]-1, 2))
        print(ans)

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()


Fixed submission:
#!/usr/bin/env python3
import sys
import math
import collections

def comb(n, r):
    if n ==0 or n == 1: return 0
    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

def solve(N: int, A: "List[int]"):

    count_dict = collections.Counter(A)

    comb_list = []
    for i in range(0, N+1):
        comb_list.append(int(comb(i, 2)))

    ans_dic = {}
    sum_all = 0
    for key, value in count_dict.items():
        ans_dic[key] = comb_list[value]
        sum_all += comb_list[value]

    for k in range(N):
        ans = sum_all - ans_dic[A[k]] \
            + comb_list[count_dict[A[k]]-1]
        print(ans)

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
import sys
import math
import collections

def comb(n, r):
    if n < r:
        return 0
    else:
        return math.factorial(n)/math.factorial(n-r)/math.factorial(r)

def solve(N: int, A: "List[int]"):

    count_dict = collections.Counter(A)

    for k in range(N):
        ans = 0
        for key, value in count_dict.items():
            if key == A[k]:
                value -= 1
            ans += int(comb(value, 2))
        print(ans)

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()


Fixed submission:
#!/usr/bin/env python3
import sys
import math
import collections

def comb(n, r):
    if n ==0 or n == 1: return 0
    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

def solve(N: int, A: "List[int]"):

    count_dict = collections.Counter(A)

    comb_list = []
    for i in range(0, N+1):
        comb_list.append(int(comb(i, 2)))

    ans_dic = {}
    sum_all = 0
    for key, value in count_dict.items():
        ans_dic[key] = comb_list[value]
        sum_all += comb_list[value]

    for k in range(N):
        ans = sum_all - ans_dic[A[k]] \
            + comb_list[count_dict[A[k]]-1]
        print(ans)

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()