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):
from collections import Counter
import sys

readline = sys.stdin.readline
n = int(readline())
a = tuple(map(int, readline().split()))

counter = Counter(a)
c = [0] * n
for k, v in counter.items():
    c[k-1] = v


def comb(n):
    r = 2
    numerator = denominator = 1
    for i in range(r):
        numerator = numerator * (n - i)
        denominator = denominator * (i + 1)
    return numerator // denominator


combination = tuple(map(comb, c))
comb_sum = sum(combination)
for i in a:
    i -= 1
    if c[i] > 0:
        print(comb_sum - (c[i] - 1))
    else:
        print(comb_sum)

Solution (Python):
import math
from collections import deque
def main():
    n = int(input())
    a = [int(t)for t in input().split()]

    vote = {}
    for a_ in a:
        if a_ in vote.keys():
            vote[a_] += 1
        else:
            vote[a_] = 1

    combi = {}

    result = 0
    for a_,num in vote.items():
        
        result += num*(num-1)//2

    for a_ in a:
        res = result-(vote[a_]-1)
        print(res)

if __name__ == "__main__":
    main()

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

dic = {}
for i in range(n):
    if str(a[i]) not in dic:
        dic[str(a[i])] = 1
    else:
        dic[str(a[i])] += 1

dic2 = {}
maxi = 0
for key, num in dic.items():
    temp = 0
    if num>=2:
        temp = (num)*(num-1)//2
    dic2[key] = temp
    maxi += temp

for k in range(n):
    temp = maxi
    num = dic[str(a[k])]
    nCr = 0
    if num-1>=2:
        nCr = (num-1)*(num-2)//2
    print(temp - dic2[str(a[k])] + nCr)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
    
fac = [-1] * (10**7+1)
inv = [-1] * (10**7+1)
finv = [-1] * (10**7+1)

fac[0] = fac[1] = 1
inv[1] = 1
finv[0] = finv[1] = 1

def initNCMMod(limit):
    for i in range(2, limit):
        fac[i] = fac[i-1] * i

def NCM(n):
    return n * (n-1) // 2
def main():
    N = I()
    A = LI()
    vals = [0 for _ in range(N+1)]
    pick = [0 for _ in range(N+1)]
    for a in A:
        vals[a] += 1
    for i, val in enumerate(vals):
        pick[i] = NCM(val)
    picksum = sum(pick)
    for a in A:
        tmp_picksum = picksum - NCM(vals[a])
        tmp_picksum += NCM(vals[a]-1)
        print(tmp_picksum)


main()

Solution (Python):
import collections

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

S = 0
c = collections.Counter(A)
for v in c.values():
    S = S + v*(v-1)//2
for i in range(len(A)):
    print(S-c[A[i]]+1)

Solution (Python):
from collections import defaultdict

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

ans = [0 for _ in range(N+1)]
dic = defaultdict(int)
for a in A:
    dic[a]+=1
for k in range(N+1):
    ans[k] = dic[k]*(dic[k]-1)//2

s = sum(ans)
for i in range(N):
    a = A[i]
    print(s-ans[a]+(dic[a]-1)*(dic[a]-2)//2)

Solution (Python):



import sys
sys.setrecursionlimit(10000000)


dxdy=((1,0),(0,1))

def pin(type=int):
    return map(type,input().rstrip().split())

def resolve():
    N,=pin()
    A=list(pin())
    from collections import Counter as ct
    a=ct(A)
    t=(a.items())
    
    
    Sum=0
    for l in a.values():
        Sum+=(l*(l-1))//2 
    ans=[0]*N
    for j in a.items():
        x,y=j
        ans[x-1]=Sum-((y*(y-1))//2)+(((y-2)*(y-1))//2)     
    for i in A:
        print(ans[i-1])
"""         


"""
"""
お前カッコ閉じるの忘れてるだろ
"""
import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """5
1 1 2 1 2"""
        output = """2
2
3
2
3"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """4
1 2 3 4"""
        output = """0
0
0
0"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """5
3 3 3 3 3"""
        output = """6
6
6
6
6"""
        self.assertIO(input, output)

    def test_入力例_4(self):
        input = """8
1 2 1 4 2 1 4 1"""
        output = """5
7
5
7
7
5
7
5"""
        self.assertIO(input, output)


if __name__=="__main__":resolve()

Solution (Python):

import sys
import collections

def solve(N: int, A: "List[int]"):
    dictionary = collections.defaultdict(int)
    for a in A:
        dictionary[a] += 1

    counted = collections.defaultdict(int)
    for k, v in dictionary.items():
        counted[k] = v*(v-1)//2
    summarized = sum(counted.values())

    for a in A:
        v = dictionary[a]
        result = summarized - v*(v-1)//2 + (v-1)*(v-2)//2
        print(result)
    return



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

if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter
n = int(input())
A = list(map(int, input().split()))
Ca = {k:v for k, v in Counter(A).items() if v>=2}
ans = {}
al = 0
for k, v in Ca.items():
    al += v*(v-1)//2
for k, v in Ca.items():
    ans[k] = al - v*(v-1)//2 + (v-1)*(v-2)//2
for a in A:
    if a in ans:
        print(ans[a])
    else:
        print(al)

Solution (Python):
import collections as c
n=int(input())
A=list(map(int,input().split()))
D=c.Counter(A)
S=0
for i in D:
    x=D[i]
    S+=x*(x-1)//2
for a in A:
    print(S-D[a]+1)

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

n = int(sys.stdin.readline().strip())
a = [int(s) for s in sys.stdin.readline().strip().split()]
c = Counter(a)

total = 0
for key, val in c.items():
    total += val * (val-1) // 2

for i in range(n):
    ai = a[i]
    ai_count = c[ai]

    original = ai_count * (ai_count-1) // 2
    updated = (ai_count-1) * (ai_count-2) // 2
    print(total - original + updated)

Solution (Python):
N = int(input())
A_dic = {}
A_lis = [int(x) for x in input().split()]
sum = 0

for n in range(N):
    if A_lis[n] in A_dic.keys():
        A_dic[A_lis[n]] += 1
    else:
        A_dic[A_lis[n]] = 1


for key,val in A_dic.items():
    sum += val*(val-1)

sum = sum//2



for n in range(N):
    print(sum - (A_dic[A_lis[n]] - 1))

Solution (Python):
from collections import Counter

N = int(input())
A = list(map(int,input().split()))
H = Counter(A)
K = {}

for k,v in H.items():
    K[k] = v*(v-1)//2
total = sum(K.values())

for i in A:
    print(total - K[i] + (H[i] - 1)*(H[i]-2)//2)

Solution (Python):
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
c=Counter(a)
t=0
for k,v in c.items():
    t+=v*(v-1)//2
for i in range(n):
    ans=t
    x=c[a[i]]
    ans-=x*(x-1)//2
    x-=1
    ans+=x*(x-1)//2
    print(ans)

Solution (Python):
import collections

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

c=collections.Counter(a)

su=0
an={}
for k in c.keys():
    tmp=c[k]*(c[k]-1)//2
    su+=tmp
    an[k]=(c[k]*(c[k]-1)-(c[k]-1)*(c[k]-2))//2

for o in a:
    print(su-an[o])

Solution (Python):
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
c = Counter(a)
all = 0
for i in c.values():
    all += (i*(i-1))//2
for j in range(n):
    print(all-(c[a[j]]-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

s = 0
for k in d:
    s += d[k] * (d[k] - 1) // 2

for i in range(N):
    t = A[i]
    result = s
    result -= d[t] * (d[t] - 1) // 2
    result += (d[t] - 1) * (d[t] - 2) // 2
    print(result)

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

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

M = [0 for _ in range(N+1)]
C = [0 for _ in range(N+1)]
_C = [0 for _ in range(N+1)]

for i in range(N):
    M[A[i]] += 1
for i in range(N):
    cnt = M[A[i]]
    C[A[i]] = comb(cnt)
    _C[A[i]] = comb(cnt-1)

S = sum(C)
for i in range(N):
    print(S - C[A[i]] + _C[A[i]])

Solution (Python):
from collections import defaultdict
n, *A = map(int, open(0).read().split())
d = defaultdict(int)
for a in A:
    d[a] += 1
s = 0
for v in d.values():
    s += v*(v-1)//2
for a in A:
    print(s-d[a]*(d[a]-1)//2+(d[a]-1)*(d[a]-2)//2)

Solution (Python):

import collections

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

ct = collections.Counter(A)

ret = []
total = 0
for k,count in ct.items():
    if count > 1:
        total += count * (count-1) // 2

for i,v in enumerate(A):
    if ct[v] > 1:
        count = ct[v]
        adjust = (count-1) * (count-2) // 2 - (count-1) * count // 2
        val = total + adjust
    else:
        val = total
    print(val)

Solution (Python):

import sys
import collections

def solve(N: int, A: "List[int]"):
    dictionary = collections.defaultdict(int)
    for a in A:
        dictionary[a] += 1

    counted = collections.defaultdict(int)
    for k, v in dictionary.items():
        counted[k] = v*(v-1)//2
    summarized = sum(counted.values())

    
    for a in A:
        v = dictionary[a]
        
        result = summarized - v*(v-1)//2 + (v-1)*(v-2)//2
        print(result)
    return



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

if __name__ == '__main__':
    main()

Solution (Python):
L = int(input())
nums = list(map(int,input().split(" ")))
di = {}
for num in nums:
    if num not in di:
        di[num] = 1
    else:
        di[num] += 1
total = 0
minus = {}
for k,v in di.items():
    total += v * (v-1) //2
    minus[k] = v * (v-1) //2 -  max( (v - 2) * (v-1)//2,0)
for num in nums:
    print(total - minus[num])

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

def conb(n,r): 
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
n = inp()
a = inpl()
c = Counter(a)
res = 0
d = dict()
for key in list(c):
    
    if c[key] > 1:
        tmp = conb(c[key], 2)
        res += tmp
        d[key] = tmp
    else:
        d[key] = 0
for i,t in enumerate(a):
    if d[t] == 0:
        print(res)
        continue
    now = d[t] * max(0,c[t]-2) // c[t]
    
    print(res - d[t] + now)

Solution (Python):
import sys
from collections import Counter

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

n = ni()
l = nl()
d = Counter(l)
ans = sum(x*(x-1)//2 for x in d.values())
for x in l:
    print(ans - d[x] + 1)

Solution (Python):

import sys


def solve(N: int, A: "List[int]"):
    ns = [0 for _ in range(N)]
    for a in A:
        ns[a - 1] += 1
    
    ls = [int(ns[i] * (ns[i] - 1) / 2) for i in range(N)]
    sm = sum(ls)

    for a in A:
        print(int(sm - ls[a - 1] + (ns[a - 1] - 1) * (ns[a - 1] - 2) / 2))




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

if __name__ == '__main__':
    main()

Solution (Python):
def fun(a):
    return a*(a-1)//2
import sys
def main():
    n = int(input())
    l = [int(j) for j in sys.stdin.readline().split()]
    d = dict()
    for i in range(n):
        if l[i] in d:
            d[l[i]]+=1
        else:
            d[l[i]]=1
    
    ans = 0
    for key in d:
        ans+=fun(d[key])
    
    
    for i in range(n):
        if d[l[i]]==1:
            print(ans)
        else:
            print(ans - fun(d[l[i]]) + fun(d[l[i]]-1))
main()

Solution (Python):
from collections import Counter

n = int(input())
seq = [int(x) for x in input().split()]
c = Counter(seq)
d = dict()
total = 0
for val in c:
    d[(val,0)] = ((c[val]-1) * (c[val]-1- 1)) >> 1
    d[(val,1)] = (c[val] * (c[val]- 1)) >> 1
    total += d[(val,1)]

for i in range(n):
    val = seq[i]
    print(total - d[(val,1)] + d[(val,0)])

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

buk = {}
for x in a:
  if x in buk:
    buk[x] += 1
  else:
    buk[x] = 1

c = {}

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


ansall = 0
for _, z in buk.items():
  if z >= 2:
    ansall += cmb(z, 2)

for x in a:
  if buk[x] - 1 >= 2:
    print(ansall - cmb(buk[x], 2) + cmb(buk[x] - 1, 2))
  elif buk[x] == 2:
    print(ansall - 1)
  else:
    print(ansall)

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

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

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

for i in a:
    print(total - a_dict[i] + 1)

Solution (Python):
from sys import stdin
from sys import setrecursionlimit
from collections import Counter
setrecursionlimit(1000000)


INF = int(1e10)

def input():
    return stdin.readline()[:-1]

def main():
    from builtins import int, map
    N = int(input())
    A = list(map(int, input().split()))
    cA = Counter(A)

    diff = {}
    total = 0
    for key in cA:
        v = cA[key]
        total += (v * (v - 1)) // 2
        diff[key] = (v - 1) * (v - 2) // 2 - (v * (v - 1)) // 2

    for ai in A:
        print(total + diff[ai])
        
        
        
        
        
        
        

if __name__ == '__main__':
    main()

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

num_count = {}
for num in nums:
    num_count[num] = num_count.get(num, 0) + 1

total = 0
for num, ele in num_count.items():
    total += (ele * (ele - 1)) // 2

ans = []

for num in nums:
    ans.append(total - num_count[num] + 1)
print("\n".join(list(map(str, ans))))

Solution (Python):
import sys 
input = sys.stdin.readline
MOD = 10**9 +7
INF = 10**9
from bisect import bisect_right

def main():
    n = int(input())
    a = list(map(int,input().split()))
    dic = {}
    for num in a:
        if num in dic:
            dic[num] += 1
        else:
            dic[num] = 1
    
    tot = 0
    for v in dic.values():
        if v >= 2:
            tot += v * (v - 1)//2
    
    for num in a:
        if dic[num] == 1:
            print(tot)
        elif dic[num] == 2:
            print(tot - 1)
        else:
            l = dic[num]
            print(tot + 1 - l)        



if __name__=='__main__':
    main()

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b={}
for i in a:
  b[i]=b.get(i,0)+1
s=sum([i*(i-1)//2 for i in b.values()])
for i in a:
  print(s-b[i]+1)

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

summary = {}
for a in As:
    if a in summary:
        summary[a] += 1
    else:
        summary[a] = 1

n_comb = {}
for k in summary:
    if 2<=summary[k]:
        n_comb[k] = summary[k]*(summary[k]-1)//2
    else:
        n_comb[k] = 0

x_comb = {}
total = sum([n_comb[k] for k in n_comb])        
for k in summary:
    if 3<=summary[k]:
        x_comb[k] = total - n_comb[k] + (summary[k]-1)*(summary[k]-2)//2
    else:
        x_comb[k] = total - n_comb[k]


for a in As:
    print(x_comb[a])

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
cnt = [0] * (N + 1)
total = 0
for i in range(N):
    total += cnt[A[i]]
    cnt[A[i]] += 1
for i in range(N):
    ans = total - (cnt[A[i]] - 1)
    print(ans)

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

cnt = [0 for _ in range(n)]
cnt_max = [0 for _ in range(n)]
cnt_mini = [0 for _ in range(n)]

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

for i in range(n):
    cnt_max[i] = cnt[i] * (cnt[i] - 1) // 2
    if cnt[i]!=0:
        cnt_mini[i] = (cnt[i] - 1) * (cnt[i] - 2) // 2

s = sum(cnt_max)
for i in range(n):
    print(s - cnt_max[a[i]-1] + cnt_mini[a[i]-1])

Solution (Python):
import unittest


class TestD(unittest.TestCase):
    def test_1(self):
        self.assertEqual(
            think([1, 1, 2, 1, 2]),
            [2, 2, 3, 2, 3]
        )

    def test_2(self):
        self.assertEqual(
            think([1, 2, 3, 4]),
            [0, 0, 0, 0]
        )

    def test_3(self):
        self.assertEqual(
            think([3, 3, 3, 3, 3]),
            [6, 6, 6, 6, 6]
        )

    def test_4(self):
        self.assertEqual(
            think([1, 2, 1, 4, 2, 1, 4, 1]),
            [5, 7, 5, 7, 7, 5, 7, 5]
        )


def solve():
    a = read()
    result = think(a)
    write(result)


def read():
    n = read_int(1)[0]
    return read_int(n)


def read_int(n):
    return read_type(int, n, sep=' ')


def read_float(n):
    return read_type(float, n, sep=' ')


def read_type(t, n, sep):
    return list(map(lambda x: t(x), read_line().split(sep)))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(a):
    hist = make_hist(a)
    num_of_pair = calc_num_of_pair(hist)
    sum_of_num_of_pair = 0

    result = [0] * len(a)

    for k, v in num_of_pair.items():
        sum_of_num_of_pair += v

    for i, e in enumerate(a):
        if hist[e] <= 1:
            result[i] = sum_of_num_of_pair
        else:
            n = hist[e]
            result[i] = sum_of_num_of_pair - num_of_pair[e]
            result[i] += ((n - 1) * (n - 2)) // 2
    return result


def make_hist(a):
    hist = {}
    for e in a:
        hist[e] = hist.get(e, 0) + 1
    return hist


def calc_num_of_pair(hist):
    num_of_pair = {}
    for k, v in hist.items():
        if v <= 1:
            num_of_pair[k] = 0
        else:
            num_of_pair[k] = (v * (v - 1)) // 2
    return num_of_pair


def write(result):
    for r in result:
        print(r)


if __name__ == '__main__':
    
    solve()

Solution (Python):
from collections import Counter

def main():
    input()
    a = [int(i) for i in input().split()]
    c = Counter(a)
    s = sum(v * (v - 1) // 2 for v in c.values())
    for i in a:
        print(s - (c[i] - 1))

if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter
def comb(n):
    return n*(n-1)//2
def main():
    n = int(input())
    A = list(map(int,input().split()))
    C = Counter(A)
    mx = sum(comb(b) for b in C.values())
    for a in A:
        print(mx-C[a]+1)
main()

Solution (Python):
n = int(input())
a_array = [int(x) for x in input().split()]
d = dict()
ans_d = dict()
for a in a_array:
    d.setdefault(a, 0)
    d[a] += 1
for key, value in d.items():
    ans_d[key] = value*(value-1)//2

sum_count = sum(ans_d.values())
ans = []
for a in a_array:
    tmp = d[a] - 1
    tmp_count = sum_count -ans_d[a]
    ans.append(tmp_count + tmp*(tmp-1)//2)

for a in ans:
    print(a)

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

c = collections.Counter(a)
d = {}

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


for i in a:
    print(sum_num-d[i])

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


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


for aa in a:
    print(int(al - c[aa]+1))

Solution (Python):
from collections import Counter

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

cmbA = {}
for key, value in cntA.items():
    cmbA[key] = value * (value - 1) // 2

sum_cmbA = sum(cmbA.values())

for n in range(N):
    if cntA[A[n]] == 1:
        print(sum_cmbA)
    else:
        print(sum_cmbA - (cntA[A[n]] - 1))

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

counts = {}
for e in A:
    if e not in counts:
        counts[e] = 0
    counts[e] += 1

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

for i in range(0, len(A)):
    print(ans - counts[A[i]] + 1)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
x=dict()
for i in range(N):
    if A[i] in x:
        x[A[i]]+=1
    else:
        x[A[i]]=1
sm=0
for idx in x:
    sm+=((x[idx]-1)*x[idx])//2
    
ans=[]

for i in range(N):
    a=sm-(x[A[i]]-1)
    ans.append(a)
for i in range(N):
    print(ans[i])

Solution (Python):
from collections import Counter

n = int(input())
nums = list(map(int, input().split()))
length = len(nums)
num_dict = Counter(nums)
all_count = 0
for i in num_dict.keys():
    all_count += ((num_dict[i]-1) * num_dict[i]) //2
    
for i in range(length):
    print(all_count - (num_dict[nums[i]]-1))

Solution (Python):
from collections import defaultdict

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

yag = defaultdict(int)

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

result = 0
for i in yag.keys():
    t = yag[i]
    result += (t*(t-1))//2

for i in a:
    t = yag[i]
    print(result - (t*(t-1))//2 + ((t-1)*(t-2))//2)

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

cnt = [0 for i in range(n + 1)]

for _a in a:
  cnt[_a] += 1

ans = 0
for _cnt in cnt:
  ans += _cnt * (_cnt - 1) // 2

for _a in a:
  print(ans - cnt[_a] + 1)

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

freq_dic = Counter(ls)
ans = 0
for key,val in freq_dic.items():
    if val>1:
        ans += (val*(val-1))//2
for val in ls:
    currans = ans
    if freq_dic[val]>1:
        tempval = freq_dic[val]
        tempans = (tempval*(tempval-1))//2
        newans = 0
        if tempval>2:
            newans += ((tempval-1)*(tempval-2))//2
        currans -= tempans
        currans += newans
    print(currans)

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

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

for i in a:
    if i in dic:
        print(total-dic[i]+1)
    else:
        print(total)

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

for i in range(N):
    num = numofnum[a[i]-1]
    x =num*(num-1)//2
    y = (num-1)*(num-2)//2
    print(k-x+y)

Solution (Python):
from collections import Counter

N = int(input())
A = [int(a) for a in input().split()]
count = Counter(A)
all_sum = 0
for key, val in count.items():
    all_sum += val * (val - 1)
all_sum //= 2

for k in range(N):
    num = count[A[k]]
    before = num * (num - 1) // 2
    after = (num - 1) * (num - 2) // 2
    print(all_sum - before + after)

Solution (Python):
def MI():return map(int,input().split())
def LI():return list(MI())

from collections import Counter

n=int(input())
a=LI()
c=Counter(a)


M=0
for i,j in c.items():
    M+=(j)*(j-1)//2

for i in a:
    t=c[i]
    d=(t-1)
    print(M-d)

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

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
a = na()

import collections
c = collections.Counter(a)

k = 0

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

for j in range(n):
    b = a[j]
    d = 0 if c[b] < 2 else 1 if c[b] == 2 else c[b]-1
    print(k-d)

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

c = Counter(A)

all_comb = 0

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

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

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

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

balls = [0] * (N + 1)

for a in A_list:
	balls[a] += 1

counts = [0] * (N + 1)
total = 0

for i in range(1, N + 1):
	ball = balls[i]
	count = ball * (ball - 1) // 2
	total += count
	counts[i] = count

for i in range(N):
	a = A_list[i]
	ball = balls[a] - 1
	count = ball * (ball - 1) // 2
	print(total - counts[a] + count)

Solution (Python):
n=int(input())
ls=list(map(int,input().split()))
dic={}
for i in ls:
    dic.setdefault(i,0)
    dic[i]+=1
org=0
for v in dic.values():
    org += v*(v-1)//2

for i in ls:
    v = dic[i]
    print(org-(v*(v-1)//2)+((v-1)*(v-2)//2))

Solution (Python):

from collections import Counter


def solve(*args: str) -> str:
    n = int(args[0])
    A = list(map(int, args[1].split()))
    counts = Counter(A)
    memo = [0]*(n+1)
    total = 0
    for i, c in counts.items():
        memo[i] = c*(c-1)//2
        total += memo[i]

    ret = []
    for a in A:
        tmp = total - memo[a] + (memo[a]*(counts[a]-2)//counts[a])
        ret.append(str(tmp))

    return '\n'.join(ret)


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

Solution (Python):
import sys

def input(): return sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)

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

all_cnt = 0
for k, v in c.items():
    if v>1:
        all_cnt += nC2(v)

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

Solution (Python):
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()


def summarize_list(l):
  sl=sorted(l)

  a=sl[0]
  c=1
  res=[]

  for x in sl[1:]:
    if x==a:
      c+=1
    else:
      res.append([a,c])
      a=x
      c=1
  res.append([a,c])

  return res



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


def main():
  n=I()
  l=LI()
  mp=[0]*(n+1)

  sl=summarize_list(l)
  

  ans=0
  for x,c in sl:
    mp[x]=c
    ans+=nCr(c)

  
  

  for x in l:
    print(ans-nCr(mp[x])+nCr(mp[x]-1))

main()

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

total = 0

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

for a in A:
    v = freq[a]
    diff = -int(v*(v-1)/2) + int((v-1)*(v-2)/2)
    print(total+diff)

Solution (Python):
from collections import Counter
N = int(input())
*A, = map(int, input().split())
C = Counter(A)
D = {}
for key in C.keys():
    D[key] = C[key] * (C[key] - 1) // 2
S = sum(D.values())
for k in range(N):
    print(S - C[A[k]] + 1)

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

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

ans = 0
ans_li = [0 for i in range(N+1)]
for i in range(N):
    ans += max(0,(cnt[i]*(cnt[i]-1))//2)
    ans_li[i] = max(0,(cnt[i]*(cnt[i]-1))//2)


for i in range(N):
    mm = cnt[A[i]-1]-1
    mm2 = ans-ans_li[A[i]-1]
    mm3 = max(0,mm*(mm-1)//2)
    
    print(mm2+mm3)

Solution (Python):
n = int(input())
A = list(map(int,input().split()))
dp = [0]*n
for i in range(n):
    dp[A[i]-1] += 1
sub = 0
for i in range(n):
    if dp[i] != 0:
        sub += dp[i]*(dp[i]-1)//2
for i in range(n):
    ans = sub
    k = A[i]
    ans -= dp[k-1]*(dp[k-1]-1)//2
    if dp[k-1] == 1:
        print(ans)
    else:
        print(ans+(dp[k-1]-1)*(dp[k-1]-2)//2)

Solution (Python):
n = int(input())
d = {}
astr = list(map(int, input().split()))
for i in astr:
    d[i] = d.get(i, 0) + 1

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

s = sum(f(i)  for i in d.values())


for i in astr:
    t = d[i]
    print(s - f(t) + f(t-1))

Solution (Python):
n,*a,=map(int,open(0).read().split())
d={}
for i in a:
    d[i]=d.get(i,0)+1
b=0
for i in d.values():
    b+=i*(i-1)//2
for i in a:
    print(b-d[i]+1)

Solution (Python):
from collections import Counter

N = int(input())
A = [int(a) for a in input().split()]
count = Counter(A)
all_sum = 0
for key, val in count.items():
    all_sum += val * (val - 1)
all_sum //= 2

for k in range(N):
    num = count[A[k]]
    before = num * (num - 1) // 2
    after = (num - 1) * (num - 2) // 2
    print(all_sum - before + after)

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

cnt = {}
for i, Ai in enumerate(A):
    key = str(Ai)
    cnt[key] = cnt.setdefault(key, 0) + 1

tab_full = {}
tab_sub  = {}
for key, val in cnt.items():
    if val == 0: continue
    tab_full[key] = val * (val - 1) // 2
    tab_sub[key] = (val - 1) * (val - 2) // 2

ans_base = sum(tab_full.values())
for i, Ai in enumerate(A):
    key = str(Ai)
    ans = ans_base
    ans -= tab_full[key]
    ans += tab_sub[key]
    print(ans)

Solution (Python):
N = int(input())
d = {}
for i in range(1,N+1):
    d[i] = 0
A = list(map(int,input().split()))
for i in range(N):
    a = A[i]
    d[a] += 1
s = 0
for i in range(1,N+1):
    x = d[i]
    s += (x*(x-1))//2
for i in range(N):
    y = A[i]
    z = s - d[y] + 1
    print(z)

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

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

for ai in a:
  if not a_pop.get(ai):
    a_pop[ai] = 1
  else:
    a_pop[ai] += 1

hoge = sum(v * (v - 1) // 2 for v in a_pop.values())

for ai in a:
  ans = hoge
  v = a_pop[ai]
  print(ans - (v - 1))