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

def check(A):
    count = 0
    for j in A:
        n = A[j]
        if n > 1:
            count += n * (n - 1) / 2
            
    return int(count)


N = int(input())
A = list(map(int, input().split(" ")))
A2 = collections.Counter(A)

Nsum = check(A2)

for i in range(0,N):
    num = A[i]
    count = A2[num]
    out = Nsum - ( count - 1 )
    print(out)

Solution (Python):
import collections

n = int(input())
a = list(map(int,input().split()))
d = collections.Counter(a)
s = sum([i*(i-1)//2 for i in d.values()])

for j in a:
    print(s-d[j]+1)

Solution (Python):
import collections
from sys import stdin






















def get_result(data):
    N = data[0][0]
    A = data[1]
    uniq_cnt = collections.Counter(A)
    all_selection = 0
    for val in uniq_cnt.values():
        all_selection += int(val * (val-1) / 2)

    for i in range(N):
        print(all_selection - uniq_cnt[A[i]] + 1)


if __name__ == '__main__':
    raw_data = [val.rstrip('\n') for val in stdin.readlines()]
    data = [list(map(int, val.split(' '))) for val in raw_data]
    get_result(data)

Solution (Python):
n=int(input())
su=0
a = list(map(int, input().split())) 
da={}
for i in range(len(a)):
    if a[i] in da:
        da[a[i]]+=1
    else:
        da[a[i]]=1
 
for p in da.keys():
    su+=da[p]*(da[p]-1)/2

for k in range(len(a)):
    sut=su-(da[a[k]]-1)
    print(int(sut))

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
nC2 = [0 for i in range(n + 1)]
for i in range(2, n + 1):
    nC2[i] = i * (i - 1) // 2
cnt = [0]*n
for i in a:
    cnt[i - 1] += 1
res = 0
for i in cnt:
    res += nC2[i]
for i in a:
    print(res - nC2[cnt[i - 1]] + nC2[cnt[i - 1] - 1])

Solution (Python):
n=int(input())
l=[int(i) for i in input().split()]
c=[0 for i in range(n)]
for i in l:
    c[i-1]+=1
res=0
for i in range(n):
    res+=c[i]*(c[i]-1)//2
for i in range(n):
    j=l[i]-1
    print(res-c[j]*(c[j]-1)//2+(c[j]-1)*(c[j]-2)//2)

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):
n=int(input())
l=list(map(int,input().split()))
d={}
ans=0
for i in l:
    if i not in d:
        d[i]=1
    else:
        d[i]+=1
for i in d.keys():
    x=d[i]*(d[i]-1)//2
    ans+=x
for i in l:
    if d[i]>2:
        print(ans-(d[i]*(d[i]-1)//2)+((d[i]-1)*(d[i]-2)//2))
    elif d[i]==2:
        print(ans-1)
    else:
        print(ans)

Solution (Python):
from collections import Counter

N = int(input())
A = list(map(int,input().split()))
a = list(set(A))
cc = Counter(A)
dup = set([x for x in a if cc[x] > 1])
c_l = [(cc[i]-1)*(cc[i])/2 for i in a]
al = sum(c_l)

for k in A:
    if k in dup:
        print(int(al-cc[k]+1))
    else :
        print(int(al))

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

sums = 0

for i in a.values():
    sums +=  i * (i - 1) // 2

for j in res:
    print(sums - a[j] * (a[j] -1) //2 + (a[j]-1) * (a[j] -2 ) // 2)

Solution (Python):
from collections import Counter

N = int(input())
A = list(map(int,input().split()))
a = list(set(A))
cc = Counter(A)
dup = set([x for x in a if cc[x] > 1])
c_l = [(cc[i]-1)*(cc[i])/2 for i in a]
al = sum(c_l)

for k in A:
    if k in dup:
        print(int(al-cc[k]+1))
    else :
        print(int(al))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
import collections
c = collections.Counter(a)
val_list=list(c.values())
v_count=map(lambda x: x * (x-1), val_list)
total_count=int(sum(v_count)/2)

for i in a:
    print(total_count-(c[i]-1))

Solution (Python):
import math
n=int(input())
A = list(map(int, input().split()))
res=[0]*n
import collections
def com(n, r):
    if n<r:
        return 0
    else:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

af=collections.Counter(A)
res2=list(af.items())
res3=[0]*n
s=0
for i in range(0,len(res2)):
    s=s+com(res2[i][1],2)
for j in range(0,len(res2)):
    res3[res2[j][0]-1]=s-res2[j][1]+1
for i in range(n):
    res[i]=res3[A[i]-1]

for j in range(n):
    print(res[j])

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

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

num = 0
for v in c.values():
  if v > 1:
      num += comb(v, 2,exact=True)
for k in range(N):
  print(int(num - (c[A[k]]-1)))

Solution (Python):
import collections

def count_defaultdict(it):
    counter = collections.defaultdict(int)
    for x in it:
        counter[x] += 1
    return dict(counter)

_ = int(input())
l = list(map(int, input().split()))
all_sum = 0
count = count_defaultdict(l)
for _, v in count.items():
    all_sum += v * (v - 1) // 2
for i in range(len(l)):
    if count[l[i]] > 1:
        v = count[l[i]]
        print(all_sum - v * (v-1) // 2 + (v-1) * (v-2)//2)
    else:
        print(all_sum)

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

n = int(input())
a = list(map(int, input().split()))
count = collections.Counter(a)
sm = {}
sm2 = {}
for key, value in count.items():
    sm[key] = value * (value - 1) // 2
    sm2[key] = (value - 1) * (value - 2) // 2
total = sum(sm.values())

for num in a:
    print(total - sm[num] + sm2[num])

Solution (Python):
import collections

def main(): 
    N = int(input())
    A = list(map(int, input().split()))
    cnt = [0]*(N+1)
    C = collections.Counter(A)
    
    for k, v in C.items():
        cnt[k] = v*(v-1)//2
    S = sum(cnt)
    
    for i in A:
        print(S - cnt[i] + (C[i]-1)*(C[i]-2)//2)
        
main()

Solution (Python):
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
c = Counter(a)
total = 0
los = [0]*n
for x, y in c.items():
    if y >= 2:
        total += y * (y-1) // 2
    if y >= 2:
        los[x-1] = y*(y-1)//2 - (y-1)*(y-2)//2

for i in range(n):
    print(total - los[a[i]-1])

Solution (Python):
n = int(input())

a = list(map(int, input().split()))
s = [0]*n
for i in range(n):
    s[a[i]-1] += 1
k = 0
for i in range(n):
    if s[i] >= 2:
        k += ((s[i]*(s[i]-1))//2)
for i in range(n):
    if s[a[i]-1] >= 2:
        print(k-(s[a[i]-1]-1))
    else:
        print(k)

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


counter = collections.Counter(a)


def combi(num):
    if num < 2:
        pair = 0
    else:
        pair = int(num * (num-1) / 2)
    return pair


total = sum([combi(v) for v in counter.values()])
for el in a:
    print(total - (counter[el] - 1))

Solution (Python):
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
re = [0] * (n+1)

c = Counter(a)   
for k, v in c.items():
    re[k] = v*(v-1) // 2

total = sum(re)
for i in a:
    print(total - re[i] + (c[i]-1)*(c[i]-2) // 2)

Solution (Python):
from collections import Counter

N = int(input())
A = list(map(int,input().split()))
a = list(set(A))
cc = Counter(A)
dup = set([x for x in a if cc[x] > 1])
c_l = [(cc[i]-1)*(cc[i])/2 for i in a]
al = sum(c_l)






[print(int(al-cc[k]+1)) if k in dup else print(int(al)) for k in A]

Solution (Python):
from collections import Counter

N = int(input())
A = list(map(int,input().split()))
a = list(set(A))
cc = Counter(A)
dup = set([x for x in a if cc[x] > 1])
c_l = [(cc[i]-1)*(cc[i])/2 for i in a]
al = sum(c_l)






[print(int(al-cc[k]+1)) if k in dup else print(int(al)) for k in A]

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())
k = list(map(int,input().split()))

c = Counter(k)

li = c.most_common()

a_li = [x for x in li if x[1] != 1]

max = 0

for i in a_li:
    num = i[1]
    max = max + cmb(num,2)
    


for i in k:
    num = c[i]
    if num == 1:
        ans = max
    elif num == 2:
        ans = max - 1
    else:
      pre = cmb(num,2)
      cur = cmb(num-1,2)
      ans = max - pre + cur
    print(ans)

Solution (Python):
import sys

input = sys.stdin.readline

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

cnt = [0] * N

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

combination = [0] * N

for a in A:
    combination[a - 1] = cnt[a - 1] * (cnt[a - 1] - 1) // 2

total = sum(combination)

for a in A:
    c = combination[a - 1]
    b = cnt[a - 1]
    ans = total - c + c * (b - 2) // b
    print(ans)

Solution (Python):


def main():
    import collections

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

    allc = collections.Counter(A)
    
    allsu = 0
    for pairs in sorted(allc.values(), reverse=True):
        allsu += pairs*(pairs-1)//2

    for a in A:
        num = allc[a]
        print(allsu - (num-1))

    
    
    
    
    
    
    
    
    
    
    
    


if __name__ == '__main__':
    main()

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

counters = {}
cevap_list = {}
sayilar = set(numbers)
for i in numbers:
    if i in counters:
        counters[i] += 1
    else:
        counters[i] = 1

sonuc = 0
for sayi in counters:
    count = counters[sayi]
    sonuc += int(count * (count - 1) / 2)


for i in range(n):
    print(sonuc-counters[numbers[i]]+1)

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

def main():
    n = int(input())
    lis = list(map(int,input().split()))
    d = Counter(lis)
    su = 0
    for e in d:
        su += (d[e] - 1) * d[e] // 2

    for e in lis:
        if d[e] <= 1:
            print(su)
        else:
            k = d[e]
            res = su - k*(k-1)//2 + (k-1)*(k-2)//2
            print(res)

if __name__ == '__main__':
    main()

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):
import collections
import math


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


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

c = collections.Counter(A)
full_ans = sum(list(map(comb, c.values())))

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

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

c=Counter(a)

al=0
for i in c.values():
    al+=(i*(i-1))//2
for i in range(n):
    print(al - (c[a[i]]-1))

Solution (Python):
sel='D'

if sel=='A':
    N,M=map(int,input().split())
    ans=0
    ans+=M*(M-1)//2
    ans+=N*(N-1)//2
    print(ans)


if sel=='B':
    def ispal(s):
        for i in range(len(s)//2+1):
            if s[i]!=s[-(i+1)]:
                return False
        return True

    S=input()
    if ispal(S) and ispal(S[:(N-1)//2]) and ispal(S[(N+3)//2-1:]):
        print('Yes')
    else:
        print('No')


if sel=='C':
    L=int(input())
    print((L**3)/27)


if sel=='D':    
    N=int(input())
    A=[int(i) for i in input().split()]
    kin=list(set(A))
    cnt={}
    for k in kin:
        cnt[k]=0
    for a in A:
        cnt[a]+=1
    SUM=0
    for k in kin:
        SUM+=cnt[k]*(cnt[k]-1)//2
    for a in A:
        if cnt[a]>=2:
            print(SUM-cnt[a]+1)
        else:
            print(SUM)
        

if sel=='E':
    a=0


if sel=='F':
    N,S=map(int,input().split())
    A=[int(i) for i in input().split()]
    
    for l in range(N):
        for r in range(l,N):
            print(tab[l][r])

Solution (Python):
import sys
import numpy as np


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    A = np.array(list(map(int, input().split())))
    count = np.bincount(A)
    
    total = (count * (count - 1)).sum() // 2
    answer = (total - (count - 1))[A]
    print("\n".join(answer.astype(str)))


main()

Solution (Python):
from collections import defaultdict


def calc(x):
    return x*(x-1)//2


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

dd = defaultdict(int)
for i in range(N):
    dd[A[i]] += 1

count = 0
for k, v in dd.items():
    if v >= 2:
        count += calc(v)
for i in range(N):
    ans = 0
    if dd[A[i]] >= 3:
        ans = count - calc(dd[A[i]]) + calc(dd[A[i]]-1)
    elif dd[A[i]] == 2:
        ans = count - 1
    elif dd[A[i]] == 1:
        ans = count
    print(ans)

Solution (Python):
from collections import defaultdict


def calc(x):
    return x*(x-1)//2


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

dd = defaultdict(int)
for i in range(N):
    dd[A[i]] += 1

count = 0
for k, v in dd.items():
    if v >= 2:
        count += calc(v)
for i in range(N):
    ans = 0
    if dd[A[i]] >= 3:
        ans = count - calc(dd[A[i]]) + calc(dd[A[i]]-1)
    elif dd[A[i]] == 2:
        ans = count - 1
    elif dd[A[i]] == 1:
        ans = count
    print(ans)

Solution (Python):
import collections

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

c = collections.Counter(A)
cou = list(c.values())

ans = 0
for j in range(len(cou)):
	m = cou[j]
	k = m*(m-1)/2
	ans += int(k)


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

Solution (Python):
from collections import Counter
def num_to_comb(val):
    return val*(val-1)//2

num_ = int(input())
ball_list = [int(i) for i in input().split()]

if len(set(ball_list)) == num_:
    for i in range(num_):
        print(0)

elif len(set(ball_list)) == 1:
    for i in range(num_):
        print((num_ - 1)*(num_ - 2) // 2)

else:
    temp = Counter(ball_list)
    ans_dict={}
    temp_ans_dict = {}
    
    for num in set(ball_list):
        if temp_ans_dict.get(temp[num], 0) == 0:
            temp_dict = temp.copy()
            temp_dict[num] = temp_dict[num]-1
            ans_dict[num] = sum(list(map(num_to_comb, temp_dict.values())))
            temp_ans_dict[temp[num]] = ans_dict[num]
        else:
            ans_dict[num] =temp_ans_dict[temp[num]]
            
    for i in ball_list:
        print(ans_dict[i])

Solution (Python):
import collections




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

counts = collections.Counter(A_list)
ans = 0
for _, v in counts.items():
    ans += v * (v - 1) / 2

for k in range(1, N + 1):
    n = counts[A_list[k - 1]]
    ans_k = ans
    if n > 1:
        ans_k -= n - 1

    print(int(ans_k))

Solution (Python):
import collections
 
def LI():
	return [ int(s) for s in input().split() ]
 
N = int(input())
A = LI()
 
c_counter = [0]*(2*(10**5))
 
for a in A:
  c_counter[a]+=1
 
total = 0
total_dict = [0]*(2*(10**5))
 
for a in range(len(c_counter)):
  v = c_counter[a]
  if v >= 2:
    comb = v*(v-1)//2
    total_dict[a] = comb
    total += comb
 
for a in A:
  if total == 0:
    print(0)
  else:
    print(total - total_dict[a] + (c_counter[a]-1)*(c_counter[a]-2)//2)

Solution (Python):
def inp():
    return input()
def iinp():
    return int(input())
def inps():
    return input().split()
def miinps():
    return map(int,input().split())
def linps():
    return list(input().split())
def lmiinps():
    return list(map(int,input().split()))
def lmiinpsf(n):
    return [list(map(int,input().split()))for _ in range(n)]

n = iinp()
q = lmiinps()

k = 1
l = 0
s = 0

a = sorted(q)
a.append(0)

b = [0]*(a[n-1])
d = [0]*(a[n-1])

for i in range(n):
    if a[i] == a[i+1]:
        k += 1
    else:
        l = ((k*(k-1))//2)
        s += l
        b[a[i]-1]=l
        d[a[i]-1]=k
        k = 1

for j in q:
    p = s-((b[j-1])-((b[j-1] *(d[j-1]-2)) // d[j-1]))
    print(p)

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

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

count = Counter(A)
sm = {}
sm2 = {}
for key, value in count.items():
    sm[key] = value*(value-1)//2
    sm2[key] = (value-1)*(value-2)//2
total = sum(sm.values())

for num in A:
    print(total - sm[num] + sm2[num])

Solution (Python):
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
re = [0] * (n+1)

c = Counter(a)

for k,v in c.items():
    re[k] = v*(v-1) // 2

s = sum(re)
for i in a:
    print(s - re[i] + (c[i]-1)*(c[i]-2) // 2)

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

from collections import defaultdict

resdict = {}

from collections import Counter

c = Counter(alist)

cdict = dict(c)





maxi = 0

for k, v in cdict.items():
    maxi += v * (v - 1) // 2

for a in alist:
    num = cdict[a]
    if num in resdict:
        
        print(resdict[num])
        continue
    
    if a in cdict:
        cdict[a] -= 1
        temp = 0
        for k,v in cdict.items():
            temp += v*(v-1)//2
        print(temp)
        resdict[num] = temp
        cdict[a] += 1
    else:
        print(maxi)

Solution (Python):
import copy
import collections
import math
 
N = int(input())
A = [int(i) for i in input().split()] 
 
maxnum = max(A)
lis = [-1] * maxnum
bigC = collections.Counter(A)
for i, a in enumerate(A):
  if lis[a-1] < 0:
    newA = copy.deepcopy(A)
    newA.pop(i)
    c = collections.Counter(newA)
    total = 0
    for idx in c:
      num = c[idx]
      total += num * (num-1) // 2
      
    lis[a-1] = total
    for x in bigC:
      if bigC[x] == bigC[a]:
        lis[x-1] = total
      
    print(total)
  else:
    print(lis[a-1])

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

ans = 0

k = [0 for _ in range(n+1)]

for i in range(n):
    k[l[i]] += 1
    
ll = len(k)
for i in range(ll):
    a = k[i]
    if a <= 1:
        pass
    else:
        ans += (a * (a - 1))/2
        
for i in range(n):
    a = k[l[i]]
    b = (a * (a - 1))/2
    c = a - 1
    if a < 1:
        print(int(ans))
    else:
        pans = ans - b
        c = (c * (c - 1))/2
        pans = pans + c
        print(int(pans))

Solution (Python):
import collections 
from collections import defaultdict
n=int(input())
a=list(map(int,input().split()))
c=defaultdict(int)
for s in a:
    c[s]+=1

a1=list(set(a))

total=0
for j in range(len(a1)):
    x=c[a1[j]]
    total+=x*(x-1)/2

for i in range(n):
    ans=total-c[a[i]]+1
    print(int(ans))

Solution (Python):
import operator as op
from functools import reduce
from collections import defaultdict
from math import ceil
def ncr(n, r):
    r = min(r, n-r)
    numer = reduce(op.mul, range(n, n-r, -1), 1)
    denom = reduce(op.mul, range(1, r+1), 1)
    return numer // denom

n = int(input().strip())
nums = [int(i) for i in input().strip().split()]
ans = [0 for i in range(n)]
num_d = defaultdict(int)
for i in nums:
    num_d[i] += 1

ans_d = defaultdict(int)
s = 0
for each_num in num_d:
    if num_d[each_num] < 2:
        ans_d[each_num] = 0
        continue
    res = ncr(num_d[each_num],2)
    s += res
    ans_d[each_num] = res

for index,i in enumerate(nums):
    if num_d[i] - 1 < 2:
        ans[index] = s - ans_d[i]
    else:
        ans[index] = s - ans_d[i] + ncr(num_d[i] - 1,2)

for each in ans:
    print(each)

Solution (Python):
import collections
import math
def combination(a, b):
    tmp = 1
    for i in range(a - b + 1, a + 1):
        tmp *= i
    return tmp // math.factorial(b)
 
N = int(input())
listA = list(map(int, input().split()))

countA = dict(collections.Counter(listA).most_common())
countA_calc = {k: v * (v -1) // 2 for (k, v) in countA.items()}
countA_calc2 = {k: (v - 1) * (v -2) // 2 for (k, v) in countA.items()}
summery = sum(countA_calc.values())
for i in listA:
    print(summery - countA_calc[i] + countA_calc2[i])

Solution (Python):
def fun(n):
    return (n*(n+1))//2
n = int(input())
a = list(map(int,input().split()))
d = {}
for i in range(len(a)):
    d[a[i]] = 0
for i in range(len(a)):
    d[a[i]] += 1
total = 0
for i in d:
    total += fun(d[i]-1)
for i in d:
    d[i] = total - (d[i] -1)
for i in range(len(a)):
    print(d[a[i]])

Solution (Python):
n=int(input())
a=[int(i) for i in input().split()]
nC2=[0 for i in range(n+1)]
for i in range(2,n+1):
    nC2[i]=i*(i-1)//2
cnt=[0 for i in range(n)]
for i in a:
    cnt[i-1]+=1
res=0
for i in cnt:
    res+=nC2[i]
for i in a:
    print(res-nC2[cnt[i-1]]+nC2[cnt[i-1]-1])

Solution (Python):
N = int(input())

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

list1 = [ 0 for a in range(N)]

for i in An:
    list1[i-1] += 1

list2 = [int((a-1)*a/2) for a in list1]
sum1 = sum(list2)


for i in An:
    if list1[i-1] != 2:
        a = sum1 - list1[i-1] + 1
        print(a)
    else:
        a = sum1 - 1
        print(a)

Solution (Python):
import collections
def cal(n):
    ans = int(n*(n-1)/2)
    return ans 
n = int(input())
a = list(map(int,input().split()))
c = collections.Counter(a)
temp = list(c.values())
t_val = list(map(cal,c.values()))
t_key = list(c.keys())
t_cal = sum(t_val)



for i in range(n):
    ans1 = c[a[i]]-1
    print(t_cal - ans1)

Solution (Python):
m = int(input())
q_list = list(map(int, input().strip().split()))
n = [0] * m
k = [0] * m

for i in q_list:
    n[i - 1] += 1
for j, l in zip(range(m), n):
    k[j] = int(l * (l - 1)/2)
sum_n = sum(k)

for j in q_list:
    print(sum_n - n[j - 1] + 1)

Solution (Python):
import collections

n=int(input())
arr=list(map(int,input().split()))
cnt=collections.Counter(arr)
total=0
for key in cnt.keys():
  total+=cnt[key]*(cnt[key]-1)//2
for i in range(n):
  tmp=total
  tmp-=cnt[arr[i]]*(cnt[arr[i]]-1)//2
  tmp+=(cnt[arr[i]]-1)*(cnt[arr[i]]-2)//2
  print(tmp)

Solution (Python):
N = int(input())
k = list(map(int, input().split()))
d = {}
dd = {}
for i in range(N):
    a = k[i]
    if a in d:
        d[a] += 1
    else:
        d[a] = 1
tot = 0
for key in d:
    a = d[key]
    dd[key] = int(a*(a-1)/2)
    tot += dd[key]
for i in range(N):
    print(tot - d[k[i]]+1)

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

cnt=[0]*1000000
for i in a:
    cnt[i]+=1
for i in cnt:
    ans+=i*(i-1)/2
for i in a:
    
    print(int(ans-cnt[i]*(cnt[i]-1)/2+(cnt[i]-1)*(cnt[i]-2)/2))

Solution (Python):
n = int(input().rstrip())
line = input().rstrip().split(' ')

a = []
d = [0] * n
for i in range(n):
    num = int(line[i])
    a.append(num)
    d[num - 1] += 1

ans_list = [0] * n
for i in range(n):
    ans_list[i] = int(d[i] * (d[i] - 1) / 2)

total = 0
for i in range(n):
    total += ans_list[i]

for i in range(n):
    ans = total

    ans -= ans_list[a[i] - 1]
    num = d[a[i] - 1] - 1
    ans += num * (num - 1) / 2

    print(int(ans))

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

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

N=int(input())
A=list(map(int,input().split(" ")))
count=Counter(A)
com={}

for k,v in count.items():
    com[k]=combo(v)
s=sum(com.values())
for i in range(N):
    su=s-combo(count[A[i]])+combo(count[A[i]]-1)
    print(su)

Solution (Python):
import collections

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

c = collections.Counter(a)

r = 0
for j in c:
    if c[j] > 1:
        r = int(r + (c[j] * (c[j] - 1) / 2))

tmp = r 
for i in range(n):
    r = int(r - ((c[a[i]] * (c[a[i]] - 1) / 2)) + (((c[a[i]] - 1) * (c[a[i]] - 2) / 2)))
    print(r)
    r = tmp

Solution (Python):
import collections

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


c = collections.Counter(a)



c_value_list = list(c.values())



sum = 0
for i in range (len(c_value_list)):
  sum += int((c_value_list[i]*(c_value_list[i]-1))/2)



for i in range(len(a)):
  if c[a[i]] == 1:
    print(sum)
  else:
    diff = int((c[a[i]] * (c[a[i]]-1)) / 2)
    diff_a = int(((c[a[i]]-1) * (c[a[i]]-2)) / 2)
    print(sum - diff + diff_a)

Solution (Python):
import math

def combinations_count(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=list(map(int,input().split()))
cnt={}
comb={}
ans=0
for i in a:
    if i in cnt:
        cnt[i]+=1
    else:
        cnt[i]=1
        
for i in cnt.values():
    if i<2:
        continue
    if not i in comb:
        comb[i]=combinations_count(i,2)
    ans+=comb[i]

for i in range(n):
    pans=ans
    if cnt[a[i]] in comb:
        pans-=comb[cnt[a[i]]]
    c=cnt[a[i]]-1
    if c>1:
        if not c in comb:
            comb[c]=combinations_count(c,2)
        pans+=comb[c]
    print(pans)

Solution (Python):
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()))
a1 = sorted(a)
cnt = {}
cnt1 = 1
for i in range(1,n):
    if a1[i] == a1[i-1]:
        cnt1 += 1
        if i == n-1:
            cnt[a1[i]] = cnt1
    else:
        cnt[a1[i-1]] = cnt1
        cnt1 = 1
        if i == n-1:
            cnt[a1[i]] = cnt1


cmb = {}
cmb[1] = 0
cmb_all = 0
for i in cnt.values():
    if i == 1:
        continue
    else:
        cmb1 = combinations_count(i,2)
        cmb_all += cmb1
        cmb[i] = cmb1

for i in a:
    cnt1 = cnt[i]
    if cnt1 == 1:
        print(cmb_all)
    else:
        ans = cmb_all + cmb[cnt1] * ((cnt1-2)/cnt1 - 1)
        print(int(ans))

Solution (Python):
import collections
import math

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

    Cou = collections.Counter(A)
    ans = 0

    oo = 0
    for c in Cou.values():
        oo += c * (c-1) // 2
    
    for k in range(1, N+1):
        c = Cou[A[k-1]] - 1
        print(oo - c)
    return 0


if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter

n = int(input())
c = list(map(int,input().split()))
s = Counter(c)
t = list(s.items())
nag = len(t)
x = 0
ans = 0

for i in range(nag):
    x += (t[i][1]*(t[i][1]-1))//2
    
for j in range(n):
    ans = x - s[c[j]] + 1 
    print(ans)

Solution (Python):
import collections

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

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

l1 = list(c.values())
l2 = [(i*(i-1))/2 if i >= 2 else 0 for i in l1]
sum_l2 = sum(l2)

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

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

se = set()
dic = dict()

for i in range(N):
    if A[i] in se:
        dic[A[i]] += 1
    else:
        se.add(A[i])
        dic.update({A[i]: 1})

c = 0
for k in dic.keys():
    c += (dic[k])*(dic[k]-1)//2

result = [0]*N
for i in range(N):
    result[i] = c - (dic[A[i]])*(dic[A[i]]-1)//2 + (dic[A[i]]-1)*(dic[A[i]]-2)//2

print("\n".join(list(map(str, result))))

Solution (Python):
import collections

n = input()
al = tuple(x for x in input().split())
aset = set(al)


ccc = collections.Counter
counts_d = ccc(al)


v_counts = ccc(v for v in counts_d.values())

whole_patterns = sum(
    (int(k * (k - 1) / 2)) * v for k, v in v_counts.items() if k > 1
)

[print(whole_patterns - counts_d[a] + 1) for a in al]

Solution (Python):
def main():
    import sys
    from collections import Counter
    
    N = int(input())
    A = list(map(int, sys.stdin.readline().split()))
    
    count = Counter(A)
    
    d = {}
    comb_sum = 0
    
    for i, n in count.items():
        if n == 1:
            comb = 0
            comb_ex = 0
        else:
            comb = n * (n - 1) / 2
            comb_ex = (n - 1) * (n - 2) / 2
        comb_sum += comb
        d[i] = comb_ex - comb
     
    for n in A:
        ans = int(d[n] + comb_sum)
        print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import collections
n = int(input())
a = list(map(str,input().split()))
ls = [i for i in range(2*10**5)]
c = collections.Counter(a)
comon = c.most_common()
ans = 0

for i in comon:
    if i != 1:
      ls[int(i[0])] = i[1]
      ans += (i[1]*(i[1]-1)//2)

for i in a:
    co = ls[int(i)]
    print(ans-(co*(co-1)//2)+((co-1)*(co-2)//2))

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

seen = [0]*n
count = 0
for i in range(n):
  seen[a[i]-1] =  seen[a[i]-1] + 1

for i in range(n):
   if (seen[i] == 0) or (seen[i] == 1):
      continue
   count = count +seen[i]*(seen[i]-1)//2

for i in a:
  ans = count - (seen[i-1]*(seen[i-1]-1))//2 + ((seen[i-1]-2)*(seen[i-1]-1))//2
    
  print(int(ans))

Solution (Python):
N = int(input())
num = list(map(int, input().split()))
dict = {}
for i in range(0, N):
    if num[i] in dict:
        dict[num[i]] += 1
    else:
        dict[num[i]] = 1
totalNum = 0
for i in range(1, N + 1):
    if i in dict and dict[i] > 1:
        totalNum += ((dict[i] * (dict[i] - 1)) / 2)
for i in range(0, N):
    if dict[num[i]] > 1:
        print(int(totalNum - dict[num[i]] + 1))
    else:
        print(int(totalNum))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
cnt = [0 for _ in range(N)]
for i in range(N):
    cnt[A[i]-1] += 1
S = 0
for i in range(N):
    S += cnt[i]*(cnt[i]-1)/2
for i in range(N):
    if cnt[A[i]-1] == 1:
        print(int(S))
    else:
        print(int(S+(3-2*cnt[A[i]-1])/2))

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

b=[0]*N
for a in A:
    b[a-1]+=1
    
c=[0]*N
tmp=b
for i in range(N):
    c[i]+=tmp[i]*(tmp[i]-1)//2
sum_c=sum(c)

for a in A:
    b[a-1]-=1
    print(sum_c-c[a-1]+b[a-1]*(b[a-1]-1)//2)
    b[a-1]+=1