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 math
def P(n, r):
    return math.factorial(n)//math.factorial(n-r)
def C(n, r):
    return P(n, r)//math.factorial(r)
N = int(input())
arr = list(map(int, input().split()))
cnt = [0]*(1+N)
ans = [0]*(1+N)
ans_less = [0]*(1+N)
for i in range(N):
    cnt[arr[i]] += 1
for i in range(1,N+1):
    if cnt[i] == 0 or cnt[i] == 1:
        continue
    ans[i] = C(cnt[i],2)
    if cnt[i] >= 3:
        ans_less[i] = C(cnt[i]-1,2)
    else:
        ans_less[i] = 0
su = sum(ans)
for i in range(N):
    print(su-ans[arr[i]]+ans_less[arr[i]])

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

import collections
c = collections.Counter(a)

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

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
from collections import Counter
def fac(n):
    return (n*(n-1))/2

cnt = Counter(A)

total = 0
for k,v in cnt.items():
    total += fac(cnt[k])

for a in A:
    print(int(total + fac(cnt[a]-1) - fac(cnt[a])))

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

l = [0] * (2*10**5)
for i in a:
    l[i-1] += 1

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

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

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

from collections import Counter
c = Counter(A)
m = {}
total = 0
for k, v in c.items():
    a = v*(v-1)//2
    m[k] = a
    total += a

for i in range(N):
    a = total - m[A[i]] + ((c[A[i]]-1)*(c[A[i]]-2)//2)
    print(a)

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

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

X = [0]*N

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

ans = 0
for i in range(N):
    ans += int(X[i]*(X[i]-1)/2)

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

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

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

import collections

c = collections.Counter(a)
cc=c.keys()

maxsum=0
for item in cc:
    temp=c[item]
    if temp>1:
        maxsum+=(temp)*(temp-1)//2

for item in a:
    temp=c[item]
    if temp>1:
        t=maxsum-temp*(temp-1)//2+(temp-1)*(temp-2)//2
        print(t)
    else:
        print(maxsum)

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

c = {}

for i in range(n):
  if a[i] in c:
    c[a[i]] += 1
  else:
    c[a[i]] = 1
  
s = 0
for i in c:
  s += c[i]*(c[i]-1)//2
  
dic = {i: s-c[i]+1 for i in c}
  
for i in range(n):
  print(dic[a[i]])

Solution (Python):
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
count = Counter(a)
kind = list(set(a))
ans_count = dict()
sum_ans = 0
for i in range(len(kind)):
    num = kind[i]
    k = count[num] 
    sum_ans += k*(k-1)//2
for i in range(n):
    ans = sum_ans
    num = a[i]
    k = count[num]
    ans -= k*(k-1) // 2
    ans += (k-1)*(k-2)//2
    print(ans)

Solution (Python):
n=int(input())
arr=list(map(int,input().split()))
d={}
for i in arr:
	if i in d:
		d[i]+=1
	else:
		d[i]=1
ans=0
for key,value in d.items():
	ans+=(value*(value-1))//2
for i in range(len(arr)):
	print(ans-(d[arr[i]]-1))

Solution (Python):

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



bucket = [0] * (2*10**5+1)
comb_bucket = [0] * (2*10**5+1)
for i in a:
    bucket[i]+=1

for i,v in enumerate(bucket):
    if v==0:
        continue
    comb_bucket[i] = (v * (v-1))//2
total_comb = sum(comb_bucket)
for ai in a:
    bi = bucket[ai]
    if bi == 1:
        print(total_comb)
    elif bi == 2:
        print(total_comb-1)
    else:
        print(total_comb - (bi-1))

Solution (Python):
from collections import Counter

n = int(input())
*arr, = map(int, input().split())


cnt = Counter(arr)

ans = 0

total = 0

for k, v in cnt.items():
    if v > 1:
        total += v * (v - 1) // 2


for v in arr:
    print(total - cnt.get(v, 1) + 1)

Solution (Python):
from collections import Counter
n = int(input())
A = list(map(int, input().split()))
c = Counter(A)
s = sum(v*(v-1)//2 for v in c.values())

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

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

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

for i in a:
    if count[i] == 1:
        print(ans)
    else:
        o = count[i]
        pi = ans - o*(o-1)//2
        pi += (o-1)*(o-2)//2
        print(pi)

Solution (Python):
from collections import Counter


def comb(k):
    return k*(k - 1)//2


def submit():
    n = int(input())
    alist = list(map(int, input().split()))
    a_count = Counter(alist)
    total = sum(comb(v) for k, v in a_count.items())

    for a in alist:
        print(total - (a_count[a] - 1))


submit()

Solution (Python):
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True 
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n = inn()
a = inl()
h = {}
for x in a:
    if x not in h:
        h[x] = 0
    h[x] += 1
tot = 0
for x in h:
    tot += h[x]*(h[x]-1)//2
for i in range(n):
    y = h[a[i]]
    print(tot-y*(y-1)//2+(y-1)*(y-2)//2)

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

counter = Counter(A)
cnts = counter.most_common()
total = 0
for cnt in cnts:
    total += cnt[1]*(cnt[1]-1)//2

for ai in A:
    print(total-(counter[ai]-1))

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

cnt={}
for i in A:
    if i not in cnt:cnt[i]=1
    else:cnt[i] +=1

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

point=sum(B.values())

for i in A:
    print(point-B[i]+C[i])

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

di = {}
B = set(A)
for b in B:
  di[b] = 0
total = 0
for a in A:
  di[a]+=1
for v in di.values():
  total += v*(v-1)//2

ans = []
for a in A:
  ans.append(total-di[a]+1)
print(*ans,sep='\n')

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

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

d = defaultdict(int)
for i in A :
    d[i] += 1

ans = 0
ans2 = defaultdict(int)
for a in d :
    ans += d.get(a)*(d.get(a)-1)//2
    ans2[a] = d.get(a)*(d.get(a)-1)//2 - (d.get(a)-1)*(d.get(a)-2)//2

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

Solution (Python):


def comb(num):
    return num*(num-1)//2

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

log=[0]*N
for item in A:
    log[item-1]+=1

sm=[0]*N
for index,item in enumerate(log):
    sm[index]=comb(item)

temp=sum(sm)

for k in range(N):
    ans=temp
    
    ans-=sm[A[k]-1]
    ans+=comb(log[A[k]-1]-1)
    print(ans)

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

A_count = collections.Counter(A)
A_count_nCr = {}

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

all_count = 0

for k, v in A_count.items():
    if v >= 2:
        A_count_nCr[k] = cmb(v, 2)
    else:
        A_count_nCr[k] = 0
    all_count += A_count_nCr[k]

for a in A:
    if A_count[a]-1 >= 2:
        print(all_count - A_count_nCr[a] + cmb(A_count[a]-1, 2))
    else:
        print(all_count - A_count_nCr[a])

Solution (Python):


def resolve():
    N=int(input())
    A=[int(i)-1 for i in input().split()]

    Atbl=[0]*N
    for i in A:
        Atbl[i]+=1
    cmpsum = 0
    for i in Atbl:
        cmpsum+=i*(i-1)//2
    for i in A:
        print(cmpsum-Atbl[i]+1)






if __name__ == "__main__":
    resolve()

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

    num_balls = [0 for i in range(N+1)]
    for a_i in A:
        num_balls[a_i] += 1

    comb = 0
    for n in range(N+1):
        comb += num_balls[n] * (num_balls[n]-1) // 2

    for a_i in A:
        ori = num_balls[a_i] * (num_balls[a_i]-1) // 2
        rev = (num_balls[a_i]-1) * (num_balls[a_i]-2) // 2
        print(comb + rev - ori)


solve()

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

d = defaultdict(int)
c = Counter(A)
total = sum(x * (x - 1) // 2 for x in c.values())

ans = []
for a in A:
    v = c[a]
    subt = v * (v - 1) // 2
    v -= 1
    add = v * (v - 1) // 2
    ans.append(total - subt + add)

print(*ans, sep="\n")

Solution (Python):
import collections
n = int(input())
a = list(map(int,input().split()))
c = collections.Counter(a)
sumnum = 0
for key,value in c.items():
    if value >= 2:
        sumnum += value*(value-1)//2

for i in range(n):
    key = [a[i]]
    ans = sumnum
    value = c[a[i]]
    if value >= 3:
        ans -= value*(value-1)//2
        ans += (value-2)*(value-1)//2
    if value <= 2:
        ans -= value*(value-1)//2
    print(ans)

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

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

Solution (Python):
def nc2(n):
	return (n*(n-1))//2
n=int(input())
arr=[int(x) for x in input().split()]
cnt={}
for x in arr:
	if x in cnt:
		cnt[x]+=1
	else:
		cnt[x]=1
ans=0
for x in cnt:
	ans+=nc2(cnt[x])
for x in arr:
	print(ans+nc2(cnt[x]-1)-nc2(cnt[x]))

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

for e in A:
    print(t-C[e]+1)

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

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

ans = 0
for k in dic:
    x = dic[k]
    ans += x * (x - 1) // 2

for a in A:
    print(ans - dic[a] + 1)

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

sumans = sum(ans)
for i in a:
    print(sumans - ans[i] + (tmp[i]-1)*(tmp[i]-2)//2)

Solution (Python):
import math
from collections import Counter

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

def cmb(r,k):
  if r < k: return 0
  return math.factorial(r) // math.factorial(r-k) // math.factorial(k)

c = Counter()
for a in A:
  c[a] += 1
al = 0
for v in c.values():
  al += cmb(v,2)

for k in range(N):
  print(al - (c[A[k]]-1))

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

max = 0
for i in range(1,N+1):
    x = numdic[i]
    max += x * (x - 1) // 2
for a in A:
    x = numdic[a]
    tmp = max - (x * (x - 1) // 2) + ((x - 1) * (x - 2) // 2)
    print(tmp)

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

Solution (Python):
import collections
input = raw_input
n = int(input())
nums = map(int, input().split(" "))

counter = collections.Counter(nums)
values = counter.keys()
all_out = 0
for val, num in counter.items():
    if num == 1:
        continue
    else:
        all_out += num * (num-1) // 2

outs = {}
for val, num in counter.items():
    if num == 1:
        outs[val] = all_out
    else:
        outs[val] = all_out - (num-1)

for num in nums:
    print(outs[num])

Solution (Python):
from collections import defaultdict

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

cnt = defaultdict(int)
for a in A:
    cnt[a] += 1

SUM = 0
for c in cnt.values():
    if c >= 2:
        SUM += c * (c - 1) // 2

for a in A:
    c = cnt[a]
    tmp = c * (c - 1) // 2
    if c > 2:
        tmp -= (c - 1) * (c - 2) // 2
    print(SUM - tmp)

Solution (Python):
import collections
n = int(input())
a = list(map(int,input().split()))
c = collections.Counter(a)
sumnum = 0
for key,value in c.items():
    if value >= 2:
        sumnum += value*(value-1)//2

for i in range(n):
    key = [a[i]]
    ans = sumnum
    value = c[a[i]]
    if value >= 3:
        ans -= value*(value-1)//2
        ans += (value-2)*(value-1)//2
    if value <= 2:
        ans -= value*(value-1)//2
    print(ans)

Solution (Python):
inxx = input()
inxx = inxx.split()

n = int(inxx[0])

inxx = input()
inxx = inxx.split()

arr = []
for x in inxx:
    arr.append(int(x))

import collections
cc = collections.Counter(arr)

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

s = sum(choose2(x) for x in cc.values())

for x in arr:
    sol = s - cc[x] + 1
    print(sol)

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

def comb2(n):
    if n < 2:
        return 0
    return n*(n-1)//2

cntd = {}
for i in range(N):
    if A[i] in cntd:
        cntd[A[i]] += 1
    else:
        cntd[A[i]] = 1

sum_comb = 0
for k, v in cntd.items():
    sum_comb += comb2(v)



for i in range(N):
    k = A[i]
    ans = sum_comb - comb2(cntd[k]) + comb2(cntd[k]-1)
    print(ans)

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

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

ans_list1 = []
ans_list2 = []

for i in cnt_list:
    if i < 2:
        ans_list1.append(0)
        ans_list2.append(0)
    elif i == 2:
        ans_list1.append(1)
        ans_list2.append(0)
    else:
        ans_list1.append(i*(i-1)//2)
        ans_list2.append((i-1)*(i-2)//2)

SUM = sum(ans_list1)

for i in A:
    print(SUM - ans_list1[i] + ans_list2[i])

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
from collections import Counter
c=Counter(A)
x=sum([v*(v-1)//2 for v in c.values()])
for a in A:
    print(x-c[a]+1)

Solution (Python):
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
a = list(Counter(A).items())
b = dict(a)

ans = 0
for i, j in a:
    ans += (j * (j-1)) // 2

for i in A:
    c = b[i]
    hiku = (c * (c-1)) // 2 
    tasu = ((c-1) * (c-2)) // 2
    print(ans - hiku + tasu )

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

D = {}
for a in A:
    if a not in D:
        D[a] = 1
    else:
        D[a] += 1

pattern = 0
for d in D.values():
    pattern += d * (d - 1) // 2

for i in range(N):
    print(pattern - D[A[i]] + 1)

Solution (Python):
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
count_A = Counter(A)
n2c = {k:v*(v-1)//2 for k, v in count_A.items()}
base = sum(n2c.values())
ans = 0
for ai in A:
    count_ai = count_A[ai]
    if count_ai == 1:
        ans = base
    else:
        ans = base - n2c[ai] + ((count_ai - 1) * (count_ai - 2) // 2)
    print(ans)

Solution (Python):
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)


def get_nums_l():
    return [ int(s) for s in input().split(" ")]


def get_nums_n(n):
    return [ int(input()) for _ in range(n)]


def get_all_int():
    return map(int, open(0).read().split())

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

n = int(input())
aaa = get_nums_l()

counts = [0] * (n+1)
patterns = [0] * (n+1)

for a in aaa:
    counts[a] += 1

for i in range(n+1):
    m = counts[i]
    patterns[i] = m * (m-1) // 2

all_patterns = sum(patterns)

for a in aaa:
    ans = all_patterns - patterns[a]
    ans += (counts[a]-1) * (counts[a]-2) // 2
    print(ans)

Solution (Python):
from collections import Counter

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

cA = Counter(A)

binomials = [0, 0, 1]
for i in range(3, max(cA.values()) + 1):
    binomials.append(binomials[-1] * i // (i - 2))

ans0 = sum(binomials[v] for v in cA.values())

for a in A:
    print(ans0 - binomials[cA[a]] + binomials[cA[a] - 1])

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


counter = {}
for number in A:
    if number not in counter:
        counter[number] = 1
    else:
        counter[number] += 1

tmp = 0
for c in counter.values():
    tmp += c * (c-1) // 2

answer = [0] * N

for i, number in enumerate(A):
    v = counter[number]
    answer[i] = str(tmp - v * (v-1) // 2 + (v-1) * (v-2) // 2)

print('\n'.join(answer))

Solution (Python):
import collections

def solve(N, A):
    c = collections.Counter(A)
    originals = [0 for i in range(N + 1)]
    minus = [0 for i in range(N + 1)]
    for (k, v) in c.items():
        originals[k] = v * (v-1) // 2
        minus[k] = ((v * (v-1)) - (v-1) * (v-2)) // 2
    S = sum(originals)

    ans = []
    for a in A:
        ans.append(str(S - minus[a]))
    return "\n".join(ans)

if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split(" ")))
    print(solve(N, A))

Solution (Python):
from collections import Counter as co
n=int(input())
l=list(map(int,input().split()))
var=co(l)
v=var.values()
ans=sum((i*(i-1))//2 for i in v)
for i in l:
    d=var[i]
    d=(d*(d-1))//2-((d-1)*(d-2))//2
    print(ans-d)

Solution (Python):
import sys
from collections import Counter

def comb(n, r):
    if r > n:
        return 0
    r = min(r, n - r)
    if r == 0: return 1
    if r == 1: return n
    numerator = list(range(n-r+1, n+1))
    denominator = list(range(1, r+1))
    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, *a = map(int, sys.stdin.read().split())

def main():
    c = Counter(a)
    res = 0
    for v in c.values():
        res += comb(v, 2)

    for i in a:
        yield res - comb(c[i], 2) + comb(c[i]-1, 2)
        
if __name__ == "__main__":
    ans = main()
    print(*ans, sep='\n')

Solution (Python):
import sys
from collections import Counter
n = int(input())
a_ls = [int(i) for i in sys.stdin.readline().split()]
ct = Counter(a_ls)
res = 0
for k, v in ct.items():
    res += v * (v-1) // 2
for a in a_ls:
    print(res - max(0, ct[a] - 1))

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

    table = collections.defaultdict(int)

    for x in A:
        table[x] += 1

    ret = 0
    for x in table:
        ret += (table[x]*(table[x]-1)) // 2
    for x in A:
        tmp = ret
        tmp -= (table[x]*(table[x]-1)) // 2
        tmp += ((table[x]-1)*(table[x]-2)) // 2
        print(tmp)


main()

Solution (Python):
import collections
N=int(input())
A=list(map(int,input().split()))
l=[]
allsum=0
val=0
c=collections.Counter(A)
values=list(c.values())	
key=list(c.keys())	
for i in range(len(key)):
    l.append([key[i],values[i]])
    allsum=allsum+(values[i]*(values[i]-1))/2
for i in range(N):
    val=c[A[i]]
    print(int(allsum-(val*(val-1)/2)+((val-1)*(val-2)/2)))

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

dic = {}

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

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

for x in A: print(P-dic[x]+1)

Solution (Python):
from collections import defaultdict as d

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

di = d(int)
for i in arr:
	di[i] += 1
	
s = 0
for key, value in di.items():
	s += (value * (value - 1)) // 2

di2 = d(int)

for key, value in di.items():
	nw = (value * (value - 1)) // 2
	x = s - nw
	di2[key] = x + ((value - 1) * (value - 2)) // 2
	
for i in range(n):
	print(di2[arr[i]])

Solution (Python):
n=int(input())
x=[int(n) for n in input().split()]
a={}
for i in x:
    try:
        a[i]+=1 
    except:
        a[i]=1
c=0
for i in a:
    c+=((a[i]-1)*(a[i]))//2
    
for i in x:
    print(c-((a[i]-1)*(a[i]))//2+((a[i]-1)*(a[i]-2))//2)

Solution (Python):
from collections import defaultdict


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

counts = defaultdict(int)

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

total = 0
for num, count in counts.items():
    pattern = count * (count - 1) // 2
    total += pattern

for a in A:
    count = counts[a]
    if count == 1:
        print(total)
        continue
    prev = count * (count - 1) // 2
    after = (count - 1) * (count - 2) // 2
    delta = prev - after
    print(total - delta)

Solution (Python):
import sys
n=int(sys.stdin.readline())
arr=list(map(int,sys.stdin.readline().split()))
from collections import defaultdict
s=0
dic=defaultdict(int)
for i in range(n):
    dic[arr[i]]+=1
for i in dic:
    s+=(dic[i]*(dic[i]-1))//2

for i in range(n):
    a=arr[i]
    ans=s
    
    ans-=dic[a]*(dic[a]-1)//2
    if dic[a]-2>=0:
        ans+=(dic[a]-1)*(dic[a]-2)//2
    print(ans)

Solution (Python):
import collections

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

ac = collections.Counter(a)

ac_c={}
sum=0
for i,j in ac.items():
    ac_c[i]=(j*(j-1))//2
    sum+=ac_c[i]

for i in a:
    tmp=ac_c[i]-ac_c[i]*(ac[i]-2)//(ac[i])
    print(sum-tmp)

Solution (Python):
N = int(input())
A = list(map(int, input().split()));
lis = [0]*(N+1)
ans = [0]*(N+1)
ans_selected = [0]*(N+1)
ans_sum = 0

for i in A:
    lis[i] += 1

for num, i in enumerate(lis):
    ans[num] = i * (i-1) // 2
    ans_sum += i * (i-1) // 2
    if i != 0:
        ans_selected[num] = (i-1) * (i-2) // 2

for i in A:
    print(ans_sum - ans[i] + ans_selected[i])

Solution (Python):

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

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

cmb = 0
for d in D:
    cmb += d*(d-1)//2

ans = []
for i in range(N):
    d = D[A[i]]
    tmp = cmb-(d*(d-1)//2)
    tmp += (d-1)*(d-2)//2
    
    ans.append(tmp)

for an in ans:
    print(an)

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

keisan = dict()

for key,value in d.items():
  keisan[key] = (value * (value-1)) // 2
res = sum(keisan.values())

for a in A:
  
  val = d[a]
  
  print(res - ((val*(val-1))//2) +(((val-1)*(val-2))//2)  )

Solution (Python):
def main():
    from collections import Counter
    n = int(input())
    l = list(map(int,input().split()))
    c = Counter(l)

    
    count = 0
    for j,k in c.items():
        count += k*(k-1)//2
        
    for i in l:
        x = c[i]
        y = (x*(x-1)-(x-1)*(x-2))//2
        print(count-y)
if __name__ == "__main__":
    main()

Solution (Python):
import sys
from collections import defaultdict

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')

N = I()
A = LMI()

d = defaultdict(int)
for a in A:
    d[a] += 1


ad = dict()
s = 0
for i, cnt in d.items():
    if cnt < 2:
        ad[i] = 0
    else:
        ad[i] = cnt * (cnt - 1) // 2
    s += ad[i]

for a in A:
    ans = s - ad[a]
    if d[a] >= 2:
        ans += (d[a] - 1) * (d[a] - 2) // 2
    print(ans)

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

n = 0

d2 = {}

for k, v in d.items():
    m = v*(v-1)//2
    d2[k] = m
    n += m

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

Solution (Python):

import sys,os
from collections import defaultdict, deque
from fractions import gcd
from math import ceil, floor
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    YN = lambda c : print('Yes') if c else print('No')
    MOD = 10**9+7

    N = II()
    A = LMIIS()
    nums = [0]*(N+1)
    
    for a in A:
        nums[a] += 1

    max_choice = 0
    for c in nums:
        max_choice += c*(c-1)//2
    
    for k in range(N):
        x = nums[A[k]]
        print(max_choice - x*(x-1)//2 + max((x-1)*(x-2)//2,0))

    

if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
counter = Counter(A)
ans = 0
for k, v in counter.items():
    if 2 <= v:
        ans += v * (v - 1) // 2
for a in A:
    if a in counter and 2 <= counter[a]:
        v = counter[a]
        diff = v * (v - 1) // 2 - ((v- 1) * (v - 2) // 2)
        print(ans - diff)
    else:
        print(ans)

Solution (Python):
inpl = lambda: list(map(int,input().split()))
N = int(input())
A = inpl()
C = [0]*(N+1)
for i in range(N):
    C[A[i]] += 1
total = sum([ c*(c-1)//2 for c in C])
for k in range(N):
    c = C[A[k]]
    print(total - c + 1)

Solution (Python):

N = int(input())
A = list(map(int, input().split()))
cnt = [0]*N
for i in range(N):
    cnt[A[i]-1] += 1
c_sum = 0
for i in range(N):
    c_sum += cnt[i] * (cnt[i]-1) // 2
for i in range(N):
    print(c_sum - cnt[A[i]-1] + 1)

Solution (Python):
import collections

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

c = collections.Counter(A)
ans = sum([x*(x-1)//2 for x in c.values()])

for i in range(N):
    print(ans - c[A[i]] + 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
ans=0
for d in D:
    ans+=D[d]*(D[d]-1)//2
for a in A:
    print(ans-D[a]+1)

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

l1 = [0]* (n+1)

for i in range(n):
    l1[a[i]] += 1

l2 = [0] * (n+1)

for i in range(1, n+1):
    l2[i] = l1[i] * (l1[i]-1) // 2

l3 = [0] * (n+1)

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

x = sum(l2)

l4 = [0] * (n+1)

for i in range(1, n+ 1):
    l4[i] = x - l2[i] + l3[i]


for i in range(n):
    print(l4[a[i]])

Solution (Python):
import sys
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

NUM = defaultdict(int)

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

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



ans = []
for a in A:
    r = NUM[a]
    t = s - (r - 1)
    ans.append(t)

print("\n".join(map(str, ans)))

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
from collections import *
c=Counter(l)
a=sum(i*(i-1)//2 for i in c.values())
for i in l: print(a-c[i]+1)

Solution (Python):
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols
    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])
    return ret


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read()[:-1] for _ in range(H)]


def read_map_as_int(H):
    '''
    
    '''
    ret = []
    for _ in range(H):
        ret.append([1 if s == '
        
        
    return ret


MOD = 10**9 + 7

from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  

N = read_a_int()
A = read_ints()

A_cnt = Counter(A)




total = 0
for _, v in A_cnt.items():
    total += v * (v - 1) // 2

for a in A:
    print(total - (A_cnt[a] - 1))

Solution (Python):
n = int(input())
a_list = [int(a) - 1 for a in input().split()]
count = [0] * n
for a in a_list:
  count[a] += 1
comb = 0
for c in count:
  comb += c * (c - 1) // 2
for a in a_list:
  c = count[a]
  print(comb - c + 1)

Solution (Python):
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

n = ii()
alis = li()
dp = [0] * n
for i in range(n):
    dp[alis[i] -1] += 1
dpp = [0] * n
summ = 0
for i in range(n):
    ima = dp[i] * (dp[i]-1) // 2
    summ += ima
for i in range(n):
    if dp[alis[i] -1] <=1:
        print(summ)
    else:
        mi = dp[alis[i]-1] -1 
        print(summ -mi)

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

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

S = 0
for n in num:
    S += n * (n - 1) // 2

ans = []
for i in range(N):
    rst = S
    p = A[i]
    m = num[p]

    rst -= m * (m - 1) // 2
    rst += (m - 1) * (m - 2) // 2
    ans.append(rst)

print(*ans, sep='\n')

Solution (Python):
n = int(input())
a = [int(x) for x in input().split()]
key = [0]*n
for i in range(n):
  key[a[i]-1] += 1
kake = [1]*n
ans = 0
for i in range(n):
  ans += key[i]*(key[i]-1)//2
for i in range(n):
  print(ans-(key[a[i]-1]-1))

Solution (Python):
import sys
from collections import defaultdict

rl = sys.stdin.readline


def solve():
    N = int(rl())
    A = list(map(int, rl().split()))
    
    counter = defaultdict(int)
    for ai in A:
        counter[ai] += 1
    
    B = defaultdict(int)
    for ai in A:
        B[ai] = counter[ai] * (counter[ai] - 1) // 2
    
    tmp = sum(B.values())
    for ai in A:
        print(tmp - B[ai] + (counter[ai] - 1) * (counter[ai] - 2) // 2)


if __name__ == '__main__':
    solve()