Problem:
 Score : 400 points    Problem Statement  We have N balls. The i -th ball has an integer A_i written on it. For each k=1, 2, ..., N , solve the following problem and print the answer.   Find the number of ways to choose two distinct balls (disregarding order) from the N-1 balls other than the k -th ball so that the integers written on them are equal.       Constraints   3 \leq N \leq 2 \times 10^5  1 \leq A_i \leq N  All values in input are integers.         Input  Input is given from Standard Input in the following format:  N  A_1  A_2  ...  A_N       Output  For each k=1,2,...,N , print a line containing the answer.        Sample Input 1 5
1 1 2 1 2      Sample Output 1 2
2
3
2
3  Consider the case k=1 for example. The numbers written on the remaining balls are 1,2,1,2 . From these balls, there are two ways to choose two distinct balls so that the integers written on them are equal. Thus, the answer for k=1 is 2 .       Sample Input 2 4
1 2 3 4      Sample Output 2 0
0
0
0  No two balls have equal numbers written on them.       Sample Input 3 5
3 3 3 3 3      Sample Output 3 6
6
6
6
6  Any two balls have equal numbers written on them.       Sample Input 4 8
1 2 1 4 2 1 4 1      Sample Output 4 5
7
5
7
7
5
7
5

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
lst = [0]*(max(a)+1)

for i in a:
    lst[i] += 1

num_lst = [0]*(max(a)+1)
for i in range(1,max(a)+1):
    if not lst[i] in [0,1]:
        num_lst[i] = lst[i]*(lst[i]-1)//2
ans = sum(num_lst)

for i in a:
    if lst[i] in [0,1]:
        print(ans)
    else:
        print(ans-num_lst[i]+((lst[i]-1)*(lst[i]-2)//2))

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
B=[0 for i in range(N+1)]
C=[0 for i in range(N+1)]
D=[0 for i in range(N+1)]
for i in range(N):
    B[A[i]]+=1
for i in range(1,N+1):
    C[i]=B[i]*(B[i]-1)//2
sum_=sum(C)
for i in range(1,N+1):
    print(sum_-B[A[i-1]]*(B[A[i-1]]-1)//2+(B[A[i-1]]-2)*(B[A[i-1]]-1)//2)

Solution (Python):
import collections
N=int(input())
A=list(map(int,input().split()))

D = collections.Counter(A)
s=0
for i in D:
  s+=D[i]*(D[i]-1)//2
  
for i in range(N):
  print(s-(D[A[i]]-1))

Solution (Python):
import collections
N = int(input())
A = list(map(int, input().split()))

c = collections.Counter(A)
ans = 0
for i in c:
  ans+=c[i]*(c[i]-1)//2

for i in range(N):
  print(ans-(c[A[i]]-1))

Solution (Python):
from collections import Counter
N = int(input())
A = [int(x) for x in input().split()]


c = Counter(A)

ans = 0
for i in c:
    ans += c[i] * (c[i] - 1) // 2

for i in range(len(A)):
    if c[A[i]] == 1:
        print(ans)
    else:
        tmp = c[A[i]]
        print(ans - tmp * (tmp - 1) // 2 + (tmp - 1) * (tmp - 2) // 2)

Solution (Python):
def sep():
    return map(int,input().strip().split(" "))
def lis():
    return list(sep())

n=int(input())
from collections import Counter
ar=lis()
c=Counter(ar)
s=0
for i in c.values():
    s+=((i*(i-1))//2)
for i in ar:
    print(s-c[i]+1)

Solution (Python):
def nC2(n):
    if n <= 1:
        return 0
    return n * (n - 1) // 2

N = int(input())
A = list(map(int, input().split()))
A = list(map(lambda x: x - 1, A))

A_count = [0] * N
for a in A:
    A_count[a] += 1

combis = [0] * N
for i, a in enumerate(A_count):
    combis[i] = nC2(a)

combis_m1 = [0] * N
for i, a in enumerate(A_count):
    combis_m1[i] = nC2(a-1)

total_sum = sum(combis)
for a in A:
    print(total_sum - combis[a] + combis_m1[a])

Solution (Python):
from collections import Counter
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=Counter(a)
com=0
for i in range(1,n+1):
    if(c[i]<=1):
        continue
    com+=combinations_count(c[i],2)
for k in range(1,n+1):
    ans=com
    ans-=c[a[k-1]]-1
    print(ans)

Solution (Python):
from collections import Counter
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=Counter(a)
com=0
for i in range(1,n+1):
    if(c[i]<=1):
        continue
    com+=combinations_count(c[i],2)
for k in range(1,n+1):
    ans=com
    ans-=c[a[k-1]]-1
    print(ans)

Solution (Python):
import collections

n = int(input())
a = list(map(int, input().split()))
cnt = 0

c = collections.Counter(a)
for i in c:
  cnt += c[i]*(c[i]-1)//2



for i in range(n):
	print(cnt-c[a[i]]+1)

Solution (Python):
import collections
n = int(input())
num_list = list(map(int, input().split()))
all = 0

c = collections.Counter(num_list)

for i in c:
    all += c[i]*(c[i]-1)//2

for k in range(n):
    print(all - c[num_list[k]] + 1)

Solution (Python):
N = int(input())
A = list(map(int, input().strip().split()))
from collections import Counter
C=Counter(A)
K=set(A)
ans = 0
for i in K:
    ans += C[i]*(C[i]-1)//2

for i in A:
    print(ans-(C[i]-1))

Solution (Python):
n = int(input())
lst = [ int(i) for i in input().split() ]

visited = {}

for d in lst:
  if d in visited.keys():
    visited[d] += 1
  else:
    visited[d] = 1
count = 0
for d in visited.keys():
  count += int(visited[d]*(visited[d]-1)/2)
for d in lst:
  v = visited[d] - 1
  ans = count - v
  print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    A=LI()
    from collections import defaultdict
    dd = defaultdict(int)
    
    for i in range(N):
        dd[A[i]]+=1
    
    ans=0
    for k,v in dd.items():
        ans+=(v*(v-1))//2
        
    for i in range(N):
        a=A[i]
        v=dd[A[i]]
        temp=ans
        temp-=(v*(v-1))//2
        temp+=((v-1)*(v-2))//2
        
        print(temp)
    
main()

Solution (Python):
from collections import Counter

n = int(input())
a_array = list(map(int, input().split()))
a_counter = Counter(a_array)
count_max = max(a_counter.values())

def nC2(n):
    return n * (n - 1) // 2

nc2_array = [0] * (count_max + 1)
sum_comb = 0
for a, count in a_counter.items():
    comb = nC2(count)
    sum_comb += comb
    nc2_array[count] = comb
    nc2_array[count - 1] = nC2(count - 1)

for a in a_array:
    a_count = a_counter[a]
    ans = sum_comb - nc2_array[a_count] + nc2_array[a_count - 1]
    print(ans)

Solution (Python):
import collections
def resolve():
    n = int(input())
    a = tuple(map(int,input().split()))
    A = collections.Counter(a)
    cnt_0 = 0
    for i in A.values():
        cnt_0 += int(i*(i-1)/2)
    for i in a:
        cnt_1 = A[i]-1
        print(cnt_0-cnt_1)
resolve()

Solution (Python):
N = int(input())
As = list(map(int, input().split()))
count_dict = {}


def calc_combo(n):
    return int(n * (n - 1) / 2)


for i in range(N):
    try:
        count_dict[As[i]] += 1
    except KeyError:
        count_dict[As[i]] = 1

combo = 0
for key in count_dict:
    combo += calc_combo(count_dict[key])

for i in range(N):
    num_delete = As[i]
    try:
        num_count = count_dict[num_delete]
        out = combo - calc_combo(num_count) + calc_combo(num_count - 1)
        print(out)
    except KeyError:
        print(combo)

Solution (Python):
n=(int)(input())
a=input().split()
for i in range(n):
  a[i]=(int)(a[i])
  
dic={}
sum=0
for i in range(n):
  if not a[i] in dic:
    dic[a[i]]=1
  else:
    dic[a[i]]+=1
    sum+=dic[a[i]]-1

for i in a:
  print(sum-dic[i]+1)

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
x=[0]*n
for i in range(n):
  x[l[i]-1]+=1
d=0
for i in range(n):
  d=d+x[i]*(x[i]-1)//2

for i in range(n):
  b=d-(x[l[i]-1]-1)
  print(b)

Solution (Python):
import collections

n = int(input())
a = list(map(int, input().split()))
c = collections.Counter(a)

cmb = 0
for x in c.values():
    cmb += x * (x - 1) // 2
    
for z in a:
    y = c.get(z)
    before = y * (y - 1) // 2
    after = (y - 1) * (y - 2) //2
    ans = cmb - before + after
    print(ans)

Solution (Python):
import collections
import math

def cmb_factorial(s):
    return math.factorial(s) // (math.factorial(2) * math.factorial(s - 2))

n = int(input())
a = list(map(int, input().split()))
c = collections.Counter(a)
f = {}

cmb = 0
for x in c.values():
    f[1] = 0
    f[2] = 1
    if x >= 3:
        f[x] = cmb_factorial(x)
        if x - 1 not in f:
            f[x - 1] = cmb_factorial(x - 1)
    cmb += f.get(x)

for z in a:
    y = c.get(z)
    ans = cmb
    if y >= 2:
        ans = cmb - f.get(y) + f.get(y - 1)
    print(ans)

Solution (Python):
from collections import *
from math import *
N = int(input())
A = list(map(int,input().split()))
C = Counter(A)
S = 0

for k,v in C.items():
  S+=comb(v,2)

for a in A:
  print(S-(comb(C[a],2)-comb(C[a]-1,2)))

Solution (Python):
N = int(input())
Alst = list(map(int, input().split()))
Blst = [0]*(N+1)
num = 0
for i in Alst:
    num = num + Blst[i]
    Blst[i] += 1

for i in Alst:
    k = Blst[i] -1
    print(num - k)

Solution (Python):
N = int(input())
Alst = list(map(int, input().split()))
Blst = [0]*(N+1)
num = 0
for i in Alst:
    num = num + Blst[i]
    Blst[i] += 1

for i in Alst:
    k = Blst[i] -1
    print(num - k)

Solution (Python):
import math
from math import gcd,pi,sqrt
INF = float("inf")

import sys
sys.setrecursionlimit(10**6)
import itertools
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]

def main():
    n = i_input()
    a = i_list()

    k = [0]
    for i in range(2*10**5):
        k.append(k[i]+(i+1))
    t = a.copy()
    t.sort()

    s = 0

    m = [0]*n

    for i,n in itertools.groupby(t):
        l = len(list(n))
        s += k[l-1]
        if l > 1:
            m[i-1] = k[l-2] - k[l-1]

    for i in a:
        print(s + m[i-1])







if __name__=="__main__":
    main()

Solution (Python):
""" スニペット """
def get_int():
	return int(input())

def get_ints():
	return list(map(int, input().split()))
""" スニペット """
import math

def choose2(n):
    return math.floor(n*(n-1)/2)


N = get_int()
An = get_ints()
""" 全ての要素から2つのボールを選ぶ通り数を求める """
uni = [0] * (N+1)

for i in range(N):
    uni[An[i]] += 1
sumWay = 0

for i in range(N+1):
    sumWay += choose2(uni[i])
""" 全ての要素数 - 削除する要素の通り数 + 削除する要素を引いた際の通り数 を求める """
for i in range(N):
    print(sumWay - choose2(uni[An[i]]) + choose2(uni[An[i]]-1))

Solution (Python):
""" スニペット """
def get_int():
	return int(input())

def get_ints():
	return list(map(int, input().split()))
""" スニペット """
import math

def choose2(n):
    return n*(n-1)//2

N = get_int()
An = get_ints()
""" 全ての要素から2つのボールを選ぶ通り数を求める """
uni = [0] * (N+1)

for i in range(N):
    uni[An[i]] += 1
sumWay = 0
con = [0] * (N+1)	
for i in range(N+1):
  con[i] = choose2(i)

"""
for i in range(N + 1):
    sumWay += con(uni[i])
"""

for i in uni:
  sumWay += con[i]
""" 全ての要素数 - 削除する要素の通り数 + 削除する要素を引いた際の通り数 を求める """
for i in range(N):
    print(sumWay - con[uni[An[i]]] + con[uni[An[i]]-1])

Solution (Python):
""" スニペット """
def get_int():
	return int(input())

def get_ints():
	return list(map(int, input().split()))
""" スニペット """
import math

def choose2(n):
    return n*(n-1)//2

N = get_int()
An = get_ints()
""" 全ての要素から2つのボールを選ぶ通り数を求める """
uni = [0] * (N+1)

for i in range(N):
    uni[An[i]] += 1
sumWay = 0

for i in range(N + 1):
    sumWay += choose2(uni[i])
""" 全ての要素数 - 削除する要素の通り数 + 削除する要素を引いた際の通り数 を求める """
for i in range(N):
    print((sumWay - choose2(uni[An[i]]) + choose2(uni[An[i]]-1)))

Solution (Python):
def abc159d_banned_k():
    def cmb(n, r):
        """ nCrの組み合わせ数を返す """
        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()))
    dict_a = {}
    for item in a:
        if item in dict_a.keys():
            dict_a[item] += 1
        else:
            dict_a[item] = 1
    cnt = 0
    for k in dict_a:
        if dict_a[k] > 1:
            cnt += cmb(dict_a[k],2)
    for item in a:
        if dict_a[item] > 2:
            print(cnt - cmb(dict_a[item],2) + cmb(dict_a[item]-1,2))
        elif dict_a[item] == 2:
            print(cnt - 1)
        else:
            print(cnt)

abc159d_banned_k()

Solution (Python):
n = int(input())
cnt = [0] * (n + 1)
cmb = 0
a = list(map(int, input().split()))

for ai in a:
  cnt[ai] += 1
  cmb += cnt[ai] - 1

for ai in a:
  print(cmb - cnt[ai] + 1)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
d=dict()
for i in range(N):
    if A[i] not in d:
        d[A[i]]=0
    d[A[i]]+=1
x=sum(x*(x-1)//2 for x in d.values())
for i in range(N):
    y=d[A[i]]
    print(x-y*(y-1)//2+(y-1)*(y-2)//2)

Solution (Python):
from collections import Counter
n=int(input())
A=list(map(int,input().split()))
cnt=Counter(A)
tot=0
dif={}
for k,v in cnt.items():
  tot += v*(v-1)//2
  dif[k]=v-1
ans=[tot-dif[a] for a in A]
print('\n'.join(map(str,ans)))

Solution (Python):
from collections import Counter
from operator import mul
from functools import reduce


def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under


n = int(input())
al = list(map(int, input().split()))
c = Counter(al)

k = 0
for v in c.values():
    if v == 1:
        continue
    else:
        k += cmb(v, 2)

for a in al:
    if c[a] == 1:
        print(k)
    elif c[a] == 2:
        print(k-cmb(c[a], 2))
    else:
        print(k-cmb(c[a], 2)+cmb(c[a]-1,2))

Solution (Python):
N = int(input())
A = input().split()
num_list = [0] * N

def combination(n):
    C = [0]
    for i in range(2,n+1):
        C.append(int(i*(i-1)/2))
    
    return C

com = combination(N)
sum_com = 0

for i in range(N):
    num_list[int(A[i])-1] += 1


for i in range(N):
    if num_list[i] == 0:
        continue
    else:
        sum_com += com[num_list[i]-1]

for i in range(N):
    print(sum_com - num_list[int(A[i])-1] + 1)

Solution (Python):
from collections import Counter

n=int(input())
A=list(map(int,input().split()))

c=Counter(A)
s=0
for v in c.values():
  s+=v*(v-1)//2
for i in range(n):
  ans=s-(c[A[i]])+1
  print(ans)

Solution (Python):
from collections import defaultdict
def main():
    n = int(input())
    a = list(map(int, input().split(" ")))
    d = defaultdict(lambda:0)
    d2 = {}
    for i in a:
        d[i] += 1
    for i,j in d.items():
        x = j*(j-1)//2
        d2[i] = x
    ans = 0
    ans = sum(d2.values())
    for i in range(n):
        print(ans-d[a[i]]+1)

if __name__ == "__main__":
    main()

Solution (Python):
import math


def combination(n, r):
    if n <= 1:
        return 0
    elif n == 2:
        return 1
    else:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


def count_list(lst):
    count_lst = [0] * (max(lst) + 1)

    for i in lst:
        count_lst[i] += 1

    return count_lst


def main():
    n = int(input())
    a_lst = list(map(int, input().split()))
    lst1 = count_list(a_lst)
    lst2 = [0] * len(lst1)
    for i in range(len(lst1)):
        lst2[i] = combination(lst1[i], 2)
    tmp = sum(lst2)

    for i in range(n):
        a = a_lst[i]
        print(tmp - (lst1[a] - 1))


if __name__ == '__main__':
    main()

Solution (Python):
from copy import copy
from collections import Counter
from scipy.special import comb

n = int(input())
A = list(map(int,input().split()))

C = Counter(A)
S = 0
for i in range(1,n+1):
  S += comb(C[i],2,exact=True)

for k in range(n):
  p = A[k]
  ans = S - comb(C[p],2,exact=True) + comb(C[p]-1,2,exact=True)
  print(ans)

Solution (Python):
import collections
N=int(input())
A=list(map(int,input().split()))

cc=collections.Counter(A)
L=list(cc.values())

total=0
for i in range(len(L)):
    total+=L[i]*(L[i]-1)//2

ans=0
for a in A:
    temp=cc[a]*(cc[a]-1)//2
    ttemp=(cc[a]-1)*(cc[a]-2)//2
    ans=total-temp+ttemp
    print(ans)

Solution (Python):
import collections
N=int(input())
A=list(map(int,input().split()))

cc=collections.Counter(A)
L=list(cc.values())

total=0
for i in range(len(L)):
    total+=L[i]*(L[i]-1)//2

ans=0
for a in A:
    temp=cc[a]*(cc[a]-1)//2
    ttemp=(cc[a]-1)*(cc[a]-2)//2
    ans=total-temp+ttemp
    print(ans)

Solution (Python):
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
C = Counter(A)
total = 0
for v in C.values():
    total += v*(v-1)//2

for a in A:
    x = C[a]*(C[a]-1)//2
    ans = total - x
    ans += (C[a]-1)*(C[a]-2)//2
    print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
import collections
a = collections.Counter(A)

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
ans = 0
for i in a:
    if  a[i] > 1:
        ans += combinations_count(a[i],2) 
for i in A:
    n = a[i]
    if n > 2:
        print(ans+combinations_count(n-1,2)-combinations_count(n,2))
    elif n == 2:
        print(ans-1)
    else:
        print(ans)

Solution (Python):
n = int(input())
a =list(map(int,input().split()))
d = [0]*(n+1)
answer = 0
for i in range(n):
  d[a[i]]+=1
for j in range(n+1):
  answer+= (d[j]*(d[j]-1))//2
for k in range(n):
  print(answer+1-(d[a[k]]))

Solution (Python):
from collections import Counter


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

c=Counter(A)
res=[]
for p,r in c.items():
    res.append((r*(r-1))//2)
sum_res=sum(res)
for k in range(N):
    r=c[A[k]]
    print(sum_res-(r*(r-1))//2+((r-1)*(r-2))//2)

Solution (Python):
N = int(input())
nums = list(map(int, input().split()))
counts = [0 for _ in range(N+1)]
for num in nums:
  counts[num] += 1
combination = 0
for count in counts:
  if count >= 2:
    combination += (count*(count-1))//2
for num in nums:
  print(combination-counts[num]+1)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
d = {}
for a in A:
    if a in d:
        d[a] += 1
    else:
        d[a] = 1

s = 0
for k in d:
    s += d[k] * (d[k] - 1) // 2

for i in range(N):
    t = d[A[i]]
    
    print(s - t * (t - 1) // 2 + (t - 1) * (t - 2) // 2)

Solution (Python):
from collections import Counter
from scipy.special import comb

s = input()
A = list(map(int, input().split()))

d = Counter(A)

sum_c = 0
for v in d.values():
    if v >= 2:
        sum_c += comb(v, 2, exact=True)

for a in A:
    x = sum_c - comb(d[a], 2, exact=True) + comb(d[a]-1, 2, exact=True)
    print(x)

Solution (Python):
def main():
    import math
    n = int(input())
    inlis = list(map(int, input().split()))
    indic = dict()
    total = 0

    for num in inlis:
        if num in indic:
            indic[num] += 1
        else:
            indic[num] = 1
    
    for k, v in indic.items():
        total += int(v * (v-1) / 2)
    
    for num in inlis:
        print(total - indic[num] + 1)





if __name__ == "__main__":
    main()

Solution (Python):
N=int(input())
s=0
A=list(map(int,input().split()))
li=[0]*(2*(10**5)+1)
for i in range(N):
    li[A[i]]+=1
for j in range(N+1):
    s+=li[j]*(li[j]-1)//2
for k in range(N):
    print(s-li[A[k]]+1)

Solution (Python):
def nC2(n):
    return n * (n - 1) // 2


def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    cnt_list = [0] * n  

    for a in a_list:
        cnt_list[a - 1] += 1

    total_method = 0
    for i in range(n):
        total_method += nC2(cnt_list[i])

    for k in range(n):
        ans = total_method
        num_k = cnt_list[a_list[k] - 1]  
        ans -= nC2(num_k)
        num_k -= 1
        ans += nC2(num_k)
        print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    num_list = [0] * n  
    method_list = [0] * n  

    for a in a_list:
        num_list[a - 1] += 1

    for i in range(n):
        b = num_list[i]
        if b >= 2:
            method_list[i] = b * (b - 1) // 2

    total_method = sum(method_list)

    for k in range(n):
        c = a_list[k]  
        ans = total_method - method_list[c - 1]
        d = num_list[c - 1]
        if d >= 3:
            e = (d - 1) * (d - 2) // 2
        else:
            e = 0
        ans += e
        print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import collections
n=int(input())
A=list(map(int,input().split()))

c = collections.Counter(A)
all_combi=0

for i in c:
    
    all_combi +=  c[i]*(c[i]-1)//2

count = [c[A[i]] for i in range(len(A))]

for i in range(len(A)):
    

    n_same_i = count[i]-1 
    ans = all_combi - n_same_i
    
    print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
S = [0] * (N+1)
total = 0
 
for i in A:
  S[i] =S[i]+ 1
for i in S:
    total = i * (i - 1) // 2 + total
for i in A:
    
    a=total-S[i]+1
    print(a)

Solution (Python):

"""
Created on Mon Sep  7 22:04:16 2020

@author: liang
"""

N = int(input())
A = [int(x) for x in input().split()]
d = [0]*N

for a in A:
    d[a-1] += 1

score = 0
for  a in d:
    score += a*(a-1)//2

for a in A:
    print(score - d[a-1] + 1)

Solution (Python):
n=int(input())
arr=list(map(int,input().split()))
tt=[0]*(n+7)

total=0

for a in arr:
  tt[a]+=1

for t in tt:
  total += (t*(t-1)//2)

for i in range(n):
  n = tt[arr[i]]
  d = (n*(n-1)//2) - ((n-1)*(n-2)//2)
  print(total-d)

Solution (Python):

"""
Created on Mon Sep  7 22:04:16 2020

@author: liang
"""

N = int(input())
A = [int(x) for x in input().split()]
d = [0]*N

for a in A:
    d[a-1] += 1

score = 0
for  a in d:
    score += a*(a-1)//2

for a in A:
    print(score - d[a-1] + 1)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))

from collections import Counter
counter = Counter(A)

from collections import defaultdict
res_dic = defaultdict(int)

base = 0
for key,n in counter.items():
  res_dic[key] = (n * (n - 1) // 2, (n - 1) * (n - 2) // 2)
  base += n * (n - 1) // 2
  
for i in range(N):
  ans = base - res_dic[A[i]][0] + res_dic[A[i]][1]
  print(ans)

Solution (Python):
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right 
from heapq import heapify, heappop, heappush
from math import *

sys.setrecursionlimit(10**6)
INF = 10**20
MOD = 10**9+7

def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)

N,A = int(input()),lint()
S = dict()
for a in A:
    S.setdefault(a,0)
    S[a] += 1
ans = 0
for k in S:
    ans += S[k]*(S[k]-1)//2
for a in A:
    print(ans-(S[a]-1))

Solution (Python):
def f():
  n = int(input())
  *li,=map(int, input().split())
  from collections import Counter
  c = Counter()
  for i in li:
    c[i] += 1
  ans=0
  from math import comb
  for i in c.values():
    ans += comb(i, 2)
  for i in li:
    print(ans - comb(c[i], 2) + comb(c[i]-1, 2))
f()

Solution (Python):
n = int(input())
*li,=map(int, input().split())
from collections import Counter
c = Counter()
for i in li:
    c[i] += 1
ans=0
from math import comb
for i in c.values():
    ans += comb(i, 2)
for i in li:
    print(ans - comb(c[i], 2) + comb(c[i]-1, 2))

Solution (Python):
from collections import Counter

n = int(input())
def kC2(k):
  return k * (k - 1) // 2
A = list(map(int, input().split()))
AC = Counter(A)
total = sum(kC2(v) for v in AC.values())
print(*[total - kC2(AC[a]) + kC2(AC[a] - 1) for a in A], sep='\n')

Solution (Python):
def main():
	N = int(input())
	A = [int(a) - 1 for a in input().split(" ")]
	int_cnt = [0] * N
	for a in A:
		int_cnt[a] += 1
	S = sum([combination_2(c) for c in int_cnt])
	for i in range(N):
		x = int_cnt[A[i]]
		print(S - combination_2(x) + combination_2(x - 1))

def combination_2(n):
	return int(n * (n - 1) / 2)

main()

Solution (Python):
n = int(input())
a = list(map(int,input().split()))

s = [0]*(n+1)
for x in a:
    s[x] += 1
    

p = 0
for i in range(1,n+1):
    if s[i] >= 2:
        p += s[i]*(s[i]-1)//2

for i in range(n):
    ans = p - s[a[i]]*(s[a[i]]-1)//2 + (s[a[i]]-2)*(s[a[i]]-1)//2
    print(ans)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))

import collections
count = collections.Counter(a)

import math
def p(n, r):
    return math.factorial(n) // math.factorial(n - r)
def c(n, r):
    if n < r:
        return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


s = 0
cmb = {}
for i in count.keys():
    cmb[i] = (c(count[i], 2),c(count[i] - 1, 2))
    s += cmb[i][0]



for i in range(n):
    ans = s - cmb[a[i]][0] + cmb[a[i]][1]
    print(ans)

Solution (Python):
n = int(input())
lst = list(map(int,input().split()))

num = [0] * n
for i in lst:
  num[i-1] += 1
  
total = 0
for i in range(n):
  m = num[i]
  total += m*(m-1)//2

for k in range(n):
  
  s = num[lst[k]-1]
  t = total + 1- s
  
  print(t)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))

count = [0]*(N+1)

for i in range(N):
  count[A[i]] += 1



total = 0
for j in range(1,N+1):
  cj = count[j]*(count[j]-1)//2
  
  total = total + cj

ans = total
for k in range(N):
  ans = total - (count[A[k]] - 1)
  print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

C = [0 for _ in range(N+1)]

for a in A:
  C[a] += 1

ans = 0
for c in C:
  ans += c*(c-1)//2
  
for a in A:
  c_a = C[a]
  print(ans - c_a*(c_a-1)//2 + (c_a-1)*(c_a-2)//2)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))

li = [0] * (N + 1)
total = 0

for i in A:
  li[i] += 1
for i in li[1:]:
  total += i * (i - 1) // 2

for i in A:
  j = li[i]
  print(total - (j * (j - 1) // 2) + ((j - 1) * (j - 2) // 2))

Solution (Python):
from collections import Counter
n = int(input())
a = list(map(int, input().split()))

count = Counter(a)

ans = 0
for i in count.values(): ans += i*(i-1)//2
for i in a: print(ans - (count[i]-1))

Solution (Python):
from collections import Counter
N=int(input())
A=list(map(int,input().split()))

cnt=Counter(A)
sum=0
for k,v in cnt.items():
    sum+=v*(v-1)//2

for i in range(N):
    num=cnt.get(A[i])
    if num==1:
        ans=sum
    elif num==2:
        ans=sum-1
    else:
        ans=sum-num*(num-1)//2+(num-1)*(num-2)//2

    print(ans)

Solution (Python):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
import numpy as np
from collections import Counter,defaultdict
def main():
    n, *a = map(int, read().split())
    aa = Counter(a)
    d1 = defaultdict(int)
    d2 = defaultdict(int)
    for k, v in aa.items():
        d1[k] = v * (v - 1) // 2
        v -= 1
        d2[k] = v * (v - 1) // 2
    sumd1 = sum(d1.values())
    for ae in a:
        r = sumd1 - d1[ae] + d2[ae]
        print(r)

if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter
n = int(input())
a = list(map(int, input().split()))

c = Counter(a)
d = c.copy()
k = c.keys()
for i in k:
    d[i] = c[i]*(c[i]-1) // 2
s = sum(list(d.values()))
for i in a:
    print(s - d[i] + max(0, (c[i]-1)*(c[i]-2)//2))

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
dic = {}
for i in range(n):
  dic[i+1] = 0
for ai in a:
  dic[ai] += 1
cnt = 0
for di in dic:
  cnt += dic[di] * (dic[di] - 1) // 2
for i in range(n):
  print(cnt - dic[a[i]] * (dic[a[i]] - 1) // 2 + (dic[a[i]] - 2) * (dic[a[i]] - 1) // 2)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
x=[0]*(n+1)
for i in a :
    x[i]+=1


ans=0
for i in x :
    n=i
    ans+=n*(n-1)//2

for i in a :
    n=x[i]
    ans1=ans-(n-1)
    print(ans1)

Solution (Python):
n=int(input())
x=list(map(int,input().split()))
xm=max(x)
def ra(a): 
    ll,l=[0]*xm,1
    for i in range(len(a)-1):
        if a[i]==a[i+1]:
            l+=1
        else:
            ll[a[i]]+=l
            l=1
    ll.append(l)
    return ll
b=ra(sorted(x))
d=0
for i in b:
    d+=(i*(i-1))//2
for i in range(n):
    print(d-b[x[i]]+1)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))

ls = [0] * (N + 1)
for a in A:
  ls[a] += 1

C = 0
for i in set(A):
  n = ls[i]
  C += n * (n - 1) //2
  
for a in A:
  n = ls[a]
  print(C - (n - 1))

Solution (Python):
n=int(input())
A=[int(i) for i in input().split()]

L=[0 for i in range(n+1)]
for num in A:
    L[num]+=1

big=0
for num in L:
    big+=num*(num-1)//2

for num in A:
    sma=L[num]
    print(big-sma*(sma-1)//2+(sma-1)*(sma-2)//2)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
s = [0] * (N + 1)
for i in range(N):
    s[A[i]] += 1

def nC2(n):
    return n*(n-1)//2

ss = list(map(nC2, s))
sss = sum(ss)

for i in range(N):
    print(sss - s[A[i]] + 1)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
l=[0]*(n+1)
l2=[0]*(n+1)
for i in a:
    l[i]+=1

for i in range(1,n+1):
    l2[i]=l[i]*(l[i]-1)//2
sum_l=sum(l2)
for i in range(1,n+1):
    print(sum_l-(l[a[i-1]]-1))

Solution (Python):
from collections import Counter

N = int(input())
A = [int(i) for i in input().split()]
cnt_list = Counter(A)

total = sum([cnt*(cnt-1)//2 if cnt > 1 else 0 for cnt in cnt_list.values()])
print(*[total-cnt_list[a]+1 for a in A], sep='\n')

Solution (Python):
from collections import Counter

def solve():
    N = int(input())
    A = list(map(int, input().split()))
    counter = Counter(A)
    comb_2 = lambda n:n*(n-1)//2 if n > 1 else 0
    A_comb = sum(comb_2(v) for v in counter.values())
    
    
    for a in A:            
        q = counter[a]
        ret = A_comb -comb_2(q) + comb_2(q-1)
        
        print(ret, flush=True)
        
solve()

Solution (Python):
import copy
N = int(input())
A = list(map(int, input().split()))
A_sum = 0
A2 = []
idx = dict()

for i in A:
  if i in idx:
    idx[i] += 1
  else:
    idx[i] = 1
    
for key in idx:
  if idx[key] == 1:
    idx[key] = [idx[key], 0]
  else:
    temp = int(idx[key] * (idx[key] - 1)/2)
    A_sum += temp
    temp = temp - int(temp * (idx[key] - 2) / idx[key])
    idx[key] = [idx[key], temp]
  
for i in range(N):
  key = A[i]
  print(A_sum - idx[key][1])

Solution (Python):
import collections

def main():
    N = int(input())
    A = list(map(int, input().split()))
    c = collections.Counter(A)
    comb_dict = {}
    for num, freq in c.items():
        comb_dict[num] = freq * (freq-1) // 2
    comb_sum = sum(comb_dict.values())

    ans_dict = {}
    for a in A:
        if a not in ans_dict:
            ans_dict[a] = comb_sum - comb_dict[a] + (c[a] - 1) * (c[a] - 2) // 2
        print(ans_dict[a])


if __name__ == '__main__':
    main()