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

Solution (Python):



import sys
from collections import Counter

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    N = i2n()
    A = i2nn()
    cntr = Counter()
    c_dict = {}
    for a in A:
        cntr[a] += 1
    sum_c = 0
    for k in cntr:
        n = cntr[k]
        c = n * (n - 1) // 2 if n > 1 else 0
        sum_c += c
        c_dict[k] = c
    for i in range(N):
        k = A[i]
        n = cntr[k] - 1
        c = n * (n - 1) // 2 if n > 1 else 0
        print(sum_c - c_dict[k] + c)
    return

main()

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

ai = [False for _ in range(n)]

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



dic = {}

for i in ai:
    if(i in dic):
        dic[i] += 1
    else:
        dic[i] = 1
mx = 0
for i in dic:
    if(dic[i] < 2):
            continue
    else:
        mx += int(dic[i]*(dic[i]-1)/2)

for i in ai:
    if(dic[i] < 2):
        print(mx)
    else:
        res = mx - (dic[i]-1)
        print(int(res))

Solution (Python):
n=input()
t=map(int,raw_input().split())
l=[0]*(2*pow(10,5)+4)
for i in t:
    l[i]+=1
s=0
for x in l:
    s+=(x*(x-1))/2
for x in t:
    a=l[x]
    print s- (a*(a-1))/2 + (a-1)*(a-2)/2

Solution (Python):
import collections
def main():
    n = int(input())
    a = list(map(int,input().split()))
    cnt_ary = [0] * n

    for i,aa in enumerate(a):
        cnt_ary[aa-1] += 1


    tmp = 0
    ans_ary = [0] * n
    for i, cc in enumerate(cnt_ary):
        ans_ary[i] = cc * (cc-1)

    t = sum(ans_ary)
    ans = 0
    for i, aa in enumerate(a):
        ans = 0
        ans = t - ans_ary[aa-1]
        ans += (cnt_ary[aa-1] - 1) * (cnt_ary[aa-1] - 2)
        print(ans//2)



if __name__=='__main__':
    main()

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



for i in range(n):
    t=b[a[i]]
    s=t*(t-1)//2
    r=(t-1)*(t-2)//2

    print(p-s+r)

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

memo = {}
def ncr(n, r):
    if r > n:
        return 0
    key = "_".join(map(str, (n, r)))
    if key in memo:
        return memo[key]

    ans = factorial(n) // factorial(r) // factorial(n - r)
    memo[key] = ans
    return ans

N = int(sys.stdin.readline().strip())
A = list(sys.stdin.readline().strip().split())

counter = Counter(A)
sum_ans = 0
for key in counter:
    sum_ans += ncr(counter[key], 2)

for a_i in A:
    print(sum_ans - ncr(counter[a_i], 2) + ncr(counter[a_i] - 1, 2))

Solution (Python):
n = int(input())
ls = list(map(int,input().split()))
import collections
co = collections.Counter(ls)
ans = 0
for idx,num in co.items():
    ans +=num*(num-1)//2
for i in range(n):
    idx = ls[i]
    num = co[idx]
    if num == 1:
        print(ans)
    else:
        print(ans - num*(num-1)//2 + (num-1)*(num-2)//2)

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

nums = {}

for a in A:
    if a in nums:
        nums[a] += 1
    else:
        nums[a] = 1

pats = {}
for key in nums:
    pats[key] = nums[key] * (nums[key]-1) // 2

sum = 0
for key in pats:
    sum += pats[key]


for a in A:
    if pats[a] == 1:
        print(sum - 1)
    elif pats[a] == 0:
        print(sum)
    else:
        print(sum + (1-nums[a]))

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

dp = {}
for a in An:
    if not a in dp:
        dp[a] = 1
    else:
        dp[a] += 1
count = 0
for v in dp.values():
    if v > 1:
        count += v*(v-1)//2
for a in An:
    v = dp[a] 
    diff = 0
    if dp[a] > 1:
        diff = (v*(v-1)//2) - ((v-1)*(v-2)//2)
    print(count-diff)

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

count_dict = collections.Counter(A)
c_list = [0]*(N+1)
ans_list = [-1]*(N+1)
ans = [0]*(N)

for k,v in count_dict.items():
    c_list[k] = max(v*(v-1)/2,0)
S = sum(c_list)

for i in range(N):
    x = A[i]
    if ans_list[x] != -1:
        ans[i] = ans_list[x]
        continue
    v = count_dict[x]
    tmp = S - c_list[x] + max((v-2)*(v-1)/2,0)

    ans_list[x] = int(tmp)
    ans[i] = int(tmp)

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

Solution (Python):

import numpy as np
from functools import *
import sys
sys.setrecursionlimit(100000)
input = sys.stdin.readline




def acinput():
    return list(map(int, input().split(" ")))


def II():
    return int(input())

directions=np.array([[1,0],[0,1],[-1,0],[0,-1]])
directions = list(map(np.array, directions))

mod = 10**9+7


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact


ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'

res=[]
def search(seq,k,n):
    global N
    
    if k==N+1:

        tmp=[ascii_lowercase[s-1] for s in seq[1:]]
        res.append("".join(tmp))
        return 
    
    seq[k]=n
    search(seq,k+1,n+1)
    
    for l in range(1,n):
        seq[k] = l
        search(seq, k+1,n)
    

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

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2, r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return int(result)


def nCr(n, r):
    """
    Calculate the number of combination (nCr = nPr/r!).
    The parameters need to meet the condition of n >= r >= 0.
    It returns 1 if r == 0, which means there is one pattern
    to choice 0 items out of the number of n.
    """

    
    r = min(r, n-r)

    
    numerator = 1
    for i in range(n, n-r, -1):
        numerator *= i

    
    denominator = 1
    for i in range(r, 1, -1):
        denominator *= i

    return numerator // denominator


N=int(input())
A=acinput()


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

cb=[0]*len(y)

b=0
for i in range(1,len(y)):
    if y[i] >= 2:
        cb[i]=cmb(y[i],2)
    
        b+=cb[i]
    
for i in range(N):
    k=A[i]
    
    tmp=0
    if y[k]-1>=2:
        tmp = nCr(y[k]-1, 2)
    tmp2=b-cb[k]+tmp
    
    print(tmp2)

Solution (Python):

import os
import sys
from io import BytesIO, IOBase
from math import ceil

def main():
    n = int(input())
    a = list(map(int,input().split()))
    d = {}
    for x in a:
        d[x] = d.get(x,0)+1
    ans = 0
    for x in d.values():
        ans+=(x*(x-1))//2
    for x in a:
        if d[x] == 1:
            print(ans)
            continue
        old = (d[x]*(d[x]-1))//2
        d[x]-=1
        new = (d[x]*(d[x]-1))//2
        d[x]+=1
        print(ans-old+new)









BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")



if __name__ == "__main__":
    main()

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

ball = [0]*(n+1)

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

zenbu = 0
    
for i in range(n+1):
    tmp = ball[i]
    if tmp > 1:
        zenbu += (tmp*(tmp-1))//2
        
for i in range(n):
    tmp = ball[a[i]]
    if tmp > 1:
        
        print(zenbu-(tmp-1))
    else:
        print(zenbu)

Solution (Python):
import collections 

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

c = collections.Counter(l_a)

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


for k in range(1,N+1):
  n = c[l_a[k-1]]
  print(t-(n-1))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
c=[0]*n

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

ans=0

for i in range(n):
    ans+=c[i]*(c[i]-1)

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

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

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

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

memo = [0]*(2*(10**5)+1)

for i in t:
    memo[i]+=1

sumv=0

for j in memo:
    sumv+=j*(j-1)//2

for i in t:
    print(sumv-(memo[i]*(memo[i]-1))//2+((memo[i]-2)*(memo[i]-1)//2))

Solution (Python):
n=int(input())
x=list(map(int,input().split()))
dic={}
for i in range(n):
  if x[i] in dic.keys():
    dic[x[i]]+=1
  else:
    dic[x[i]]=1
s=0
for i in dic.keys():
  s+=dic[i]*(dic[i]-1)//2
for i in range(n):
  print(s-dic[x[i]]+1)

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

from collections import Counter
cnt = Counter(array)
total = 0
for key in cnt:
  if cnt[key] >= 2:
  	total += int(cnt[key] * (cnt[key] - 1) / 2)
    
for key in cnt:
  if cnt[key] == 1:
    cnt[key] = total
  else:
    temp = total - int(cnt[key] * (cnt[key] - 1) / 2)
    cnt[key] = temp + int((cnt[key]-1) * (cnt[key] - 2) / 2)

for num in array:
    print(cnt[num])

Solution (Python):
N=int(input())
a=[int(x) for x in input().split()]
import collections
c=collections.Counter(a)
d=list(c.keys())
def nc2(x):
    return x*(x-1)//2
ans0=0
for i in d:
    ans0+=nc2(c[i])
for i in range(len(a)):
    print(ans0-(c[a[i]]-1))

Solution (Python):


import sys

DEBUG = False

def read(t):
    return t(sys.stdin.readline().rstrip())


def read_list(t, sep = " "):
    return [t(s) for s in sys.stdin.readline().rstrip().split(sep)]


def dprint(*args, **kwargs):
    if DEBUG:
        print(*args, **kwargs)
    return

def nr_choose2(nr_ball):
    return nr_ball * (nr_ball - 1) // 2

def main():
    n = read(int)
    as_ = read_list(int)
    nr_balls_by_num = {}
    nr_choose2_by_num = {}
    for a in as_:
        if a not in nr_balls_by_num:
            nr_balls_by_num[a] = 0
        nr_balls_by_num[a] += 1

    nr_all_choose2 = 0
    for num in nr_balls_by_num:
        nr_choose2_by_num[num] = nr_choose2(nr_balls_by_num[num])
        nr_all_choose2 += nr_choose2_by_num[num]

    for k in range(len(as_)):
        ans = nr_all_choose2 - nr_choose2_by_num[as_[k]]
        ans += nr_choose2(nr_balls_by_num[as_[k]] - 1)
        print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
def binom(n,r):
    if n<r:
        return 0
    if n<0:
        return 0
    if n==r:
        return 1

    result=1
    for i in range(r):
        result*=(n-i)
        result//=(i+1)

    return result


n=int(input())
count=[0 for _ in range(n)]
minus=[0 for _ in range(n)]
an=[int(c) for c in input().split()]
for i in range(n):
    count[an[i]-1]+=1

sum_=0
for i in range(n):
    sum_+=binom(count[i],2)
    minus[i]=binom(count[i]-1,1)

for i in range(n):
    print(sum_-minus[an[i]-1])

Solution (Python):
import math
from collections import Counter


def comb(x, y):
    if x - y < 0:
        return 0
    return math.factorial(x) // (math.factorial(y) * math.factorial(x - y))


N = int(input())

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


c = Counter()

ints = list()
for a in A:
    ints.append(a)
    c[a] += 1

t = 0
for a in set(ints):
    t += comb(c[a], 2)

for a in ints:
    print(t - (c[a] - 1))

Solution (Python):
import collections

n = int(input())
a = list(map(int, input().split()))
c = collections.Counter(a)
cs = {k: v*(v-1)//2 for k, v in c.items() if v!=1}
s = sum(cs.values())

for v in a:
    if v in cs:
        print(s-(c[v]-1))
    else:
        print(s)

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

number = [0]*n
for item in A:
    number[item-1] += 1

ans = 0

for items in number:
    ans += items*(items-1)//2

for item in A:
    print(ans - (number[item-1]-1))

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


dic_a={}
for a in alist:
  if not a in dic_a:
    dic_a[a]=1
  else:
    dic_a[a]+=1
    
all_pair=0
for val in dic_a.values():
  all_pair+=val*(val-1)//2


dic_answer={}
for a in alist:
  if a in dic_answer:
    print(dic_answer[a])
    continue
  
  now=dic_a[a]
  kill=now*(now-1)//2-(now-1)*(now-2)//2
  answer=all_pair-kill
  
  print(answer)
  dic_answer[a]=answer

Solution (Python):
from collections import Counter

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

    ans=0
    c = Counter(A)
    values, counts = zip(*c.most_common())
    for j in counts:
        if j >= 2:
            ans += j * (j - 1) // 2

    for i in A:
        if c[i]>=3:
            print(ans-(c[i]*(c[i]-1)//2-(c[i]-1)*(c[i]-2)//2))
        elif c[i]==2:
            print(ans-1)
        else:
            print(ans)






resolve()

Solution (Python):
from collections import Counter

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

C = Counter(As)

ans = sum(v * (v - 1) // 2 for k, v in C.items())
M = [C[a] - 1 for a in As]

for m in M:
    print(ans - m)

Solution (Python):
n = int(input())
a_list = [int(x) for x in input().split()]
a_dict = {}

for a in a_list:
  if a in a_dict.keys():
    a_dict[a] += 1
  else:
    a_dict[a] = 1


sum_value = sum([int((v*(v-1))/2)for v in a_dict.values()])

for a in a_list:
  print(sum_value-a_dict[a]+1)

Solution (Python):
import bisect, collections
 
 
 
def solution():
    d = collections.defaultdict(int)
    n = int(input().strip())
    a = [int(x) for x in input().strip().split(" ")]
    for x in a:
        d[x] += 1
    total = 0
    for _, v in d.items():
        total += v*(v-1)//2
    for x in a:
        print(total-d[x]*(d[x]-1)//2+(d[x]-1)*(d[x]-2)//2)
    

def main():    
    
    for _ in range(1):
        solution()

if __name__ == "__main__":
    main()

Solution (Python):
n=input()
arr=map(int, raw_input().split())
count=[0]*(n+1)
for i in arr:
  count[i]+=1
res=0
for i in xrange(n+1):
  if count[i]>1:
    res+=count[i]*(count[i]-1)/2

for i in xrange(n):
  s=count[arr[i]]
  if s>1:
    if s-1>1:
        print res-s*(s-1)/2+(s-1)*(s-2)/2
    else:
        print res-s*(s-1)/2
  else:
    print res

Solution (Python):
import math

N = int(input())
nums = list(map(int, input().split()))
numsB = nums[:]
d = dict()

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

for num in nums:
    if num not in d:
        d[num] = 1
    else:
        d[num] += 1

total = 0
for i in d:
    n = d[i]
    if n == 2:
        total += 1
    elif n > 2:
        total += combinations_count(n, 2)

for num in numsB:
    tmp = d[num] - 1


    print(int(total - tmp))

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

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


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

c = collections.Counter(a)
cM = c.most_common()
n_sel = 0
for i in range(len(c)):
  if cM[i][1] > 1:
    n_sel += cmb(cM[i][1], 2)
  else:
    break;
    
for k in range(n):
  k_sel = c[a[k]] - 1
  print(n_sel - k_sel)

Solution (Python):
import collections
n=int(input())
a=list(map(int,input().split()))
M=max(a)
c=collections.Counter(a)
com=[[0] for i in range(M+1)]
for i in range(M+1):
    com[i]=c[i]*(c[i]-1)//2
s=sum(com)
for i in range(n):
    newcom=(c[a[i]]-1)*(c[a[i]]-2)//2
    print(s-com[a[i]]+newcom)

Solution (Python):
def f(x):
    return x*(x-1)//2

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

d = collections.defaultdict(int)
for i in a:
    d[i]+=1
c = collections.defaultdict(int)
ans=0
for i in set(a):
    ans+=f(d[i])

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

Solution (Python):
import sys,math,copy,queue,itertools
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
def LI(): return [int(x) for x in input().split()]
N = int(input())
A = LI()

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

tc = 0
for i in range(1,N+1):
    tc += CA[i] * (CA[i]-1) // 2

for i in range(N):
    ans = tc
    cnt = CA[A[i]]
    if cnt > 1:
        ans -= cnt * (cnt - 1) // 2 - (cnt - 1) * (cnt - 2) // 2
    print (ans)

Solution (Python):
n = int(input())
a_arr = []

a_map = {}
total = 0

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

for x in input().split():
  a = int(x)
  a_arr.append(a)
  if a in a_map.keys():
    a_map[a][0] = a_map[a][0] + 1

    thiscomb = comb(a_map[a][0])
    prevcomb = comb(a_map[a][0]-1)
    a_map[a][1] = thiscomb - prevcomb
    total = total - prevcomb + thiscomb
  else:
    a_map[a] = [1, 0]


for a in a_arr:
  print(int(total - a_map[a][1]))

Solution (Python):
from collections import Counter

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

b = Counter(A)

bnum = list(b.values())
ans = 0
for i in range(len(bnum)):
    ans = ans + bnum[i]*(bnum[i]-1)/2
    
    
for j in range(N):
    temp = b[A[j]]
    print(int(ans-temp+1))

Solution (Python):
from collections import Counter

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

A_set = set(A)
a = Counter(A)
ruiseki = 0
for i in A_set:
    count = a[i]
    ruiseki += count*(count-1)/2
    
for i in range(N):
    b = A[i]
    front = a[b] * (a[b]-1) / 2
    later = (a[b]-1) * (a[b]-2) / 2
    ans = ruiseki - front + later
    print(int(ans))

Solution (Python):

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


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

count = {}
for i in a:
    if i in count:
        count[i] += 1
    else:
        count[i] = 1

total = 0
nbk = {}
for k, c in count.items():
    tmp = cmb(c)
    nbk[k] = tmp
    total += tmp

for k in range(n):
    orginal = nbk[a[k]]
    removed = cmb(count[a[k]] - 1)
    print(total - orginal + removed)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
def combinations_count2(n):
  return n*(n-1) // 2
d=[0]*(N+1)
for a in A:
  d[a]+=1
ans=0
for i in range(1,N+1):
  if d[i]>1:
    _=combinations_count2(d[i])
    ans+=_
    d[i]=_-combinations_count2(d[i]-1)
  else:
    d[i]=0
for a in A:
  print(ans-d[a])

Solution (Python):
from scipy.misc import comb
N=int(input())
A=list(map(int,input().split()))
nset=set([])
ndict={}
for a in A:
  if a in nset:
    ndict[a]+=1
  else:
    nset.add(a)
    ndict[a]=1

all=0
for n in nset:
  tmp=ndict[n]
  if tmp>=2:
    all+=comb(tmp,2,exact=True)
all=int(all)
for a in A:
  tmp=ndict[a]
  if tmp==1:
    print(all)
  elif tmp==2:
    print(all-1)
  else:
    print( all-comb(tmp,2,exact=True)+comb(tmp-1,2,exact=True) )

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

count_dict = collections.Counter(A)
combination_dict = count_dict.copy()
diff_dict = count_dict.copy()

for k, v in count_dict.items():
    temp = combination_dict[k]
    combination_dict[k] = int(temp * (temp - 1) / 2)

for k, v in count_dict.items():
    temp = count_dict[k]
    diff_dict[k] = temp - 1

s = sum(combination_dict.values())

for k in A:
    print(s - diff_dict[k])

Solution (Python):
from collections import Counter

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

counter = Counter(A)
combinations = {k: int(v * (v - 1) / 2) for k, v in counter.items()}

total = sum(combinations.values())

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

Solution (Python):


def input_multiple_number_as_list():
    return list(map(int, input().split()))
N = int(input())
list_A = input_multiple_number_as_list()


cntlist = [0]*(N+1)

for k in range(N):
    cntlist[list_A[k]]+=1

totalcomb = 0

for k in cntlist:
    totalcomb += (k*(k-1)//2)
for k in list_A:
    print(totalcomb-((cntlist[k])*(cntlist[k]-1)//2)+((cntlist[k]-1)*(cntlist[k]-2)//2))

Solution (Python):


N = int(input())
As = list(map(int, input().split()))
As_sorted = sorted(As)

pre_num = 0
sum_cnt = 0
inner_sum_cnt = 1
num_cnt_list = [0 for i in range(max(As)+1)]  
for A in As_sorted:
    if pre_num == A:
        inner_sum_cnt += 1
    else:
        if pre_num != 0:
            sum_cnt += inner_sum_cnt * (inner_sum_cnt - 1) // 2
            num_cnt_list[pre_num] = inner_sum_cnt
        pre_num = A
        inner_sum_cnt = 1

if pre_num == A:
    sum_cnt += inner_sum_cnt * (inner_sum_cnt - 1) // 2
    num_cnt_list[pre_num] = inner_sum_cnt

for A in As:
    cnt = num_cnt_list[A]
    if cnt < 2:
        print(sum_cnt)
    else:
        print(sum_cnt - cnt + 1)

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

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

for i in range(N+1):
    M[i] = K[i]*(K[i]-1)//2

y = sum(M)


for a in A:
    
    print(y-K[a]+1)

Solution (Python):
import sys
from math import ceil, floor
from collections import deque, Counter, defaultdict
from fractions import gcd
input = lambda: sys.stdin.readline().rstrip()

def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return

def comb(n,r):
    p,q=1,1
    for i in range(r):
        p=p*(n-i) 
        q=q*(i+1) 
    return p//q

def main():
    n = int(input())
    As = list(map(int, input().split()))
    Ps = list()
    cnt = 0
    counter = Counter(As)
    for i in range(n):
        Ps.append(counter[As[i]]-1)
    
    for i in counter:
        cnt += comb(counter[i], 2)

    for i in range(n):
        print(cnt-Ps[i])

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a_ = list(map(int,input().split()))
adic = {}
for i in range(n):
    a = a_[i]
    if a in adic.keys():
        adic[a] += 1
    else:
        adic[a] = 1
ans = 0
for i in adic.values():
    if i >= 2:
        ans += i*(i-1)//2
moto = ans
for i in range(n):
    a = a_[i]
    b = adic[a]
    if b >= 2:
        ans -= b*(b-1)//2
        ans += (b-1)*(b-2)//2
    print(ans)
    ans = moto

Solution (Python):
from collections import Counter

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

Count = Counter(A)
s = sum(x * (x-1) // 2 for x in Count.values())

for a in A:
  print(s - Count[a] + 1)

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


C = collections.Counter(A)

d = {}

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


for i in range(N):
    minus = d[A[i]][0]
    plus = d[A[i]][1]
    print(total - minus + plus)

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

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

for i in a:
    sa = dic[i] * (dic[i] - 1) // 2 - ((dic[i] - 1) * (dic[i] - 2) // 2)
    print(ans - sa)

Solution (Python):
import sys
from collections import Counter,defaultdict

N=input()
A=map(int, sys.stdin.readline().split())
C=defaultdict(lambda: 0)
for x in A:
    C[x]+=1
total=0

for x in C.values():
    total+=x*(x-1)/2

for a in A:
    n=C[a]
    print total-n*(n-1)/2+(n-1)*(n-2)/2

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

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

c = Counter(a)
d = dict(c.most_common())

x = sum([comb(v, 2, exact=True) for v in d.values()])
for i in range(n):
    print(x - (comb(d[a[i]], 2, exact=True) - comb(d[a[i]] - 1, 2, exact=True)))

Solution (Python):
import collections

n=int(input())
a=list(map(int,input().split()))
c = collections.Counter(a)
k=list(c.items())

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

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

Solution (Python):

from __future__ import division, print_function

import os
import sys
from io import BytesIO, IOBase

from collections import Counter

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip


def main():
    n = int(input().strip())
    A = [int(s) for s in input().strip().split()]

    cnt = Counter(A)
    w = sum(x * (x - 1) // 2 for x in cnt.values())

    for a in A:
        result = w - (cnt[a] - 1)
        print(result)




BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")



if __name__ == "__main__":
    main()

Solution (Python):
from collections import Counter
N = int(input())
A = [int(i) for i in input().split()]
C = Counter(A)
s = 0
d = {}
for k in C.keys():
    v = C[k]
    t = v*(v-1)//2
    s += t
    d[k] = t
for a in A:
    print(s-d[a]+((C[a]-1)*(C[a]-2)//2))

Solution (Python):
from collections import Counter

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

tbl0 = Counter(a)
tbl = tuple(Counter(a).items())

t = 0
for v in tbl:
    t += v[1] * (v[1] - 1) // 2

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

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

from collections import defaultdict

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

diff = {}
total = 0
for i in count.keys():
    tmp = count[i] * (count[i]-1) // 2
    if tmp == 0:
        diff[i] = 0
    else:
        diff[i] = tmp - (count[i]-1) * (count[i]-2) // 2
    total += tmp

prnt = 0
for i in A:
    prnt = total
    prnt -= diff[i]
    print(prnt)

Solution (Python):
from collections import Counter, defaultdict
def calc_comb(n):
    return (n * (n-1)) // 2

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

sum_val = 0
for key in cnt.keys():
    if 3 <= cnt[key]:
        sum_val += calc_comb(cnt[key])
    elif cnt[key] == 2:
        sum_val += 1

for i in range(N):
    cnt[A[i]] -= 1
    if 2 <= cnt[A[i]]:
        res = sum_val - calc_comb(cnt[A[i]] + 1) + calc_comb(cnt[A[i]])
        print(res)
    elif cnt[A[i]] == 1:
        res = sum_val - 1
        print(res)
    else:
        print(sum_val)
    cnt[A[i]] += 1

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

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

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

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