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):
def comb(n):
    if n <= 1:
        return 0
    return n * (n - 1) // 2


def check(L):
    r = {}
    a = 0
    c = 0
    for i in range(len(L)):
        if a != L[i]:
            if i != 0:
                r[a] = [comb(c), comb(c - 1)]
            a = L[i]
            c = 1 
        else:
            c += 1
    r[a] = [comb(c), comb(c - 1)]
    
    return r

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

D = {}
for i in range(len(A)):
    D[i] = A[i]

D = sorted(D.items(), key=lambda x: x[1])
V = {}
for i in range(len(D)):
    V[D[i][0]] = i

AA = sorted(A)
rr = check(AA)
ss = 0
for k, v in rr.items():
    ss += v[0]


for k in range(N):
    index = A[k]
    a = rr[index][0] - rr[index][1]
    print(ss - a)

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

ball_count = [0] * (n + 1)

for i in a:
    ball_count[i] += 1
def nCr(i):
    if i<=1:
        return 0
    else:
        return i*(i-1)//2

def get_total(ball_count):
    ans = 0
    for i in ball_count:
        ans += nCr(i)
    return ans


total = get_total(ball_count)
for i in range(1, n + 1):
    num = ball_count[a[i - 1]]
    ans = total - nCr(num) + nCr(num-1)
    print(ans)

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

N = int(input())
A = tuple(map(int,input().split()))
cnt = [0]*200005
tot = 0
for i in A:
    cnt[i] += 1
for i in cnt:
    tot += nC2(i)
for i in range(N):
    ans = tot - nC2(cnt[A[i]]) + nC2(cnt[A[i]]-1)
    print(ans)

Solution (Python):
import math
def combination(n,r):
    if r<n:
        o=math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
    else:
        o=1 if r==n else 0
    return o

N=int(input())
A=list(map(int,input().split()))
l=[0]*N
m=[0]*N
s=0
for i in A:l[i-1]+=1
for i in range(N):
    c=combination(l[i],2)
    s+=c
    m[i]=c-combination(l[i]-1,2)
for i in A:print(s-m[i-1])

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

from collections import Counter
cnt = Counter(A)

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

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

from collections import Counter
cnt = Counter(A)

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

Solution (Python):

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

cnt = collections.Counter(A)

combinations = {}
for j in cnt.keys():
    if cnt[j] >= 2:
        comb = (cnt[j]*(cnt[j]-1))//2
        combinations[j] = comb      



tot = sum(combinations.values())
ans = []
for i in A:
    a = tot - (cnt[i] - 1)
    ans.append(a)
    
for k in ans:
    print(k)

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


n=int(input())
arr=[int(i) for i in input().strip().split(" ")]
ans=0
dic={}
for i in arr:
    dic[i]=dic.get(i,0)+1
for i in dic:
    if dic[i]==1 :
        continue
    ans+=nc2(dic[i])
    


for i in range(n):
    curr_nc2=nc2(dic[arr[i]])
    
    print(int(ans-curr_nc2+nc2(dic[arr[i]]-1)))

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


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


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

Solution (Python):
import collections
import math

def c2(x):
  c = math.factorial(x) // (math.factorial(x-2)*2)
  return c

n = int(input())

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

adic = collections.Counter(a)

cnt = 0

for i in adic:
  num = adic[i]
  if num > 1:
    cnt += c2(num)

for i in a:
  num = adic[i]
  if num == 1:
    print(cnt)
  else:
    print(cnt - (num-1))

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

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

S = 0
for i in range(M + 1):
    m = count[i]
    S += (m * (m - 1)) // 2

ans = [S for _ in range(N)]
for i in range(N):
    m = count[A[i]]
    ans[i] -= m - 1

for i in range(N):
    print(ans[i])

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

from collections import Counter
cnt = Counter(A)

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

for a in A:
    k = cnt[a]
    print(ans - (k*(k-1)//2) + (k-1)*(k-2)//2)

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

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

x = 0
for j in range(n):
    x += ((b[j] - 1) * b[j] / 2)

for k in range(n):
    print(int(x - b[a[k] - 1] + 1))

Solution (Python):
import collections

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

c=collections.Counter(a)
s=0

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

for j in a:
  f=c[j]
  print(s-(f-1)) 

'''
複数個のボールから1個のボールが抜かれる場合、i*(i-1)//2が(i-1)*(i-2)//2となる。

i*(i-1)//2-(i-1)*(i-2)//2
=i-1

よって、2つのボールを選ぶ方法が(i-1)だけ減る。
'''

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

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

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

Solution (Python):
from collections import Counter
from operator import mul
from functools import reduce
import sys
input = sys.stdin.readline

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

    ans = [0] * N
    cnt = Counter(A)
    s = 0
    for key, value in cnt.items():
        if value < 2:
            continue
        s += combinations_count(value, 2)
    
    for i in range(N):
        if cnt[A[i]]-1 == 0:
            ans[i] = s
        elif cnt[A[i]]-1 == 1:
            ans[i] = s - combinations_count(cnt[A[i]], 2)
        else:
            ans[i] = s - combinations_count(cnt[A[i]], 2) + combinations_count(cnt[A[i]]-1, 2)
    
    print(*ans, sep="\n")

main()

Solution (Python):
from collections import Counter

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

count = [0]*(N+1)

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

sum_ = sum(map(lambda x: int(x*(x-1)/2), count))

ans = []

for a in A:
    temp = sum_ - (count[a] - 1)
    ans.append(temp)

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

Solution (Python):

import collections

n = int(input())
A = list(map(int, input().split( )))
c = collections.Counter(A)
s = 0
for a in c:
  if c[a] >= 2:
    s += c[a]*(c[a]-1)//2

for a in A:
  if c[a] == 1:
    print(s)
  else:
    print(s - c[a] + 1)

Solution (Python):
import collections

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

c = collections.Counter(A_list)
c_value_list = list(c.values())


ans = 0

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

for k in range(N):
    ans_temp = ans - c[A_list[k]] + 1
    print(ans_temp)

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

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

Solution (Python):
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n = I()
    a = LI()
    dic = collections.Counter(a)
    als = 0
    exl = [0 for _ in range(n+1)]
    for key, value in dic.items():
        if value >= 3:
            choice = (value)*(value-1)//2
            choice1 = (value-1)*(value-2)//2
            als += choice
            exl[key] = choice1 - choice
        elif value == 2:
            choice = (value)*(value-1)//2
            als += choice
            exl[key] = -choice
    
    for ball in a:
        choices = als + exl[ball]
        print(choices)
main()

Solution (Python):

def main():
    from scipy.special import comb

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

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

    ans = 0
    for num in num_lst:
        ans += comb(num, 2, exact=True)

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


if __name__ == '__main__':
    main()

Solution (Python):

def main():
    import time

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

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

    ans = 0
    for num in num_lst:
        
        ans += num * (num - 1) // 2

    start = time.time()
    for a in A:
        print(ans - (num_lst[a] - 1))
    end = time.time() - start
    


if __name__ == '__main__':
    main()

Solution (Python):

def main():
    import time

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

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

    ans = 0
    for num in num_lst:
        
        ans += num * (num - 1) // 2

    start = time.time()
    for a in A:
        print(ans - (num_lst[a] - 1))
    end = time.time() - start
    


if __name__ == '__main__':
    main()

Solution (Python):






N = int(input())



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





















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

cnt = [0] * (N + 1)

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

tot = 0
for i in cnt:
    if i > 1:
        tot += nC2(i)

for i in A:
    j = cnt[i]
    if j < 2:
        print(tot)
    else:
        print(tot - (j - 1))

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

count=[0 for i in range(N)]
for i in A:
    count[i-1]+=1
    
def chose(n):
    return int(n*(n-1)/2)

total=0
for i in count:
    total+=chose(i)

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

Solution (Python):
def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    import collections
    counter = collections.Counter(A)
    total = 0
    for k, v in counter.items():
        total += v*(v-1)//2
    
    for a in A:
        cnt = counter[a]
        cntm1 = cnt - 1
        print(total - cnt*(cnt-1)//2 + cntm1*(cntm1-1)//2)


if '__main__' == __name__:
    resolve()

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

cnt = {}
for i in A:
    if i in cnt:
        cnt[i] += 1
    else:
        cnt[i] = 1
total = 0
for i in cnt:
    if cnt[i]>=2:
        total += cnt[i] * (cnt[i]-1) // 2
        
for i in A:
    if cnt[i] == 1:
        print(total)
    else:
        
        print(total - (cnt[i]-1))

Solution (Python):
def main():
  N=int(input())
  A=[int(a) for a in input().split()]
  cnt={}
  for a in A:
    cnt[a] = cnt.get(a, 0) + 1
  prob=0
  for a in cnt.values():
    prob+=a*(a-1)//2
    
  for a in A:
    print(prob-(cnt[a]-1))
  
main()

Solution (Python):
def main():
  N=int(input())
  A=[int(a) for a in input().split()]
  Acnt, Aprob={}, {}
  for a in A:
    Acnt[a] = Acnt.get(a, 0) + 1
  sumA=0
  for k, a in Acnt.items():
    Aprob[k]=a*(a-1)//2
    sumA+=Aprob[k]
  
  
  

  for a in A:
    ans=(Acnt[a]-1)*(Acnt[a]-2)//2
    print(ans+sumA-Aprob[a])
  
main()

Solution (Python):
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
A = LI()

c = collections.Counter(A)


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

for i in range(N):
    a = c[A[i]]
    print(cnt - (a - 1))

Solution (Python):
from collections import defaultdict

n = int(input())
alst = list(map(int, input().split()))
ans = 0
dd = defaultdict(int)
for a in alst:
    dd[a] += 1
for _, num in dd.items():
    ans += num * (num - 1) // 2
for a in alst:
    num = dd[a]
    ret = ans - num * (num - 1) // 2 + (num - 1) * (num - 2) // 2
    print(ret)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
d=dict([])
ans=0
for a in A:
  if a in d:
    ans+=d[a]
    d[a]+=1
  else:
    d[a]=1
for a in A:
  print(ans-d[a]+1)

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

ans=0
for k in C:
    n=C.get(k)
    ans+=n*(n-1)//2


for i in range(N):
    k=A[i]
    n=C.get(k)
    print(ans-n+1)

Solution (Python):
from collections import Counter

if __name__ == '__main__':

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

    
    d = Counter(A)
    
    d2 = dict()
    
    for k,v in d.items():
        d2[k] = (v*(v-1)) // 2
    ans_sum = sum(d2.values())

    
    for i in A:
        ans = 0
        if i in d2:
            
            tmp = d[i] - 1
            before= d2[i]
            after = (tmp*(tmp-1)) //2
            ans = ans_sum - (before-after)
            print(ans)
        else:
            
            print(ans_sum)

Solution (Python):
import math

def comb(n):
    if n-2<0:
        return 0
    return math.factorial(n)//(math.factorial(2)*math.factorial(n-2))
    
N=int(input())
A=list(map(int,input().split()))

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

num=0
for i in range(len(nums)):
    num+=comb(nums[i])

for i in range(N):
    ans=num-(nums[A[i]]-1)
    print(ans)

Solution (Python):
import collections
N = int(input())
A = list(map(int,input().split()))
Cn_A = collections.Counter(A)
ans_list = [0]*len(Cn_A)
ans = 0
for i,j in enumerate(Cn_A.values()):
    ans+=j*(j-1)//2
for i in A:
    j = Cn_A[i]
    print(ans-(j*(j-1)//2)+((j-1)*(j-2)//2))

Solution (Python):
from collections import Counter
 
def main():
    n = int(input())
    A = [int(x) for x in input().split()]
    a_c = Counter(A)
    res = [int(v*(v-1)/2) for v in a_c.values()]
    cnt = sum(res)
    for a in A:
        ans = (cnt - (a_c[a]-1))
        print(ans)
 
if __name__ == '__main__':
    main()

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

N = int(input())
A = list(map(int, input().split()))
C = Counter(A)
x = 0

for c in C.values():
    x += comb(c, 2, exact=True)

for i in range(N):
    y = x - comb(C[A[i]], 2, exact=True) + comb(C[A[i]] - 1, 2, exact=True)
    print(y)

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N = INT()
    A = LIST()

    C = Counter(A)

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

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


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()

A_c = Counter(A)

A_c_c = 0
for i in A_c.values():
    A_c_c += i*(i-1)//2

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

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect, bisect_left, bisect_right

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()

C = Counter(A)
S = set(A)





total = 0
for s in S:
    total += C[s] * (C[s] - 1) // 2


for i, a in enumerate(A):
    
    exc = total - C[a] * (C[a] - 1) // 2
    a_num = C[a] - 1
    now = exc + a_num * (a_num - 1) // 2
    print(now)

Solution (Python):
import math
def combination(n,r):
      return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
N = int(input())
A = list(map(int,input().split()))
ls = [0 for i in range(N)]
for i in range(N):
  ls[A[i]-1] += 1
summation = 0
for i in range(N):
  if ls[i] >= 2:
    summation += combination(ls[i],2)
for i in range(N):
  if ls[A[i]-1] >= 2:
    print(summation - (ls[A[i]-1] -1))
  else:
    print(summation)

Solution (Python):
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

N = k()
A = l()
Cnt = [0] * (N+1)
for a in A:
    Cnt[a] += 1
for c in Cnt:
    ans += c * (c-1) // 2
Ans = [ans - Cnt[a] + 1 for a in A]
print("\n".join(map(str, Ans)))

Solution (Python):
N = int(input())
A = [0] + list(map(int, input().split()))

num = {}



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


for a in A[1:]:
    if a not in num:
        num[a] = 0
    num[a] += 1

pattern = 0

for n in num:
    pattern += calc(num[n])

for i in range(1, N+1):
    n = num[A[i]]
    ans = pattern - calc(n) + calc(n-1)
    print(ans)

Solution (Python):
from collections import Counter
import sys
from functools import lru_cache
from functools import reduce
from operator import mul
input = sys.stdin.readline



@lru_cache(None)
def cmb(n: int, r: int):
    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


def main():
    _ = int(input().strip())
    A = [int(i) for i in input().strip().split()]
    cnts = Counter(A)
    d = {}
    _sum = 0
    for k, v in cnts.items():
        d[k] = (cmb(v, 2), cmb(v - 1, 2))
        _sum += d[k][0]
    for a in A:
        dif = d[a][0] - d[a][1]
        print(_sum - dif)
    return


if __name__ == "__main__":
    main()

Solution (Python):
from collections import Counter

input()
a = list(map(int, input().split()))
c = Counter(a)
d = {i: (i * (i - 1)) // 2 for i in set(c.values())}
s = sum(d[i] for i in c.values())
for k in d:
    d[k] = s - d[k] + ((k - 1) * (k - 2) // 2)
d[1] = s
for i in a:
    print(d[c[i]])

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


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


N = int(input())
A = list(map(int, input().split()))
dp = [0] * (N + 1)
ans = 0
l = Counter(A)

for sa in set(A):
    if l[sa] < 2:
        continue
    dp[sa] = combinations_count(l[sa], 2)
sum_dp = sum(dp)

for a in A:
    ans = dp[a] * (l[a]-2)
    ans //= l[a]
    ans += sum_dp - dp[a]
    print(ans)

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

Solution (Python):
from math import factorial
N = int(input())
A = list(map(int, input().split()))
n = {}
s = 0
for i in range(N):
    if A[i] not in n:
        n[A[i]] = 1
    else:
        n[A[i]] += 1
for j in n:
    if n[j] > 1:
        s += factorial(n[j]) // 2 // factorial(n[j]-2)
for i in range(N):
    print(s-n[A[i]]+1)

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

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

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

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

S={}
for i in range(n):
    a = A[i]
    if a in S :
        S[a]+=1
    else:
        S[a]=1

V=list(S.values())
c = sum([v*(v-1)//2 for v in V])

cdiff=0
K=[]
for i in range(n):
    cdiff=0
    a = A[i]
    s=S[a]
    if s>=2 :
        cdiff=(s*(s-1)-(s-1)*(s-2))//2
    print(c-cdiff)

Solution (Python):
from collections import Counter




N = int(input())
A_lst = list(map(int,input().split()))
S = Counter(A_lst)
sums = 0



for c, i in S.items():
    sums += i*(i-1)//2

for j in range(1, N + 1):
    if S[A_lst[j-1]] == 2:
        print(sums - 1)
    elif S[A_lst[j-1]] > 2:
        print(int(sums - S[A_lst[j-1]] * (S[A_lst[j-1]]-1) * 0.5 + (S[A_lst[j-1]] - 1) * (S[A_lst[j-1]] - 2) * 0.5))
    else:
        print(sums)

Solution (Python):
from collections import Counter
import math

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

N = int(input())
A_lst = list(map(int,input().split()))
S = Counter(A_lst)
sums = 0




for c, i in S.items():
    sums += i*(i-1)//2

for j in range(1, N + 1):
    if S[A_lst[j-1]] == 2:
        print(sums - 1)
    elif S[A_lst[j-1]] > 2:
        print(int(sums - S[A_lst[j-1]] * (S[A_lst[j-1]]-1) * 0.5 + (S[A_lst[j-1]] - 1) * (S[A_lst[j-1]] - 2) * 0.5))
    else:
        print(sums)

Solution (Python):
from collections import Counter
import math

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

N = int(input())
A_lst = list(map(int,input().split()))
S = Counter(A_lst)
sums = 0
for i in S.keys():
    if S[i] > 1:
        sums += combinations_count(S[i], 2)

for j in range(1, N + 1):
    if S[A_lst[j-1]] == 2:
        print(sums - 1)
    elif S[A_lst[j-1]] > 2:
        print(int(sums - S[A_lst[j-1]] * (S[A_lst[j-1]]-1) * 0.5 + (S[A_lst[j-1]] - 1) * (S[A_lst[j-1]] - 2) * 0.5))
    else:
        print(sums)

Solution (Python):
import collections
n = int(input())
a = list(map(int, input().split()))
c = collections.Counter(a)
d = {}
m = 0
for e in c:
  m += (c[e] * (c[e] - 1) // 2)
  if c[e] == 1:
    d[e] = 0
  elif c[e] == 2:
    d[e] = 1
  else:
    d[e] = (c[e] * (c[e] - 1) // 2) - ((c[e] - 1) * (c[e] - 2) // 2)
for i in range(n):
  print(m - d[a[i]])

Solution (Python):

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


num = [0]*N
for i in range(N):
    num[A[i]-1]+=1




C = [0]*N
cnt=0 

for i in range(N):
    if num[i]!=0 and num[i]!=1:
        dammy = (num[i] * (num[i]-1))//2
        cnt+=dammy
        C[i] = dammy




for i in range(N):
    
    print(cnt - C[A[i]-1] + ((num[A[i]-1]-1)*(num[A[i]-1]-2)) //2)

Solution (Python):
def main():
    n = int(input())
    a = list(map(int,input().split()))
    d = {}
    for i in range(n):
        if d.get(a[i]) == None:
            d[a[i]] = 1
        else:
            d[a[i]] += 1
    ans = 0
    for k in d.keys():
        ans += d[k] * (d[k]-1) //2
    for i in range(n):
        print(ans - ((d[a[i]]*(d[a[i]]-1))//2 - (d[a[i]]-1) * (d[a[i]]-2)//2))

if __name__ == "__main__":
    main()

Solution (Python):
from collections import Counter
def solve():
  N = int(input())
  A = list(map(int, input().split()))
  c = Counter(A)
  cnt = 0
  for k,v in c.items():
    cnt += v*(v-1)//2
  ans = [cnt]*N
  for i in range(N):
    ans[i] -= c[A[i]]*(c[A[i]]-1)//2
    ans[i] += (c[A[i]]-1)*(c[A[i]]-2)//2
  
  return ans
print(*solve(),sep='\n')

Solution (Python):
import copy
from collections import Counter

N = int(input())
K = list(map(int,input().split()))
"""
N = 4
K = [1, 2 ,3 ,4]
"""
a = list(set(K))

a_count = {}



a_count = Counter(K)




ans_dict = 0
for l in a_count.values() :
        
    ans_dict += l * (l-1)  // 2



"""
for i in a_count :

    a_count_2 = copy.deepcopy(a_count)
    
    a_count_2[i] -= 1

    
    
    ans_dict[i] = 0
    for l in a_count_2.values() :
        
        ans_dict[i] += l * (l-1)  // 2


"""

for i in K:
    print(ans_dict - (a_count[i]-1))
    pass

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
Cnt = [0] * (N+1)
for a in A:
    Cnt[a] += 1
ans0 = 0
for c in Cnt:
    ans0 += c * (c-1) // 2
Ans = [ans0 - Cnt[a] + 1 for a in A]
print("\n".join(map(str, Ans)))

Solution (Python):
n = int(input())
lst = [int(i) for i in input().split()]
dic = {}
for i in range(n):
  if lst[i] not in dic:
    dic[lst[i]] = [1, 0, 0]
  else:
    dic[lst[i]][0] += 1
    dic[lst[i]][1] = dic[lst[i]][0] * (dic[lst[i]][0] - 1) // 2
    dic[lst[i]][2] = (dic[lst[i]][0] - 1) * (dic[lst[i]][0] - 2) // 2

count = 0
for value in dic.values():
  count += value[1]
for i in range(n):
  m = lst[i]
  count -= dic[m][1]
  count += dic[m][2]
  print(count)
  count += dic[m][1]
  count -= dic[m][2]

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

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

allc = 0
for a in range(1, N + 1):
    c = dupcount[a]
    if c > 1:
        allc += (c * (c - 1) // 2)

for a in A:
    ans = allc
    c = dupcount[a]
    if c > 0:
        ans -= (c - 1)
    print(ans)

Solution (Python):
import math

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

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

ans = 0
for a in dic:
    if dic[a] > 1:
         ans += dic[a] * (dic[a] - 1) // 2

ansdic = {}

for a in dic:
    ansdic[a] = ans
    if dic[a] > 1:
        ansdic[a] = ans - (dic[a] - 1)

for a in A:
    print(ansdic[a])

Solution (Python):
from collections import Counter

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

c = Counter(a)
total = sum([v*(v-1)//2 for v in c.values()])
for i in a:
    print(total-(c[i]-1))

Solution (Python):

import sys
from collections import Counter
input = lambda: sys.stdin.readline().strip()
n = int(input())
a = [int(x) for x in input().split()]
c = Counter(a)
def c2(n):
    return n * (n - 1) // 2

ans = sum(c2(v) for v in c.values())

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

Solution (Python):

import sys
from collections import Counter
input = lambda: sys.stdin.readline().strip()
n = int(input())
a = [int(x) for x in input().split()]
c = Counter(a)
def c2(n):
    return n * (n - 1) // 2

ans = sum(c2(v) for v in c.values())

def add(x):
    global ans
    ans -= c2(c[x])
    c[x] += 1
    ans += c2(c[x])

def remove(x):
    global ans
    ans -= c2(c[x])
    c[x] -= 1
    ans += c2(c[x])

for x in a:
    remove(x)
    print(ans)
    add(x)

Solution (Python):
def combination(n,r):
    n_=1
    r_=1
    for i in range(n,n-r,-1):
        n_*=i
    for i in range(1,r+1):
        r_*=i

    return n_//r_

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

    ball_dic={}

    for a in A:
        if a in ball_dic.keys():
            ball_dic[a]+=1
        else:
            ball_dic[a]=1


    all_patern=0

    combination_dic={}

    for key in ball_dic.keys():
        if ball_dic[key]>=2:
            if (ball_dic[key] in combination_dic.keys()) is False:
                combination_dic[ball_dic[key]] = combination(ball_dic[key], 2)
            all_patern += combination_dic[ball_dic[key]]

    for a in A:
        if ball_dic[a]-1<2:
            if ball_dic[a] in combination_dic.keys():
                res=all_patern-combination_dic[ball_dic[a]]
            else:
                res=all_patern
        else:
            com=combination_dic[ball_dic[a]]
            res=all_patern-com+(com*(ball_dic[a]-2)//ball_dic[a])

        print(res)

if __name__=="__main__":
    main()

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

kosuu=[0]*(n+10)
kumiawase=[0]*(n+10)


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

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

ans=sum(kumiawase)
for i in range(1,n+1):
    print(int(ans-(kosuu[a[i]]-1)))

Solution (Python):
import collections
import math

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

def com(n, r):
    if n > 1:
        ann = math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
    else:
        ann = 0
    return ann

b = collections.Counter(a)
ans = []
for i in b:
    ans.append(com(b[i],2))
anss = sum(ans)

for i in range(n):
    t = b[a[i]]
    print(anss - (t-1))

Solution (Python):
def solve():
    import collections
    N = int(input())
    A = [int(i) for i in input().split()]
    counter = collections.Counter(A)
    combis = {}
    total = 0
    for k,v in counter.items():
        combi = v * (v-1) // 2
        combis[k] = combi
        total += combi
    for i in range(N):
        cnt = counter[A[i]]
        if cnt > 1:
            combi = (cnt-1) * (cnt-2) // 2
        else:
            combi = 0
        print(total - combis[A[i]] + combi)

if __name__ == "__main__":
    solve()