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

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

b = a[:]
b.sort()

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

ct = 1
tmp = b[1]
sum = 0
for j in range(1,n):
  if b[j] == b[j-1]:
    ct += 1
  else :  
    c[tmp][0] = ct
    cm = int(ct*(ct-1)/2)
    c[tmp][1] = cm
    cm1 = int((ct-1)*(ct-2)/2)
    c[tmp][2] = cm1
    sum += cm
    tmp = b[j]
    ct = 1

c[tmp][0] = ct
cm = int(ct*(ct-1)/2)
c[tmp][1] = cm
cm1 = int((ct-1)*(ct-2)/2)
c[tmp][2] = cm1
sum += cm      

for k in a:
  print(sum-c[k][1] + c[k][2])

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


from operator import mul
from functools import reduce
import copy
hh = copy.deepcopy(h)
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

for index,i in enumerate(hh):

    if i==0 or i==1:
        h[index]=0
        continue
    h[index] = cmb(i,2)

ss = sum(h)

for i,j in zip(a,hh):

    if hh[i]<=2:

        print(ss-h[i])
    else:

        print(ss-h[i]+cmb(hh[i]-1,2))

Solution (Python):
k = int(input())
l = list(map(int, input().split()))
import collections
z = []
n = collections.Counter(l)
y = [x* (x -1) / 2 for x in n.values()]
mai = {}
plus = {}
for k in n.keys():
  plus.setdefault(k, ((n[k] - 1) * (n[k]) / 2) )
  mai.setdefault(k, ((n[k] - 1) * (n[k] -2) / 2) )

base = sum(y)
q = [base - (plus[i] - mai[i]) for i in l]
for i in range(len(q)):
  print(int(q[i]))

Solution (Python):
from collections import Counter

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

ans_sum = 0

counter = Counter(a_li)

for i in counter.values():
    ans_sum += i*(i - 1) // 2

for a in a_li:
    count = counter[a]
    if count == 1:
        print(ans_sum)
    else:
        print(ans_sum - count*(count - 1) // 2 + (count - 1)*(count - 2) // 2)

Solution (Python):
import collections
N = int(input())
A = list(map(int,input().split()))
ans = 0
c = collections.Counter(A)
for l in c.values():
    ans = ans + l*(l-1)/2
for k in A:
    print(int(ans-(c[k]-1)))

Solution (Python):
from collections import Counter
import sys
input = sys.stdin.readline
n = int(input())
li = list(map(int,input().split()))
S=Counter(li)
Sum = 0
for k in range(n):
            if S[k+1] > 1:
                Sum += (S[k+1] * (S[k+1] -1)) / 2
            else:
                continue


for i in range(n):
    if S[li[i]] > 1:
        Sum_2 = Sum
        Sum_2 = Sum_2 - (S[li[i]] * (S[li[i]] -1)) / 2 + ((S[li[i]]-1) * (S[li[i]] -2)) / 2
        print(int(Sum_2))
    else:
        print(int(Sum))

Solution (Python):
import copy
import collections
n = int(input())

a_list = list(map(str, input().split()))
c = collections.Counter(a_list)

tmp_list = []

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





for k in range(n):
    
    
    
    
    
    
    
    
    
    

    

    print(int(ans - (c[str(a_list[k])]-1)))

Solution (Python):
import collections
N = int(input())
A = list(map(int,input().split()))
sameNumber = collections.Counter(A)
total = 0
for n in sameNumber:
  total += sameNumber[n] * (sameNumber[n]-1)

def count(val):
  if sameNumber[val] > 1:
    return (total -2 * (sameNumber[val]-1)) //2
  return total //2

for n in A:
  print(count(n))

Solution (Python):

import collections

def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n
    if n == 0: return 0
    if n == 1: return 0

    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 cmb(n, r):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % mod

N = 3 * pow(10, 5)  
mod = pow(10, 9) + 7
fact = [1, 1]  
factinv = [1, 1]  
inv = [0, 1]  

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % mod)
    
    inv.append((-inv[mod % i] * (mod // i)) % mod)
    
    factinv.append((factinv[-1] * inv[-1]) % mod)
    
"""

n = int(input())
a = [int(i) for i in input().split()] 

count = [0 for _ in range(n+1)]
ans = [0 for _ in range(n+1)]
c = collections.Counter(a)

for i in range(n+1):
    
    tmp = c[i]
    
    count[i] = tmp
    ans[i] = cmb(tmp, 2)
    

sum_ans = sum(ans)

for k in range(n):
    
    
    
    
    ans = sum_ans + cmb(count[a[k]]-1, 2) - cmb(count[a[k]], 2)
    print(ans)

Solution (Python):
from collections import defaultdict
n, *D  = [list(map(int, s.split())) for s in open(0)]
D = D[0]
dic = defaultdict(int)
for i in D:
    dic[i] += 1

total_comb = 0
for j in dic:
    if dic[j] >= 2:
        total_comb += dic[j] * (dic[j] - 1) / 2
for k in D:
    if dic[k] >= 2:
        print(int(total_comb - dic[k] + 1))
    else:
        print(int(total_comb))

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

from collections import Counter
d=Counter(a)
f={}
for k,v in d.items():
    f[k]=v*(v-1)/2

ans={}
s=sum(f.values())
for k in f.keys():
    ans[k]=(d[k]-1)*(d[k]-2)/2 + s- f[k]

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

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
count = [0]*(N+1)
for i in range(N):
    count[A[i]] += 1

s = sum([x*(x-1)/2 for x in count])

for k in range(N):
    print(int(s-count[A[k]]+1))

Solution (Python):
import collections
import math

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

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

counts = collections.Counter(A_list)
ans = 0
for _, v in counts.items():
    
    if v > 1:
        ans += combinations_count(v, 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

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


def main(n: int, a: list):
    counter = collections.Counter(a)
    pairs = {k: v * (v - 1) / 2 for k, v in counter.items()}
    pairs_all = sum(pairs.values())

    for i in range(N):
        k = a[i]
        v = counter[k] - 1
        pair_cnt = pairs_all - pairs[k] + (v * (v - 1) / 2)

        print(int(pair_cnt))


main(N, A)

Solution (Python):
import sys
from collections import Counter

input = sys.stdin.readline


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

    counter = Counter()
    ret = 0
    for num in nums:
        counter[num] += 1
        ret += (counter[num] - 1)

    for num in nums:
        ret -= (counter[num] - 1)
        print(ret)
        ret += (counter[num] - 1)


if __name__ == '__main__':
    main()

Solution (Python):

import collections
import math


def combinations_count(s, r):
    return math.factorial(s) // (math.factorial(s - r) * math.factorial(r))

def get_nc2(s):
    if nc2[s] == -1:
        t_nc2 = combinations_count(s, 2)
        nc2[s] = t_nc2
        return t_nc2
    else:
        return nc2[s]


n = int(input())
a = list(map(int, input().split()))
col_a = collections.Counter(a)
res = [-1]*(n+1)
mid = [-1]*(n+1)
mid_sum =0
nc2 = [-1]*(n+1)
nc2[0] = 0
nc2[1] = 0




for i in col_a.keys():
    tmp_l = [0]*2
    cnt_i = col_a[i]
    
    if mid[cnt_i] == -1:
        tmp_l[0] = get_nc2(cnt_i)
        tmp_l[1] = get_nc2(cnt_i-1)
        
        mid[cnt_i] = tmp_l
        mid_sum += tmp_l[0]
    else:
        mid_sum += mid[cnt_i][0]



for i in col_a.keys():
    cnt_i = col_a[i]
    
    if res[cnt_i] == -1:
        tmp_cnt = mid_sum - mid[cnt_i][0] + mid[cnt_i][1]
        res[cnt_i] = tmp_cnt
        

for ai in a:
    print(res[col_a[ai]])

Solution (Python):


n = int(input())
t = input().split()
a = []
for c in t:
    a.append(int(c))


h = {}
for v in a:
    if v in h:
        h[v] += 1
    else:
        h[v] = 1
total = 0
for k in h.values():
    if k > 1:
        total += (k*(k-1)//2)

for c in a:
    aa = total
    if h[c] > 1:
        aa -= (h[c]*(h[c]-1)//2)
    if h[c] > 2:
        aa += ((h[c]-1)*(h[c]-2)//2)
    print(aa)

Solution (Python):



from collections import deque,Counter








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

cbdp = [0,0]
cbdp2 = [i*(i-1)//2 for i in range(2,n+1)]
cbdp += cbdp2


ctA = Counter(arrA)


totalconb = 0
for v in ctA.values():
    totalconb += cbdp[v]


for a in arrA:
    print(totalconb-cbdp[ctA[a]]+cbdp[ctA[a]-1])

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

c = collections.Counter(a)
values, counts = zip(*c.most_common())
counts = list(counts)
if counts[-1] == 1:
	k = counts.index(1)
	del counts[k:]


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

from collections import deque
t = deque()
for i in counts:
    t.append(cmb(i,2))

t = list(t)
ans = sum(t)

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

Solution (Python):
N = int(input())
As = list(map(int, input().split()))
a,b,c = [0]*N,[0]*N,[0]*N
for i in As:
  a[i-1] += 1
  b[i-1] = a[i-1]*(a[i-1]-1)//2
  c[i-1] = (a[i-1]-1)*(a[i-1]-2)//2

d = sum(b)
print("\n".join([str(d-b[i-1]+c[i-1]) for i in As]))

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

import collections

from operator import mul
from functools import reduce

def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

def main():
    n = int(input())
    a_li = list(map(int, input().rstrip().split()))
    c = collections.Counter(a_li)
    total = 0
    dict_1 = {}
    dict_2 = {}
    for k, v in c.items():
        if v > 1:
            num = combinations_count(v, 2)
            total += num
            dict_1[k] = num
            if v - 1 > 1:
                dict_2[k] = combinations_count(v-1, 2)
            else:
                dict_2[k] = 0
        else:
            dict_1[k] = 0
            dict_2[k] = 0

    for a in a_li:
        ans = total - dict_1[a] + dict_2[a]
        print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
import math
def i():return int(sys.stdin.readline().replace("\n",""))
def i2():return map(int,sys.stdin.readline().replace("\n","").split())
def s():return str(sys.stdin.readline().replace("\n",""))
def l():return list(sys.stdin.readline().replace("\n",""))
def intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def strl():return [str(k) for k in sys.stdin.readline().replace("\n","").split()]
def lx():return list(map(lambda x:int(x)*-1,sys.stdin.readline().replace("\n","").split()))
def t():return tuple(map(int,sys.stdin.readline().replace("\n","").split()))

if __name__ == "__main__":pass
n = i()
a = intl()
A = list(set(a))
d = {}
c = 0
def f(c):
    return (c*(c-1))//2
for i in range(n):
    try:
        d[a[i]] += 1
    except KeyError:
        d[a[i]] = 1
for i in A:
    c += f(d[i])
for i in range(n):
    t = d[a[i]]
    if t > 1:
        print(c-t+1)
    else:
        print(c)

Solution (Python):
import math
import copy
import numpy as np

N = int(input())
As = input().split()
A = [0 for i in range(N)]
for i in range(N):
  A[i] = int(As[i])


A_unique = list(set(A))
num_unique = len(A_unique)
to_unique = {}
for i,a in enumerate(A_unique):
  to_unique[a] = i
for i in range(N):
  A[i] = to_unique[A[i]]


counts = [0 for i in range(num_unique)]
for _,A_i in enumerate(A):
  counts[A_i] += 1
answer_total = 0
for count in counts:
  answer_total += int(count*(count-1)/2)

spoiler = [-1 for i in range(num_unique)]
for k in range(N):
  if spoiler[A[k]]<0:
    answer_k = answer_total - (counts[A[k]]-1)
    spoiler[A[k]] = answer_k
  else:
    answer_k = spoiler[A[k]]
  print(answer_k)

Solution (Python):
import collections
n=int(input())
a=list(map(str,input().split()))
c=collections.Counter(a)
b=0

for i in c.keys():
    b+=(c[i]*(c[i]-1)/2)
for i in a:
    print(int(b-c[i]+1))

Solution (Python):
import collections
MOD = 10**18+9
n = int(input())
a = list(map(int, input().split()))
b = [[0 for j in range(2)] for i in range(n)]
c = collections.Counter(a)
s = 0
def nCr(nn, r, mod):
	r = min(r, nn-r)
	numer = denom = 1
	for i in range(1, r+1):
		numer = numer * (nn+1-i) % mod
		denom = denom * i % mod
	return numer * pow(denom, mod-2, mod) % mod
for i in c:
	if c[i] < 2:
		b[i-1][0] = 0
	else:
		b[i-1][0] = nCr(c[i],2,MOD)
		s += b[i-1][0]
	if c[i]-1 < 2:
		b[i-1][1] = 0
	else:
		b[i-1][1] = nCr(c[i]-1,2,MOD)
for i in a:
	ans = s-b[i-1][0] + b[i-1][1]
	print (ans)

Solution (Python):
def main():
  import sys
  input = sys.stdin.readline
  n = int(input())
  a = [int(i) for i in input().split()]
  Ss = set(a)
  s = list(Ss)
  import collections
  c = collections.Counter(a)
  j = 0
  
  for i in range(len(s)):
    j += int(c[s[i]]*(c[s[i]]-1)/2)
  
  for i in range(n):
    if c[a[i]] >= 2:
      print(int(j-(c[a[i]]-1)))
    else:
      print(j)
if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
A=list(map(int,input().split(' ')))
import math
import collections
import operator as op
from functools import reduce
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 if n>1 else 0
counter=collections.Counter(A)
penult=0
for i in range(1,N+1):
    penult=penult+nCr(counter[i],2)
for i in range(0,N):
    print(int(penult-(counter[A[i]]-1)))

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

C = collections.Counter(L)
ans = 0
for i in C.items():
    ans += int((i[1]*(i[1]-1))/2)
        
for i in range(N):
    del_target = L[i]
    print(ans - (C[del_target]-1))

Solution (Python):
N = int(input())
data = [i for i in input().split()]

number_count = {}
for cmp in data:
    if cmp in number_count:
        number_count[cmp] += 1
        continue
    number_count[cmp] = 1

C_tot = 0
for cmp in number_count:
    C_tot += int(number_count[cmp] * (number_count[cmp] - 1) / 2)

for k in range(N):
    C = C_tot - int(number_count[data[k]] * (number_count[data[k]] - 1) / 2) + int((number_count[data[k]] - 1) * (number_count[data[k]] - 2) / 2)
    print(str(C))

Solution (Python):
import collections

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

ans=0
ll=[0]*(n+1)
for i in l:
    ans-=int(ll[i]*(ll[i]-1)/2)
    ll[i]+=1
    ans+=int(ll[i]*(ll[i]-1)/2)

for j in l:
    b=int(ll[j]*(ll[j]-1)/2)
    c=int((ll[j]-1)*(ll[j]-2)/2)
    print(ans-b+c)

Solution (Python):
from collections import defaultdict

n = int(input())
box = list(map(int, input().split()))
c = defaultdict(int)
for b in box:
    c[b] += 1
    
anss = sum([int(1/2 * c[k] * (c[k] - 1)) for k in c.keys()])
for b in box:
    fi = anss - int(1/2 * c[b] * (c[b] - 1)) + int(1/2 * (c[b]-1) * (c[b] - 2)) 
    print(fi)

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

values = {}
for i in nums:
  if i in values: values[i] += 1
  else: values[i] = 1

sums={} 
subsums={} 
allsum = 0
for k,v in values.items():
  sums[k] = v * (v - 1) / 2
  undersum = (v - 2) * (v - 1) / 2 if v > 1 else 0
  subsums[k] = sums[k] - undersum
  allsum += sums[k]




for i in nums:
  print(int(allsum - subsums[i]))

Solution (Python):




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


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


for i in range(n + 1):
    anl[i] = acntl[i] * (acntl[i] - 1) // 2
    anl[0] = anl[0] + anl[i]


for i in range(n):
    ans = anl[0] - anl[a[i]] + ((acntl[a[i]] - 1) * max(0, acntl[a[i]] - 2)) // 2
    print(ans)

Solution (Python):
import sys
import math
from collections import Counter

n = int(sys.stdin.readline())
a = sys.stdin.readline().split()
c = Counter(a)

total_comb_num = sum(map(lambda v: max(v*(v-1)//2, 0), c.values()))
for num in a:
    x = c[num]
    print(total_comb_num + max((x-1)*(x-2)//2, 0) - max((x*(x-1)//2, 0)))

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

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

import collections
c = collections.Counter(A)
B=list(c.values())
ans=0
for k in range(len(B)):
    ans+=B[k]*(B[k]-1)/2
for i in range(N):
  anser=ans
  anser=anser+1-c[A[i]]
  print(int(anser))

Solution (Python):
from collections import Counter
import math

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

    cnt = Counter(A)
    
    def comb(n, r):
        if n - r < 0: 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
    cnt_comb = {}
    sm = 0
    for key, val in cnt.items():
        tmp = comb(val, 2)
        cnt_comb[key] = tmp
        sm = sm + tmp
    

    
    for a in A:
        comb_val = cnt_comb.get(a)
        cmb = comb(cnt.get(a) - 1, 2)
        print(sm - comb_val + cmb)



if __name__ == '__main__':
    solver()

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

Solution (Python):

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


combs = [0, 0]
for i in range(2, N+1):
    combs.append(i*(i-1)//2)


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

sumall = 0
for n in nums:
    sumall += combs[n]

ans = [sumall]*N
for i, a in enumerate(A):
    n = nums[a]
    ans[i] += combs[n-1] - combs[n]


for a in ans:
    print(a)

Solution (Python):
n = int(input())
a = [int(x) for x in input().split()]
sum = 0

d = {}
d_sum = {}
dd_sum = {}

for aa in a:
    if aa not in d:
        d[aa] = 0
    d[aa] += 1

for dd in d.keys():
    sum += d[dd] * (d[dd]-1) // 2
    d_sum[dd] = d[dd] * (d[dd]-1) // 2

for i in range(len(a)):
    buf = a[i]
    tmp = sum
    tmp -= d_sum[buf]
    tmp += (d[buf] - 1) * (d[buf] - 2) // 2
    print(tmp)

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

num = [0]*n

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


tmp=0
for i in num:
    if i>=2:
        tmp+=(i*(i-1))//2

for i in a:
    if num[i-1]>=2:
        print(tmp-(num[i-1]-1))
    else:
        print(tmp)

Solution (Python):
from collections import defaultdict

if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split(" ")))

    variation = set(a)
    a_count_dict = defaultdict(int)
    for j in a:
        a_count_dict[j] += 1

    total = 0
    c_dict = {}
    for i in variation:
        count = a_count_dict[i]
        if count > 1: 
            
            c = count * (count - 1) // 2
        else:
            c = 0
        c_dict[i] = c
        total = total + c

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    
    for k in range(n):
        ck = c_dict[a[k]]
        ca = a_count_dict[a[k]]
        if ck == 1 or ck == 0:
            q = ck
        else:
            
            q = ck - ck * (ca - 2) // ca


        
        
        
        
        
        
        
        print(total - q)

Solution (Python):
import math
import collections


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


N = int(input().split()[0])
a_list = list(map(int, input().split()))

counter = collections.Counter(a_list)
ans_dict_1 = {}
ans_dict_2 = {}

for k, v in counter.items():
    if v >= 2:
        ans_dict_1[k] = C(v, 2)
    if v >= 3:
        ans_dict_2[k] = C(v - 1, 2)
total_w = sum(ans_dict_1.values())

for k in range(N):
    n = a_list[k]

    if counter[n] >= 3:
        w = total_w - ans_dict_1[n] + ans_dict_2[n]
    elif counter[n] >= 2:
        w = total_w - ans_dict_1[n]
    else:
        w = total_w
    print(w)

Solution (Python):
from collections import Counter as C
n = int(input())
a = list(map(int,input().split()))
d = C(a)
res = 0
for i in d.keys():
	res+=d[i]*(d[i] - 1) // 2
ans = res
for k in range(n):
	yy = d[a[k]] - 1
	res+= (yy * (yy - 1) // 2)
	res-= (d[a[k]] * (d[a[k]] - 1) // 2)
	print(res)
	res = ans

Solution (Python):


def abc159d(n, arr):
    dic = {}
    for a in arr:
        if a in dic:
            dic[a] += 1
        else:
            dic[a] = 1
    
    
    del_list = []
    for k, val in dic.items():
        if val == 1:
            del_list.append(k)
    for item in del_list:
        del dic[item]

    typ_ans = 0
    for val in dic.values():
        typ_ans += val * (val-1) // 2
    
    ans = []
    for a in arr:
        sum = typ_ans
        if a in dic:
            sum -= dic[a] * (dic[a]-1) // 2
            new_val = dic[a] - 1
            sum += new_val * (new_val-1) // 2

        ans.append(sum)

    return ans



if(__name__ == '__main__'):
    
    n = int(input())
    arr = input().split(" ")
    
    ans = abc159d(n, arr)
    
    
    for a in ans:
        print(a)

Solution (Python):
import sys
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
 
N = II()
A = LI()
 
from collections import Counter
S = Counter(A)
result = sum(list(map(lambda x: int(x*(x-1)/2), S.values())))
 
for a in A:
 n = S[a]
 print(result-n+1)

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

import copy

from collections import Counter


ans = [0] * n
b = Counter(a)
sm = 0
for i,j in b.items():
    if j >= 2:
        sm += j*(j-1)//2

for i in range(n):
    ans[i] = sm - b[a[i]] + 1
print(*ans)

Solution (Python):
import collections

n = int(input())
A = list(map(int, input().split(' ')))
 
c = dict(collections.Counter(A))
num_dict = {}
total = 0
for a, count in c.items():
  num_dict[a] = (count*(count-1)/2, (count-1)*(count-2)/2)
  total += count*(count-1)/2

ans_dict = {}
for a in c.keys():
  ans_dict[a] = total - num_dict[a][0] + num_dict[a][1]

for k in range(n):
  print(int(ans_dict[A[k]]))

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
num = [0]*n
for i in A:
  num[i-1] += 1
def comb(n):
    if n < 2:
      return 0
    else:
      return n * (n-1) // 2
ans_sub = 0
for i in num:
  ans_sub += comb(i)
for i in A:
  if num[i-1] < 2:
    a = 0
  else:
    a = num[i-1]-1
  print(ans_sub-a)

Solution (Python):
def f(n):
    if n==0:
        return 0
    return n*(n-1)/2
N=int(input())
A=list(map(int,input().split()))
d=dict()
e=dict()
ans=0
for i in range(N):
    if A[i] not in d:
        d[A[i]]=0
    d[A[i]]+=1

for j in d:
    e[j]=f(d[j])
    ans+=e[j]

for i in A:
    print(int(ans-d[i]+1))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
amax=max(a)+1

s=[0]*amax
for ii in range(n):
    s[a[ii]]=s[a[ii]]+1

ss=0
for ii in range(amax):
    ss=ss+((s[ii]-1)*s[ii])//2

for ii in range(n):
    if s[a[ii]]==0:
        print(ss)
    else:
        sai=s[a[ii]]
        t=ss-(sai*(sai-1))//2+((sai-1)*(sai-2))//2
        print(t)

Solution (Python):
N = int(input())
import copy
import math
def nP2(N):
    return (math.factorial(N)/((math.factorial(2))*math.factorial(N-2)))
L = list(map(int,input().split()))
Count = [0 for _ in range(N+1)]
for el in L:
    Count[el]+=1
res = 1
for i in Count:
    if i>=2:
        res += nP2(i)
for balls in L:
    print(int(res-Count[balls]))

Solution (Python):
def main():
    N = int(input())
    A = [int(v) for v in input().split()]
    C = [0] * (N + 1)

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

    b = 0
    
    for cnt in C:
        b += ((cnt * (cnt - 1)) // 2)

    for i in range(N):
        p = ((C[A[i]] - 1) * (C[A[i]] - 2)) >> 1
        m = (C[A[i]] * (C[A[i]] - 1)) >> 1
        print(b - m + p)


if __name__ == '__main__':
    main()

Solution (Python):









import os
import sys
from collections import Counter as ct

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

    c = ct(A)

    d = {}
    total = 0
    for key in c:
        d[key] = c[key]*(c[key]-1)//2
        total += d[key]


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

        

if __name__ == "__main__":
    main()

Solution (Python):
from sys import stdin

N = int(input())
readline = stdin.readline
A = list(readline().split())

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

dmemo = {}
for a in A:
    dmemo.setdefault(a, 0)
    dmemo[a] += 1

max_num = 0
cmemo = {}
for dk, dv in dmemo.items():
    if dv not in cmemo:
        val = ncr(dv)
        cmemo[dv] = val
    max_num += cmemo[dv]

for a in A:
    val = dmemo[a]
    a1 = ncr(val)
    a2 = ncr(val-1)
    ans = max_num - a1 + a2
    print(ans)

Solution (Python):
from operator import mul
from functools import reduce
def cmb(n,r):
  if n<r:
    return 0
  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
from collections import Counter as cnt
n=int(input())
l=list(map(int,input().split()))

dic=cnt(l)

s1={}
s2={}
for k,v in dic.items():
  s1[k]=cmb(v,2)
  s2[k]=cmb(v-1,2)
s=sum(s1.values())
for i in range(n):
  x=l[i]
  print(s-s1[x]+s2[x])

Solution (Python):
import collections

N = int(input())
k = input().split(" ")
m = {}



c = collections.Counter(k)


for i in c:
    m[i] = c[i]*(c[i]-1)//2        
total = sum(m.values())




for i in k:
    print(total - (c[i]-1))

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

import collections
c = collections.Counter(a_list)

dict_items = list(c.items())

ans_max = 0
for j in range(len(dict_items)):
    ans_max += ((dict_items[j][1] - 0) * (dict_items[j][1] - 1)) / 2
    
for i in range(n): 
    tmp = c[a_list[i]]
    ans_tmp = ans_max - tmp*(tmp-1)/2 + (tmp-1)*(tmp-2)/2
    print(int(ans_tmp))

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

import numpy as np

n = ii()
a = li()

def cmb(n, r):
    if n - r < r: r = n - r
    if n == 0: return 0
    if n == 1: return 0
    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


memo_cnt = [0]*n
memo_cmb = [0]*(n+1)
memo_cmb2 = [0]*(n+1)

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

for k,i in enumerate(memo_cnt):
    if i >= 3:
        memo_cmb[k+1] = cmb(i,2)
        memo_cmb2[k+1] = cmb(i-1,2)
    elif i == 2:
        memo_cmb[k+1] = cmb(i,2)

cmb_cumsum = np.array(memo_cmb).cumsum()






for i in range(n):
    a_now = a[i]
    itself = memo_cmb2[a_now]
    
    other = cmb_cumsum[a_now-1] + cmb_cumsum[-1] - cmb_cumsum[a_now]

    print(itself + other)

Solution (Python):
def cycle(x):
    result = 0
    for y in range(int(x), 0, -1):
        result += y - 1
    return result


def main():
    input()
    a_list = list(map(int, input().split(' ')))
    dict_a_list = dict()
    dict_result = dict()
    for a in a_list:
        if a in dict_a_list:
            dict_a_list[a] += 1
            continue
        dict_a_list[a] = 1
    for a in a_list:
        result = 0
        if dict_a_list[a] in dict_result:
            print(dict_result[dict_a_list[a]])
            continue
        dict_a_list[a] -= 1
        for v in dict_a_list.values():
            result += cycle(v)
        print(result)
        dict_a_list[a] += 1
        dict_result[dict_a_list[a]] = result


if __name__ == '__main__':
    main()