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):
import collections
import math
 
def combinations_count(n, r):
  if n == r:
    return 1
  elif n < r:
    return 0
  else:
    return n * (n-1) // 2 
    

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

combi = collections.Counter(A)
combi_count = {}
calc_dict = {}
total = 0
for k, v in combi.items():
  if v in calc_dict:
    combi_count[k] = calc_dict[v]
  else:
    combi_count[k] = combinations_count(v, 2)
    calc_dict[v] = combi_count[k]
  total += combi_count[k]

for i in range(N):
  ans = total - combi_count[A[i]] + combinations_count(combi[A[i]]-1, 2)
  print(ans)

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

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

for a in aa:
    print(int(total - dic[a] + 1))

Solution (Python):

from itertools import combinations
from collections import Counter
import math

N = int(input().strip())
A_list = list(map(int, input().rstrip().split()))


A_cnt = Counter(A_list)

A_sum_of_combination={}
A_sum_of_combination_minus1={}
for k,v in A_cnt.items():
    
    if v >= 2:
        tmp_sum=1
        for i in range(2):
            tmp_sum *= (v-i)/(i+1)
        
        A_sum_of_combination[k] = int(tmp_sum)
        
    else:
        A_sum_of_combination[k] = 0
    
    if (v-1) >= 2:
        tmp_sum=1
        for i in range(2):
            tmp_sum *= ((v-1)-i)/(i+1)
        
        A_sum_of_combination_minus1[k] = int(tmp_sum)
        
    else:
        A_sum_of_combination_minus1[k] = 0

sum_all = sum( [ v for k,v in A_sum_of_combination.items()] )

for i in A_list:
    num_selected = A_cnt[i] - 1
    
    if num_selected >= 2:
        ans = sum_all - A_sum_of_combination[i] + A_sum_of_combination_minus1[i]
        print(ans)
    else:
        ans = sum_all - A_sum_of_combination[i]
        print(ans)

Solution (Python):
import collections
N = int(input())
A_list = list(map(int, input().split(" ")))
C_dic = collections.Counter(A_list)
ans_dic = {}
sum_cdic = 0
temp_sum = 0
for i in C_dic:
    temp = C_dic[i]
    temp = temp*(temp-1)//2
    temp_sum += temp
sum_cdic = temp_sum

for i in A_list:
    if i in ans_dic:
        print(ans_dic[i])
    else:
        temp_sum = sum_cdic
        temp_sum -= (C_dic[i]*(C_dic[i]-1)//2)
        temp_sum += ((C_dic[i]-1)*(C_dic[i]-2)//2)
        ans_dic[i] = temp_sum
        print(temp_sum)

Solution (Python):

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)


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(N, A)

if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter as C
n=int(input())
a=list(map(int,input().split()))
b=C(a)
s=sum(i*(i-1)//2for i in b.values())
t={}
for i,j in b.items():
    t[i]=s-(j*(j-1)//2-(j-1)*(j-2)//2)
for i in a:
    print(t[i])

Solution (Python):
import math
N = int(input())
A = list(map(int,input().split()))
C = [0]*(N+1)
s=0
ds = [0]*N
def c(n,r):
    if(n<r): return 0
    return math.factorial(n)//(r*math.factorial(n-r))

for i in range(N):
    C[A[i]-1] += 1
for i in range(N):
    s +=c(C[i],2)
for i in range(N):
   print(s-C[A[i]-1]+1)

Solution (Python):
N = int(input())
A = list(map(int, input().split(" ")))
ball_count = []
for i in range(0, N):
	ball_count.append(0)

for i in A:
	ball_count[i - 1] += 1

all_pair = 0
for i in ball_count:
	all_pair += i * (i - 1) // 2

for i in A:
	print(all_pair - (ball_count[i - 1] - 1))

Solution (Python):
import collections
import math

a= int(input())
l=list(map(int, input().split()))

c = collections.Counter(l)

all_ans = 0
for n in c.values():
    if n<=1:
        continue
    all_ans+=math.factorial(n) // (math.factorial(n - 2) * math.factorial(2))
for n in l:
    print(all_ans-(c[n]-1))

Solution (Python):
import collections

N = int(input())
l = [s for s in input().split()]

c = collections.Counter(l)
ans_dict = {}

hoge = list(c.values())
full_fuga = [(i*(i-1))/2 if i >= 2 else 0 for i in hoge]
full_ans = sum(full_fuga)

for i in l:
    v = c[str(i)]
    if v < 2:
        print(int(full_ans))
    elif v == 2:
        print(int(full_ans - 1))
    elif v > 2:
        print(int(full_ans - ((v*(v-1))/2) + (((v-1) *(v-2))/2)))

Solution (Python):
import collections
n=int(input())
a = list(map(int,input().split()))
b = collections.Counter(a)
c = list()
for i in range(1,n+1):
    c.append(int((b[i]*(b[i]-1))/2))

sum = sum(c)
for i in range(n):
    d = int(a[i]-1)
    if c[d]>2:
        e = sum - c[d] + int( (c[d])*(b[d+1]-2) /(b[d+1]) )
    else:
        e = sum - c[d]
    print(e)

Solution (Python):
import collections

n=int(input())
li = list(map(int,input().split()))
cli = collections.Counter(li)
cli_values = tuple(cli.values())

count = 0

for cli_value in cli_values:
    if cli_value > 1:
        count += (cli_value * (cli_value-1))//2

for k in range(n):
    tmp = cli[li[k]]
    answer = count - (tmp * (tmp-1))//2 + ((tmp-1) * (tmp-2))//2
    print(answer)

Solution (Python):
import collections as cl
import numpy as np
n=int(input())
a=list(map(int,input().split()))
b=cl.Counter(a)
a_=np.array(list(b.values()))
c=np.sum(a_*(a_-1)/2)
for k in range(n):
    x=b[a[k]]
    if x==2:
        print(int(c-1))
    elif x==1:
        print(int(c))
    else:
        print(int(c-(x-1)))

Solution (Python):
import collections
import math

def func(n):
    ans = math.factorial(n)//((math.factorial(n-2))*math.factorial(2))
    return ans

N = int(input())
a = input().split()
ans = dict(collections.Counter(a))
sums = 0
for i in ans.values():
        if i < 2:
            continue
        else:
            sums += func(i)
            
for j in range(N):  
    print(sums-(ans[a[j]]-1))

Solution (Python):
from collections import Counter

N = int(input())
A = [int(x) for x in input().split()]
C = Counter(A)
ans = {}
cal = sum([b*(b-1)//2 for b in C.values()])
for a, b in C.items():
    ans[a] = cal - (b-1)
for a in A:
    print(ans[a])

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

A_num = collections.Counter(A)
values, counts = zip(*A_num.most_common())

ans = 0

for i in values :
  num = int(A_num[i])
  ans += num * (num - 1 ) // 2

for j in A :
  print(ans - int(A_num[j]) + 1 )

Solution (Python):
from scipy.misc import comb

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

dic = dict()
memo = [-1] * (N+1)
ans_vec = [-1] * (N+1) 

for a in A:
    if a not in dic:
        dic[a] = 1
    else:
        dic[a] += 1

provisional = 0
for d in dic.values():
    if d >= 2:
        memo[d] = comb(d,2,exact=True)  
        memo[d-1] = comb(d-1,2,exact=True)   
        provisional += memo[d] 

for a in A:
    ans = 0
    t = dic[a]
    if ans_vec[t] != -1:
        print(ans_vec[t])
    else:
        if t < 2:
            print(provisional)
        else:
            ans = provisional - memo[t] 
            if t-1 >= 2:
                ans += memo[t-1]
            ans_vec[t] = ans
            print(ans)

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

C = collections.Counter(A)

calc = {}
for i,cnt in C.items():
    calc[i] = cnt*(cnt-1)//2

calc_sum = sum(calc.values())
for i in range(N):
    ans = calc_sum - calc[A[i]]


    if calc[A[i]] != 0:
        ans += (C[A[i]]-1)*(C[A[i]]-2)//2


    print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)


def main():
    n = int(input())
    a = list(map(str, input().split()))

    d = dict()
    for i in range(n):
        if a[i] in d:
            d[a[i]] = d[a[i]] + 1
        else:
            d[a[i]] = 1

    total = 0
    normal = dict()
    target = dict()
    for k, v in d.items():
        if v == 1:
            normal[k] = 0
            target[k] = 0
        else:
            normal[k] = (v-1) * ((v-1) + 1) // 2
            target[k] = normal[k] - (v-1)
            total += normal[k]

    for i in range(n):
        print(total - normal[a[i]] + target[a[i]])


main()

Solution (Python):
s = input()
n = int(s)
s2 = input().split()
k2 = [int(el) for el in s2]
a_dic = {}
cnt_dic = {}
for i in range(len(k2)):
  key = k2[i]
  if key in a_dic:
    a_dic[key] = a_dic[key] + 1
  else:
    a_dic[key] = 1

cnt = 0
for el in a_dic.items():
  if el[1] > 1:
    wk = (el[1] * (el[1] - 1 )) /2
    cnt += wk
    cnt_dic[el[0]] = wk
  else:
    cnt_dic[el[0]] = 0
    
for k in range(len(k2)):
  k_cnt = cnt_dic[k2[k]]
  wk2 = 0
  if k_cnt > 0:
    nn = a_dic[k2[k]] - 1
    wk2 = (nn * (nn - 1 )) /2
  print( int(cnt - cnt_dic[k2[k]] + wk2))

Solution (Python):


import sys
import math
import collections
import itertools
from inspect import currentframe
INF = 10 ** 10
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def gcd(x, y) : return y if x % y == 0 else gcd(y, x % y)
def lcm(x, y) : return (x * y) // gcd(x, y)
def I() : return int(input())
def MI() : return map(int, input().split())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def chkprint(*args) : names = {id(v):k for k,v in currentframe().f_back.f_locals.items()}; print(', '.join(names.get(id(arg),'???')+' = '+repr(arg) for arg in args))

def comb(x):
    return int(x * (x-1)/2)

N = I()
A = LI()

c = collections.Counter(A)

tmp = 0
for i in c.values():
    tmp += comb(i)

for a in A:
    print(tmp - comb(c[a]) + comb(c[a]-1))

Solution (Python):
import copy

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

B = copy.deepcopy(A)
B.sort()



C = [-1 for i in range(N)]
D = [-1 for i in range(N)]

n = -1
p = -1
for i in range(N):
    if n != B[i]:
        p+=1
        if (p > 0 and D[p-1] == 1): p-=1
        C[p] = B[i]
        D[p] = 1
        n = B[i]
    else:
        D[p] += 1
if D[p] == 1:
    C[p] = -1
    D[p] = -1
    p -= 1



p += 1

E = [-1 for i in range(p)]
F = [-1 for i in range(p)]

sum = 0
for i in range(p):
    E[i] = D[i]*(D[i]-1)/2
    sum += E[i]
    F[i] = E[i] - (D[i]-1)*(D[i]-2)/2




def binsearch(k, s, e):
    
    if s == e or s + 1 == e:
        if C[s] == k:
            return s
        elif C[e] == k:
            return e
        else:
            return -1
    elif (C[(s+e)/2] < k):
        return binsearch(k, (s+e)/2, e)
    else:
        return binsearch(k, s, (s+e)/2)

for i in range(N):
    pos = binsearch(A[i], 0, p-1)
    
    if pos >= 0:
        print sum - F[pos]
    else:
        print sum

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

ct = [0] * (2*10**5+1)
ans = [0] * (n+1)
ld = [0] * (n+1)

for i in range(len(la)):
    ct[la[i]] += 1

sa = set(la)

for j in sa:
    ans[j] = ct[j] * (ct[j]-1) // 2
    ld[j] = ans[j] - ((ct[j]-2) * (ct[j]-1) // 2)

total = sum(ans)




for k in range(n):
    knum = la[k]
    print(total - ld[knum])

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
count = [0] * N
for i in range(N):
  count[A[i] - 1]  += 1
  
nanimo = 0 
for i in range(N):
  nanimo += count[i] * (count[i] - 1) // 2
for i in range(N):
  tekito = count[A[i] - 1]
  if tekito == 2:
    s = nanimo - 1
  elif tekito == 1:
    s = nanimo
  else:
    s = nanimo -  ((tekito * (tekito - 1) //2) - ((tekito - 1) * (tekito - 2) //2))
  print(s)

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

m = [0] * (N + 1)
for i in range(N):
    m[A[i]] += 1
ans = 0
for i in range(len(m)):
    ans += (m[i] * (m[i] - 1) // 2)
for i in range(N):
    print(ans - m[A[i]] + 1)

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

dp = [int(n*(n-1)/2) for n in range(N+1)]
cnt_org = [0]*(N+1)

for a in A:
  key = a
  cnt_org[a] += 1 

base = sum([dp[c] for c in cnt_org])

for k in range(N):
  diff = dp[cnt_org[A[k]]] - dp[cnt_org[A[k]]-1]
  print(base - diff)

Solution (Python):
from operator import mul
from functools import reduce
import collections
n=int(input())
A=list(map(int,input().split()))

def comb(n,r):
    r = min(n-r,r)
    if n<=1: return 0
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under
  
c=collections.Counter(A)
combs,total=[0]*n,0
for a in set(A):
  tmp=comb(c[a],2)
  combs[a-1]=tmp
  total+=tmp
for a in A:
  print(total-combs[a-1]+comb(c[a]-1,2))

Solution (Python):
import collections

N = int(input())

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

c = collections.Counter(A)


ans = 0



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

for k in range(N):
    Ak = A[k]
    cAk = c[Ak]
    ansk = ans - cAk + 1
    
    
    print(ansk)

Solution (Python):


import sys
sys.setrecursionlimit(10 ** 6)
memo = [1, 1]
def fact(n):
	if len(memo) > n:
		return memo[n]
	else:
		for i in range(len(memo), n+1):
			memo.append(memo[i-1] * i)
		return memo[n]
def nCr(n, r):
	a = 1
	for i in range(r):
		a *= n - i
	return a //fact(r)
def main():
	n = int(sys.stdin.readline())
	a = list(map(int, sys.stdin.readline().split()))
	cnt = {}
	sumcase = 0
	for i in range(n):
		cnt[a[i]] = 0
	for i in range(n):
		cnt[a[i]] += 1
	
	for ai in cnt:
		if cnt[ai] >= 2:
			sumcase += nCr(cnt[ai], 2)
	for i in range(n):
		if cnt[a[i]] == 2:
			print(sumcase - nCr(cnt[a[i]], 2))
		elif cnt[a[i]] > 2:
			
			print(sumcase - nCr(cnt[a[i]], 2) + nCr(cnt[a[i]]-1, 2))
		else:
			print(sumcase)
main()

Solution (Python):
import collections
n = int(input())
*a, = map(int, input().split())
counter = collections.Counter(a)
d = {key: (value * (value - 1)) // 2 for key, value in counter.items()}

all_sum = sum(d.values())


sum_d = {}
for k in range(n):
    if a[k] not in sum_d:
        if d[a[k]] > 0:
            tmp = ((counter[a[k]] - 1) * (counter[a[k]] - 2)) // 2
            sum_d[a[k]] = all_sum  - d[a[k]] + tmp
            print(sum_d[a[k]])
        else:
            print(all_sum)
    else:
        print(sum_d[a[k]])

Solution (Python):
import collections

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


ndic = collections.Counter(A)

mem = {}
tcmb = 0
for j in ndic:
    if ndic[j] == 1:
        continue
    
    if ndic[j] in mem:
        tcmb += mem[ndic[j]]
    else:
        mem[ndic[j]] = int((ndic[j] * (ndic[j] -1)) / 2)
        tcmb += mem[ndic[j]]
        
for i in A:
    cmb  = tcmb - (ndic[i] - 1)
    print(cmb)

Solution (Python):
import collections

n, *l = map(int, open(0).read().split())

c = collections.Counter(l)

values = list(c.values())

cnt = 0
for num in values:
    cnt += num * (num - 1) // 2
    
for num in l:
    print(cnt - (c[num] - 1))

Solution (Python):
import collections
N=int(input())
a=0
list1 = list(map(int, input().split()))
c = collections.Counter(list1)
for i in range(N):
    a+=int(c[i+1]*(c[i+1]-1)/2)
for i in range(N):
    if c[list1[i]]!=0:
        print(a-c[list1[i]]+1)
    else:
        print(a)

Solution (Python):
import collections
from functools import lru_cache
from scipy.misc import comb


results = {}


@lru_cache(maxsize=None)
def get_combination(n):
    if n < 2:
        return 0
    else:
        return comb(n, 2, exact=True)


n = int(input())
A = list(map(int, input().split()))
c = collections.Counter(A)
S = 0
for k, v in c.items():
    S += get_combination(v)
for a in A:
    if results.get(a):
        print(results[a])
    else:
        results[a] = S - get_combination(c[a]) + get_combination(c[a] - 1)
        print(results[a])

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

c = collections.Counter(an)
allcount = 0

for i in c.values():
    allcount += i*(i-1)//2

for i in an:
    print(allcount - (c[i]-1))

Solution (Python):
from collections import Counter

mod = (10 ** 9 + 7) ** 5
MAX = 2 * 10 ** 5 + 1 

def com_init(MAX, mod):
    fac = [0] * MAX
    finv = [0] * MAX
    inv = [0] * MAX
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, MAX):
        fac[i] = fac[i - 1] * i % mod
        inv[i] = mod - inv[mod % i] * (mod // i) % mod
        finv[i] = finv[i - 1] * inv[i] % mod
    return fac, finv

fac, finv = com_init(MAX, mod)

def nCr(n, r):
    if n < r:
        return 0
    elif n < 0 or r < 0:
        return 0
    return fac[n] * (finv[r] * finv[n - r] % mod) % mod

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

counter = Counter(A)

mapping = {}
for k, v in counter.items():
    mapping[k] = nCr(v, 2)

all_sum = 0
for v in mapping.values():
    all_sum += v

memo = {}
for i in range(n):
    num = A[i]
    if num in memo:
        print(memo[num])
    else:
        t = counter[num]
        ans = all_sum - nCr(t, 2) + nCr(t - 1, 2)
        print(ans)
        memo[num] = ans

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

cnt = {}
for i in a:
  cnt[i] = cnt.get(i, 0) + 1
ans = 0
for i in cnt.values():
  ans += i * (i - 1) // 2
for i in a:
  
  print(ans - cnt[i] + 1)

Solution (Python):

n = int(input())
numlist = list(map(int, input().split()))
dic = {}
for num in numlist:
  if num not in dic:
    dic[num] = 1
  else:
    dic[num] += 1
    
tmpsum = 0
for value in dic.values():
  tmpsum += int(value*(value-1)/2)
ansdic = {}
for key in dic.keys():
  n = dic[key]
  ansdic[key] = tmpsum - n + 1      
    
for num in numlist:
  print(ansdic[num])

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

for j in range(n):
    print((nsum+1-b[a[j]]))

Solution (Python):


from functools import lru_cache


@lru_cache(maxsize=1000)
def combinations_count(n, r):
    if n < 2:
        return 0
    return n*(n-1)//2


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


d = {}
cnt = 0
for i in range(N):
    d[A[i]] = d.get(A[i], 0)+1

sum1 = 0
for key, value in enumerate(d):
    
    sum1 = sum1+combinations_count(d.get(value, 0), 2)

for l in range(N):

    
    
    sum = 0
    sum = sum1-combinations_count(d.get(A[cnt], 0), 2)
    d[A[cnt]] = d.get(A[cnt], 0)-1
    sum = sum+combinations_count(d.get(A[cnt], 0), 2)
    
    
    
    print(sum)
    d[A[cnt]] = d.get(A[cnt], 0)+1
    
    cnt += 1

Solution (Python):
import sys


nCr = {}
def cmb2(n, r):
    if r == 0 or r == n: return 1
    if r == 1: return n
    if (n,r) in nCr: return nCr[(n,r)]
    nCr[(n,r)] = cmb(n-1,r) + cmb(n-1,r-1)
    return nCr[(n,r)]

def cmb(n, r):
    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 = [i for i in map(int, input().split())]

cnt = {}
for i in a:
    if i not in cnt.keys():
        cnt[i] = 1
    else:
        cnt[i] += 1

total = 0

for i in cnt.values():
    if i >= 2:
        total += cmb(i, 2)

ansDic = {}

for i in range(n):
    if a[i] in ansDic.keys():
        print(ansDic[a[i]])
    else:
        x = cnt[a[i]]
        ans = total
        if x >= 2:
            ans = total - cmb(x, 2)
        if x-1 >= 2:
            ans += cmb(x-1, 2)
        ansDic[a[i]] = ans
        print(ans)

Solution (Python):
import collections
c = {0: 0, 1: 0, 2: 1}
n = int(input())
sum = 0
for i in range(3, n + 1):
    c[i] = c[i - 1] * i // (i - 2)
cc = collections.defaultdict(int)
a = list(map(int, input().split()))
for i in a:
    cc[i] += 1
for k, v in cc.items():
    sum += c[v]



for i in range(n):
    v = cc[a[i]]
    
    
    print (sum - c[v] + c[v - 1])

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

aa = [0 for i in range(2*10**5)]
for i in range(n):
  aa[a[i]] += 1
 
ans = 0
for key in range(2*10**5):
  ans += aa[key]*(aa[key]-1)//2
    
for i in range(n):
  tmp1 = aa[a[i]]*(aa[a[i]]-1)//2
  aa[a[i]] -= 1
  tmp2 = aa[a[i]]*(aa[a[i]]-1)//2
  aa[a[i]] += 1
  print(ans-tmp1 + tmp2)

Solution (Python):
import collections
import math

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

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

c = collections.Counter(A)


d = {}
csum = 0
for i,j in c.items():
    if j == 1:
        d[i] = 0
        csum += 0
    else:
        d[i] =  combinations_count2(j)
        csum += d[i]






for i in A:
    
    if c[i] == 1:
        print(csum)
    elif c[i] == 2:
        print(csum-1)
    else:
        print(csum - combinations_count2(c[i]) + combinations_count2(c[i]-1))

Solution (Python):
import collections
import numpy as np

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

c=collections.Counter(A)
kazu=list(c.values())
a=np.array(kazu)
sn=a*(a-1)/2
s=sum(sn)
for k in range(N):
    sub=c[A[k]]-1
    print(int(s-sub))

Solution (Python):

import copy, math, collections

n = int(input())
a = input().split()

result = []

num_dict = dict(collections.Counter(a))

def calc(x):
    if x >= 2:
        return math.factorial(x) / (math.factorial(x - 2) * 2)
    else:
        return 0

result_dict = {}
result_minus1_dict = {}

for key, value in num_dict.items():
    result_dict[key] = calc(num_dict[key])
    result_minus1_dict[key] = calc(num_dict[key] - 1)

result_all = sum(result_dict.values())

for i in range(len(a)):
    print(int(result_all - result_dict[a[i]] + result_minus1_dict[a[i]]))

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
count = [0 for i in range(n+1)]
ans = 0
for i in range(n):
    count[a[i]] += 1
for i in range(n+1):
    ans += count[i]*(count[i]-1)//2
for i in range(n):
    num = count[a[i]]
    p = ans-(num*(num-1)//2)+((num-1)*(num-2)//2)
    print(p)

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

N = int(input())
A = input().split()
a = Counter(A)

ans_all = 0
for x in a.keys():
    m = a[x]
    ans_all += m * (m - 1) // 2

for x in A:
    m = a[x] - 1
    print(ans_all - m)

Solution (Python):
N = int(input())
A_list = list(map(int, input().split()))
cnt_list = [0 for i in range(N)]
sum_res = 0

for i in range(N):
  cnt_list[A_list[i] - 1] += 1

for i in range(N):
  sum_res += cnt_list[i] * (cnt_list[i] - 1) // 2

for i in range(N):
  print_res = sum_res
  if cnt_list[A_list[i] - 1] >= 2:
    print_res = sum_res - cnt_list[A_list[i] - 1] * (cnt_list[A_list[i] - 1] - 1) // 2 + (cnt_list[A_list[i] - 1] - 1) * (cnt_list[A_list[i] - 1] - 2) // 2
  
  print(print_res)

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

a_dict = dict()
for x in a:
    if x not in a_dict:
        a_dict[x] = 1
    else:
        a_dict[x] += 1

sum = 0
for k, v in a_dict.items():
    sum += v*(v-1)//2
    a_dict[k] = (v*(v-1)//2)-((v-1)*(v-2)//2)

for i in a:
    print(int(sum-a_dict[i]))

Solution (Python):
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
INF = 10**30
from collections import Counter
from bisect import bisect_left

def f(i, d, dsum, n):
    return func(i, d) + dsum[i] + dsum[n] - dsum[i+1]

def func(i, w):
    return w[i][1] * max(0, w[i][1]-1) // 2

def main():
    n = int(input().strip())
    v = list(map(int, input().strip().split()))
    d = dict(Counter(v))
    w = []
    for k in sorted(d.keys()):
        w.append([k, d[k]])
    m = len(w)
    c = [0] * (m+1)
    for i in range(1, m+1):
        c[i] = c[i-1] + func(i-1, w)
        
    q = [u[0] for u in w]
    
    
    for i in v:
        g = bisect_left(q, i)
        
        
        
        w[g][1] -= 1
        print(f(g, w, c, m))
        w[g][1] += 1


if __name__ == '__main__':
    main()

Solution (Python):
a=int(input())
num=[int(i) for i in input().split()]
num_dict={}
num_min1_dict={}
sum_num=0
for i in num:
    if i not in num_dict:
        num_dict[i]=1
    else:
        num_dict[i]+=1

num_to_sum_dict={}
for key, value in num_dict.items():
    sum_num+=value*(value-1)/2

for key,value in num_dict.items():
    tmp=sum_num-value*(value-1)/2+(value-1)*(value-2)/2
    num_to_sum_dict[key]=tmp

for i in range(a):
    print(int(num_to_sum_dict[num[i]]))

Solution (Python):
import collections
import math

def combinations_count(n, r):
    if n < 2:
        return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

n = int(input())
a_list = list(map(int, input().split()))
a_col = collections.Counter(a_list)
c_dic = {}
sm = sum([c * (c - 1) for c in a_col.values()]) // 2
for a in a_list:
    if a in c_dic:
        print(c_dic[a])
    else:
        ans = sm - combinations_count(a_col[a], 2) + combinations_count(a_col[a] - 1, 2)
        print(ans)
        c_dic[a] = ans

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
count = [0] * N
for i in range(N):
  count[A[i] - 1]  += 1
  
nanimo = 0 
for i in range(N):
  nanimo += count[i] * (count[i] - 1) // 2
for i in range(N):
  tekito = count[A[i] - 1]
  if tekito == 2:
    s = nanimo - 1
  elif tekito == 1:
    s = nanimo
  else:
    s = nanimo -  ((tekito * (tekito - 1) //2) - ((tekito - 1) * (tekito - 2) //2))
  print(s)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
from collections import Counter
MOD=10**9+7

d=Counter(a)
tni=0
dv=d.values()
dv=list(dv)
for i in range(len(dv)):
    tni+=dv[i]*(dv[i]-1)//2

for i in range(n):
    r=d[a[i]]
    print(tni-(r-1))

Solution (Python):
from collections import Counter

n=int(input())
a=list(map(int,input().split()))
c=Counter(a)
s=0
for i in c:
    f=c[i]
    s+=(f*(f-1))//2
for i in a:
    ans=s 
    f=c[i]
    ans=ans-((f*(f-1))//2)
    ans+=((f-1)*(f-2))//2
    print(ans)

Solution (Python):
import collections

def comb(a, b):
    result = 1
    for i in range(b):
        result = result * ((a-i)/(b-i))
    return int(result)

N = int(input())
A = list(map(int, input().split()))
all_comb = 0
freq = collections.Counter(A)
for i in freq.values():
    if (i == 1) or (i == 0):
        pass
    else:
        all_comb += comb(i, 2)

for k in range(N):
    result = all_comb
    if freq[A[k]] == 1:
        pass
    else:
        tmp = comb(freq[A[k]], 2)
        result = result - tmp + tmp*((freq[A[k]]-2)/freq[A[k]])
    print(int(result))

Solution (Python):
import math

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

lib = [0] * n

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

for i in a:
    lib[i - 1] += 1

count = 0

for i in lib:
    if i >= 2:
        count += comb(i, 2)

for i in a:
    print(count - (lib[i - 1] - 1))

Solution (Python):
from math import factorial
import collections

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

MEMO = {}
def fact(n):
    if MEMO.get(n):
        return MEMO[n]
    r = 2
    if n - r < 0:
        MEMO[n] = 0
        return 0
    MEMO[n] = factorial(n) // factorial(r) // factorial(n - r)
    return MEMO[n]

COUNTS = collections.Counter(BALLS)
TOTAL = sum(fact(n) for n in COUNTS.values())

MEMO2 = {}
def solve(n):
    if MEMO2.get(n):
        return MEMO2[n]

    MEMO2[n] = TOTAL - fact(COUNTS[n]) + fact(COUNTS[n] - 1)
    return MEMO2[n]

for i in range(N):
    print(solve(BALLS[i]))

Solution (Python):
from scipy.misc import comb

N = int(input())
A = input().split()

B = {}
for i in range(0, N):
    if not(A[i] in B.keys()):
        B[A[i]] = 1
    else:
        B[A[i]] += 1 

S = 0
for value2 in B.values():
    S = S + comb(int(value2), 2, exact=True)

S0 = 0
for i in range(0, N):
    S0 = S - (comb(int(B[A[i]]), 2, exact=True) - comb(int(B[A[i]])-1, 2, exact=True))
    print(S0)

Solution (Python):

import collections

N = int(input())

A = list(map(int, input().split()))
count = collections.Counter(A)
d = dict(count)
total =0
for j in count.items():
    total +=(j[1])*(j[1]-1)/2
    d[j[0]] = (j[1]-1)

for i in A:
    print(int(total - d[i]))

Solution (Python):
import numpy as np

N = int(input())
mylist = np.array(list(map(int, input().split())))

mydic = {}

mysumpari = 0
for i in mylist:
    if i not in mydic:
        mydic[i] = 1
    else:
        mydic[i] += 1
        mysumpari += 1


mysum = 0
for d in mydic:
    if mydic[d] >= 2:
        a = mydic[d]
        b = mydic[d] - 1
        
        mysum +=  (a*b)//2
    
    
for i in mylist:
    if mydic[i] >= 2:
        a = mydic[i]
        b = mydic[i] - 1
        
        if b > 1:
            print(mysum - (a*b)//2 + ((a-1)*(b-1))//2)
        else:
            print(mysum - (a*b)//2)
        
        

    else:
        print(mysum)

Solution (Python):
import collections
import math

def combinations_count(n, r):
    if n==1 or n==0:
        return 0
    else:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

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

c = collections.Counter(A)

ks=list(c.keys())
vs=list(c.values())
cvs=collections.Counter(vs)
tmp_ans=0

for k,v in cvs.items():
    
    tmp_ans += v *combinations_count(k, 2)









    


for i in A:
    print(tmp_ans-(c[i]-1))

Solution (Python):
n = int(input())
a = [int(c) for c in input().split()]
c = [0 for _ in range(n)]
for i in range(n):
    c[a[i]-1]+=1
count = sum(c[i] * (c[i]-1) / 2 for i in range(n))
for i in range(n):
    pp = c[a[i]-1]
    print(int(count - pp * (pp - 1) / 2 + (pp-1) * (pp-2) / 2))