Buggy python submission (verdict: Time Limit Exceeded):
import collections
import itertools
def main():
    N = int(input())
    A = list(map(lambda x:x-1 ,map(int, input().split())))

    coll = collections.Counter(A[1:])
    counter = [0]*N
    comb = [0]*N
    sum1 = 0
    for i in range(N):
        c=coll.get(i,0)
        counter[i] = c
        if c>1:
            comb[i] = len(list(itertools.combinations([i for i in range(c)],2)))
            sum1 += comb[i]
    print(sum1)

    for i in range(1,N):
        # ボールA[i-1]を復活させて、ボールA[i]を除外
        n = counter[A[i-1]]
        if n>=1:
            sum1 += n
        counter[A[i-1]] += 1
        n = counter[A[i]]
        if n>=2:
            sum1 -= n-1
        counter[A[i]] -= 1
        print(sum1)

main()


Fixed submission:
from operator import mul
from functools import reduce
import collections

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 = list(map(lambda x:x-1 ,map(int, input().split())))

    coll = collections.Counter(A[1:])
    counter = [0]*N
    comb = [0]*N
    sum1 = 0
    for i in range(N):
        c=coll.get(i,0)
        counter[i] = c
        if c>1:
            comb[i] = combinations_count(c,2)
            sum1 += comb[i]
    print(sum1)

    for i in range(1,N):
        # ボールA[i-1]を復活させて、ボールA[i]を除外
        n = counter[A[i-1]]
        if n>=1:
            sum1 += n
        counter[A[i-1]] += 1
        n = counter[A[i]]
        if n>=2:
            sum1 -= n-1
        counter[A[i]] -= 1
        print(sum1)

main()

Buggy python submission (verdict: Time Limit Exceeded):
import collections
import itertools
def main():
    N = int(input())
    A = list(map(lambda x:x-1 ,map(int, input().split())))

    coll = collections.Counter(A[1:])
    counter = [0]*N
    comb = [0]*N
    for i in range(N):
        c=coll.get(i,0)
        counter[i] = c
        if c>1:
            comb[i] = len(list(itertools.combinations([i for i in range(c)],2)))
    sum1 = sum(comb)
    print(sum1)

    for i in range(1,N):
        # ボールA[i-1]を復活させて、ボールA[i]を除外
        n = counter[A[i-1]]
        if n>=1:
            sum1 += n
        counter[A[i-1]] += 1
        n = counter[A[i]]
        if n>=2:
            sum1 -= n-1
        counter[A[i]] -= 1
        print(sum1)

main()


Fixed submission:
from operator import mul
from functools import reduce
import collections

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 = list(map(lambda x:x-1 ,map(int, input().split())))

    coll = collections.Counter(A[1:])
    counter = [0]*N
    comb = [0]*N
    sum1 = 0
    for i in range(N):
        c=coll.get(i,0)
        counter[i] = c
        if c>1:
            comb[i] = combinations_count(c,2)
            sum1 += comb[i]
    print(sum1)

    for i in range(1,N):
        # ボールA[i-1]を復活させて、ボールA[i]を除外
        n = counter[A[i-1]]
        if n>=1:
            sum1 += n
        counter[A[i-1]] += 1
        n = counter[A[i]]
        if n>=2:
            sum1 -= n-1
        counter[A[i]] -= 1
        print(sum1)

main()

Buggy python submission (verdict: Time Limit Exceeded):
import collections
import itertools
def main():
    N = int(input())
    A = list(map(lambda x:x-1 ,map(int, input().split())))

    coll = collections.Counter(A[1:])
    counter = [0]*N
    comb = [0]*N
    for i in range(N):
        c=coll.get(i,0)
        counter[i] = c
        comb[i] = len(list(itertools.combinations([i for i in range(c)],2)))
    print(sum(comb))

    for i in range(1,N):
        # ボールA[i-1]を復活させて、ボールA[i]を除外
        n = counter[A[i-1]]
        if n>=1:
            comb[A[i-1]] += n
        counter[A[i-1]] += 1
        n = counter[A[i]]
        if n>=2:
            comb[A[i]] -= n-1
        else:
            comb[A[i]] = 0
        counter[A[i]] -= 1
        print(sum(comb))

main()


Fixed submission:
from operator import mul
from functools import reduce
import collections

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 = list(map(lambda x:x-1 ,map(int, input().split())))

    coll = collections.Counter(A[1:])
    counter = [0]*N
    comb = [0]*N
    sum1 = 0
    for i in range(N):
        c=coll.get(i,0)
        counter[i] = c
        if c>1:
            comb[i] = combinations_count(c,2)
            sum1 += comb[i]
    print(sum1)

    for i in range(1,N):
        # ボールA[i-1]を復活させて、ボールA[i]を除外
        n = counter[A[i-1]]
        if n>=1:
            sum1 += n
        counter[A[i-1]] += 1
        n = counter[A[i]]
        if n>=2:
            sum1 -= n-1
        counter[A[i]] -= 1
        print(sum1)

main()

Buggy python submission (verdict: Time Limit Exceeded):
import collections
import itertools
def main():
    N = int(input())
    A = list(map(int, input().split()))

    for i in range(N):
        coll = collections.Counter(A)
        coll[A[i]] -= 1
        ans = 0
        for j in range(1,N+1):
            c = coll[j]
            if c>1:
                ans += len(list(itertools.combinations([i for i in range(c)],2)))
        print(ans)

main()


Fixed submission:
from operator import mul
from functools import reduce
import collections

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 = list(map(lambda x:x-1 ,map(int, input().split())))

    coll = collections.Counter(A[1:])
    counter = [0]*N
    comb = [0]*N
    sum1 = 0
    for i in range(N):
        c=coll.get(i,0)
        counter[i] = c
        if c>1:
            comb[i] = combinations_count(c,2)
            sum1 += comb[i]
    print(sum1)

    for i in range(1,N):
        # ボールA[i-1]を復活させて、ボールA[i]を除外
        n = counter[A[i-1]]
        if n>=1:
            sum1 += n
        counter[A[i-1]] += 1
        n = counter[A[i]]
        if n>=2:
            sum1 -= n-1
        counter[A[i]] -= 1
        print(sum1)

main()