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

Solution (Python):


import sys
 
stdin = sys.stdin
inf = 1 << 60
mod = 1000000007
 
ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

from collections import Counter

n = ni()
a = na()
c = Counter(a)
d = dict(c)
r = 0
for k, v in d.items():
    r += v * (v - 1) // 2

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

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

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

d = [0]*n
for i in range(n):
    d[al[i]-1] += 1

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

for k in range(n):
    a = al[k]-1
    if d[a]<2:
        print(s)
        continue
    if d[a]==2:
        print(s-1)
        continue
    res = s - d[a]*(d[a]-1)//2 + (d[a]-1)*(d[a]-2)//2
    print(res)

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

all_ans = sum([item * (item - 1) // 2 for item in dic.values()])

for i in range(n):
    print(all_ans - dic[a[i]] + 1)

Solution (Python):
from collections import Counter
n = int(input())
a = [int(i) for i in input().split()]
cnt = Counter(a)
ans = {}
for key in cnt.keys():
    v = cnt[key]
    ans[key] = v * (v-1) // 2
s = sum(list(ans.values()))
for ai in a:
    if cnt[ai] == 1:
        print(s)
    else:
        print(int(s - ans[ai] + (ans[ai] / cnt[ai]) * (cnt[ai] - 2)))

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

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

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

S=0
for i in range(N+1):
    if B[i]!=0:
        S += int(B[i]*(B[i]-1)/2)

for i in range(N):
    print(S - (B[A[i]] -1))

Solution (Python):
import collections
import numpy as np

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

di = collections.Counter(li)


arr = np.array(list(di.values()))
arrr = arr - 1
c = np.dot(arr, arrr) // 2




for v in li:
    x = int(di[v])
    if x == 1:
        print(c)
    else:
        print(c-(x-1))

Solution (Python):

n=200001
k=2
C = [[0 for x in range(k+1)] for x in range(n+1)] 
for i in range(n+1): 
    for j in range(min(i, k)+1): 
        if j == 0 or j == i: 
            C[i][j] = 1
        else: 
            C[i][j] = C[i-1][j-1] + C[i-1][j]
t=int(input())
a=[int(x) for x in input().split()]
d=dict()
d2=dict()
for i in a:
    if i not in d:
        d[i]=1
    else:
        d[i]+=1
total=0
for i in d:
    
    count=d[i]
    if(count<2):
        d2[i]=0
    else:
        
        d2[i]=C[count][2]
    total+=d2[i]


for i in a:
    count=d[i]
    if(count<=2):
        ans=0
    else:
        ans=C[count-1][2]
    print(total-d2[i]+ans)

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

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

total = 0

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

for a in A:
    if m[a] >= 2:
        print(total - (m[a]-1))
    else:
        print(total)

Solution (Python):
N = int(input())
A=list(map(int,input().split()))
count_list = [0 for _ in range(N)]
choose_list = [0 for _ in range(N)]
for a in A:
    count_list[a-1] += 1
for i,c in enumerate(count_list):
    choose_list[i] = int(c*(c-1)/2)
sum = sum(choose_list)

for i in range(N):
    ball = A[i] - 1
    ball_num = count_list[ball] - 1
    print(sum - choose_list[ball] + ball_num*(ball_num-1)//2)

Solution (Python):
import sys
from math import factorial
from collections import Counter
def input(): return sys.stdin.readline().rstrip()

def main():
    N = int(input())
    A = tuple(map(int, input().split()))
    c = Counter(A)

    data = [0] * (N+1)
    sum_count = 0
    for i, v in c.items():
        if v < 2:
            continue
        value = factorial(v) // 2 // factorial(v - 2)
        data[i] = value - value * (v - 2) // v
        sum_count += value

    print('\n'.join([str(sum_count - data[a]) for a in A]))

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
count = [0]*n
base = 0
list = [int(i) for i in input().split()]

for i in list:
    count[i-1] += 1

for j in range(n):
    m = count[j]
    base += m*(m-1)//2

for k in list:
    print(base - count[k-1] + 1)

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

Solution (Python):
n = int(input())
a = [int(x) for x in input().split()]
d_cnt = dict()
d_comb = dict()
for x in a:
    cnt = d_cnt.get(x, -1) + 1
    d_cnt[x] = cnt
    if cnt >= 1:
        d_comb[x] = d_comb.get(x, 0) + cnt
base = sum(d_comb.values())
for x in a:
    print(base - d_cnt.get(x))

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

num_cnt = [0] * 200001
for i in a:
    num_cnt[i] += 1

pat_cnt = [0] * 200001
for i in range(200001):
    b = num_cnt[i]
    pat_cnt[i] = (b*(b-1))//2

ans = sum(pat_cnt)

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

Solution (Python):


def main():
     n = int(input())
     a = list(map(int, input().split()))
     num_a = dict()
     com_a = dict()

     for i in a:
          if not i in num_a.keys():
               num_a[i] = 1
          else:
               num_a[i] += 1

     for i in num_a.keys():
          value = num_a[i]
          com_a[i] = com(value)

     
     

     pre_ans = sum(com_a.values())
     

     for i in range(n):
          k = a[i]
          ans = pre_ans - com_a[k] + com(num_a[k] - 1)
          print(ans)

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

if __name__ == "__main__":
    main()

Solution (Python):
import sys 

input = sys.stdin.readline


import collections as c

n = int(input())
al = list(map(int, input().split(' ')))

c = c.Counter(al)

s = 0
for k, v in c.most_common():
    s += (v) * (v - 1) // 2

for i in range(n):
    temp = c[al[i]]
    a = temp * (temp - 1) // 2
    b = (temp - 2) * (temp - 1) // 2
    
    print(s - a + b)

Solution (Python):
import collections

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

mx = 0

for item, value in c.items():
    mx += value * (value-1) // 2

for i in range(len(a)):
    ans = mx

    t = c[a[i]]
    ans -= t * (t-1) //2
    ans += (t-1)*(t-2) //2
    print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10000000)


def main():
    N = int(input())
    A = [int(c) for c in input().split()]
    num_lists = [0] * (N + 1)
    for a in A:
        num_lists[a] += 1
    A_C = [int(c * (c - 1) / 2) for c in num_lists]
    sum_ = sum(A_C)

    for a in A:
        temp_sum = sum_ - A_C[a]
        temp_sum += (num_lists[a] - 1) * (num_lists[a] - 2) / 2
        print(int(temp_sum))


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
L = [int(s) for s in input().rstrip().split(' ')]
D = {}
for x in L:
  if x not in D.keys():
    D[x] = 1
  else:
    D[x] += 1
S = 0
for y in D.values():
  if y > 1:
    S += y*(y-1) // 2
for i in range(N):
  print(S-D[L[i]]+1)

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

ans = sum([C[key]*(C[key]-1) // 2 for key in C.keys() if C[key] >= 2])


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

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

c = Counter(arr)

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

ans = {}
tot = 0
for k,v in c.items():
    ans[k] = co(v)
    tot += ans[k]

for it in arr:
    a = tot - ans[it] + co(c[it]-1)
    print(a)

Solution (Python):
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    from collections import Counter
    c = Counter(A)
    ans = 0
    for v in c.values():
        ans += v*(v-1)//2
    for k in range(N):
        if 2 <= c[A[k]]:
            print(ans - (c[A[k]] - 1))
        else:
            print(ans)


if __name__ == '__main__':
    main()

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

from collections import Counter
AC=Counter(A)
ans=int(sum(map(lambda x: x*(x-1)/2, AC.values())))
for a in A:
    print(max(ans-AC[a]+1,0))

Solution (Python):

import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue
from decimal import *


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def divisor(n):
        res = []
        i = 1
        for i in range(1, int(n ** 0.5) + 1):
            if n % i == 0:
                res.append(i)
                if i != n // i:
                    res.append(n // i)
        return res

    @staticmethod
    def round_up(a, b):
        return -(-a // b)

    @staticmethod
    def is_prime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


class PriorityQueue:
    def __init__(self, l=[]):
        self.q = l
        heapq.heapify(self.q)
        return

    def push(self, n):
        heapq.heappush(self.q, n)
        return

    def pop(self):
        return heapq.heappop(self.q)


def pop_count(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f


MOD = int(1e09) + 7
INF = int(1e15)


def main():
    
    N = Scanner.int()
    A = Scanner.map_int()
    M = Counter(A)
    MAX = 0
    for v in M.values():
        MAX += v * (v - 1) // 2
    for i in range(N):
        a = M[A[i]]
        ans = MAX - a * (a - 1) // 2 + (a - 1) * (a - 2) // 2
        print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = [0]*(n+1)
    c = [0]*(n+1)
    d = [0]*(n+1)
    e = [0]*(n+1)
    cmb = lambda x: (x*(x-1))//2
    for ai in a:
        b[ai] += 1
    for i, bi in enumerate(b):
        if bi:
            c[i] = cmb(bi)
            d[i] = cmb(bi-1)
    ans = sum(c)
    for i, (ci, di) in enumerate(zip(c, d)):
        e[i] = ans-ci+di
    for ai in a:
        print(e[ai])

if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
a = [int(e) for e in input().split()]
c= [0]*(n+1)
def nc2(n):
    return n*(n-1)//2
for i in range(0,n):
    c[a[i]] += 1
sum = 0
for i in range(1,n+1):
    sum += nc2(c[i])
for i in range(0,n):
    print(sum - nc2(c[a[i]])+nc2(c[a[i]]-1))

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

N = int(stdin.readline())
A = list(map(int, stdin.readline().split()))

cnt = Counter(A)

tmp = cnt.most_common()


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


sumall = 0

for i in range(len(cnt)):
    sumall += comb(tmp[i][1])

for a in A:
    ans = sumall - (comb(cnt[a]) - comb(cnt[a] - 1))
    print(ans)

Solution (Python):
import sys


def main():
    n=int(input())
    a=list(map(int,input().split()))
    l=[0]*(n+1)
    s=0
    for i in range(n):
        l[a[i]]+=1
    for i in range(n+1):
        if l[i]==0:
            continue
        else:
            s+=l[i]*(l[i]-1)//2
    for i in range(n):
        print(s-(l[a[i]]-1))

if __name__ == '__main__':
    main()

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

d = dict()
for e in a:
    if e in d:
        d[e] += 1
    else:
        d[e] = 1

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

for e in a:
    if d[e] <= 1:
        print(s)
    else:
        k = d[e]
        res = s - k*(k-1)//2 + (k-1)*(k-2)//2
        print(res)

Solution (Python):
from copy import deepcopy

n = int(input().strip())
a = list(input().split())
b = deepcopy(a)
counts = {}
tmp = b[0]
for elem in b:
    if elem in counts:
        counts[elem] += 1
    else:
        counts[elem] = 1
    if tmp != elem:
        tmp = elem
c = deepcopy(counts)
for k, v in counts.items():
    c[k] = v * (v - 1) // 2
su = sum(c.values())
for elem in a:
    print(su - c[elem] + ((counts[elem] - 1) * (counts[elem] - 2) // 2))

Solution (Python):
from collections import defaultdict

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

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

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

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

Solution (Python):
import collections
n = int(input())
a = list(map(int, input().split()))
c = collections.Counter(a)
tmp = {}
sum = 0
for j in c:
  tmp[j] = c[j] * (c[j] - 1) // 2
  sum += c[j] * (c[j] - 1) // 2
for i in range(n):
  print((c[a[i]] - 1) * (c[a[i]] - 2) // 2 + sum - tmp[a[i]])

Solution (Python):
import math
import sys

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()))
mod = 10**9 + 7 
sys.setrecursionlimit(1000010)


N = ni()
A  = nl()

dict ={}

for a in A:
    if a in dict:
        x = dict[a]
        dict[a] = [ x[0]+1, -1,-1]        
    else:
        dict[a]=[1,-1,-1]


sum=0
for a in A:
    x = dict[a]
    if x[1] == -1:
        n = x[0]
        dict[a]=[n,n*(n-1)//2,n-1]
        sum = sum + n*(n-1)//2




for a in A:
    print(sum-dict[a][2])

Solution (Python):
n=int(input())
inp=list(map(int,input().split()))
hoge=[0 for i in range(n)]
huga=[0 for i in range(n)]
for i in inp:
  huga[i-1]+=1
  hoge[i-1]=(huga[i-1]-1)*huga[i-1]
some=sum(hoge)//2
for i in inp:
  if(huga[i-1]==2):
    print(some-1)
  else:
    print(some-int(hoge[i-1]/huga[i-1]))

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

s = 0
d = defaultdict(int)
for e in A:
    d[e] += 1
k = len(set(d))

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

for e in A:
    if d[e] > 1:
        res = s - (d[e]-1)
        print(res)
    else:
        print(s)

Solution (Python):
import numpy as np
from collections import Counter, defaultdict

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

C = Counter(A)
Normal = defaultdict(int)
Banned = defaultdict(int)

for i in A:
    Normal[i] = C[i] * (C[i]-1) // 2
    Banned[i] = (C[i]-1) * (C[i]-2) // 2
    
Normal_sum = sum(Normal.values())
for i in A:
    print(Normal_sum - (Normal[i] - Banned[i]))

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

B = list(set(A))
counter = Counter(A)
total = 0

for i in range((len(B))):
    n = counter[B[i]]
    total += n*(n-1)/2

for i in range((len(A))):
    n = counter[A[i]]
    ans = total -(n-1)
    print ans

Solution (Python):





def solv(N, A):
    d = dict()
    for a in A:
        if a in d:
            d[a] += 1
        else:
            d[a] = 1
    total = 0
    for a, n in d.items():
        total += (n * (n-1)) // 2
    for a in A:
        n = d[a]
        print(total - (n-1))


if __name__ == "__main__":

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

    solv(N, A)

Solution (Python):
import itertools
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush,heapify
from fractions import gcd
from copy import deepcopy
import math
import queue
import numpy as np

Mod = 1000000007
import sys
sys.setrecursionlimit(100000) 
  
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)

def main(): 
    n = int(input())
    a = list(map(int,input().split()))
    d = {}
    for i in range(n):
        if not a[i] in d:
            d[a[i]] = 1
        else:
            d[a[i]] += 1
    subans = 0
    for i in d.values():
        if i >= 2:
            subans += i*(i-1)//2
        else:
            continue
    for i in range(n):
        if d[a[i]] >= 2:
            print(subans-(d[a[i]]*(d[a[i]]-1)//2)+((d[a[i]]-1)*(d[a[i]]-2)//2))
        else:
            print(subans)

if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
N = [0 for i in range(2*(10**5)+3)]
b = a.copy()
b.sort()
sum = 0
count = 1
for i in range(1,n):
    if b[i] != b[i-1]:
        sum += count*(count-1)//2
        N[b[i-1]]=count
        count=1
    else:
        count+=1
N[b[n-1]] = count
sum += count*(count-1)//2
for i in range(n):
    print(sum + (N[a[i]]-1) * (N[a[i]]-2) // 2 - N[a[i]] * (N[a[i]]-1)//2)

Solution (Python):
from collections import Counter

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

ans = 0
for v in c.values():
    ans += (v * (v - 1)) // 2 if v > 1 else 0

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

Solution (Python):
from collections import Counter

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

cA = Counter(A)
pairsumc = 0
for k, v in cA.items():
    
    pairsumc += v*(v-1)/2

for a in A:
    print(int(pairsumc - cA[a]+1))

Solution (Python):


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

Q = {}
for a in A:
    if not(a in Q):
        Q[a] = 0

    Q[a] += 1

S = sum(q*(q-1)//2 for q in Q.values())

for k in range(N):
    print(S - (Q[A[k]]-1))

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

from collections import Counter
AC=Counter(A)
ans=int(sum(map(lambda x: x*(x-1)/2, AC.values())))
for a in A:
    print(max(ans-AC[a]+1,0))

Solution (Python):
n=int(input())
aa=[]
aa=list(map(int,input().split()))


a=sorted(aa)
sum=0
mae=0
index=[0 for i in range(n)]
for i in range(n-1):
    if a[i]!=a[i+1]:
        index[a[i]-1]=i-mae+1
        sum+=(i-mae+1)*(i-mae)/2
        mae=i+1
index[a[n-1]-1]=n-mae
sum+=(n-mae)*(n-mae-1)/2

for ii in aa:
    ans=sum-index[ii-1]+1
    print(int(ans))

Solution (Python):


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

val_cnt = [0] * N

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


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


base_sum = 0
for i in range(N):
    base_sum += comb2(val_cnt[i])





for i in range(N):
    print(base_sum + comb2(val_cnt[A[i]-1]-1) - comb2(val_cnt[A[i]-1]))

Solution (Python):



def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)
    

    def dump(*args):
        sys.stderr.write(str(args))

    from collections import Counter
    import math

    def c_count(n, r):  
        if n - r < 0:
            return 0
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

    class Combination:  
        def __init__(self):
            super().__init__()
            self.dp = dict()

        def count(self, n, r):
            if self.dp.get((n, r)) != None:
                return self.dp[(n, r)]
            self.dp[(n, r)] = c_count(n, r)
            return self.dp[(n, r)]

    n = int(input())
    a = list(map(int, input().split()))
    c = Counter(a)
    d = Combination()
    e = dict()

    ss = 0

    for i in c:
        
        e[i] = d.count(c[i], 2)
        
        ss += e[i]

    for i in range(n):
        t = e[a[i]]
        w = t * (c[a[i]] - 2) // c[a[i]]
        
        w = w if w >= 0 else 0
        print(ss-t+w)


if __name__ == '__main__':
    main()

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

    num_list = [0 for _ in range(n + 1)]
    for _a in a:
        num_list[_a] += 1
    
    ans_plus_n = sum([_num * (_num - 1) // 2 for _num in num_list])
    
    for _a in a:
        ans = ans_plus_n - (num_list[_a] - 1)
        print(ans)

    return

if __name__ == "__main__":
    solve()

Solution (Python):
n=int(input())
As = input().split()

dic = {}
for a in As:
  if a not in dic:
    dic[a] = 0
  dic[a] += 1

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

ans = []
for a in As:
  if dic[a] > 1:
    div = dic[a] * (dic[a] - 1) / 2
    kakeru = (dic[a]-1) * (dic[a]-2)/2
    ans.append(int(tmp - div + kakeru))
  else:
    ans.append(int(tmp))
for a in ans:
  print(a)

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

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

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

Sum = sum([comb(c) for c in cnt])

for i in range(len(A)):
    print(int(Sum - comb1[i] + comb2[i]))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
from collections import Counter
d=Counter(a)
import math
def cmb(n,r):
    f = math.factorial
    return int(f(n) // f(r) // f(n-r))
def combination(n, r):
    a = 1
    b = 1
    for i in range(r):
        a *= (n - i)
        b *= (i + 1)
    return a // b
e={}
dd=0
for i,j in d.items():
    dd+=combination(j, 2)
    e[i]=combination(j-1, 1)

for i in a:
    print(dd-e[i])

Solution (Python):
from collections import defaultdict

N = int(input())
A = list(map(int, input().split()))
D = defaultdict(int)
for a in A:
    D[a] += 1

memo = defaultdict(lambda: -1)
conv_D = defaultdict(int)
SUM = 0
for d in D:
    conv_D[d] = D[d] * (D[d] - 1) // 2
    SUM += conv_D[d]

for i in range(N):
    if memo[A[i]] == -1:
        tmp = SUM - conv_D[A[i]] + (D[A[i]] - 1)*(D[A[i]] - 2)//2
        print(tmp)
        memo[A[i]] = tmp
    else:
        print(memo[A[i]])

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
B=sorted(A)+[0]
cnt=1
n=0
dict={}
for i in range(N):
  if B[i+1]==B[i]:
    cnt+=1
  else:
    n+=cnt*(cnt-1)//2
    dict[B[i]]=cnt
    cnt=1
for i in A:
  print(n-(dict[i]-1))

Solution (Python):
from collections import defaultdict

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

d = defaultdict(int)
for a in A:
    d[a] += 1
S = 0
for value in d.values():
    S += value*(value-1)//2
for a in A:
    print(S - (d[a]-1))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
kind=[0]*(2*(10**5)+1) 
kind2=[0]*(2*(10**5)+1) 
for i in range(n):
    kind[a[i]]+=1
ans=0
for i in range(2*(10**5)+1):
    if kind[i]<=1:
        continue
    num=(kind[i]*(kind[i]-1))//2
    ans+=num
    kind2[i]=(kind[i]*(kind[i]-1))//2
    
for i in range(n):
    b=kind[a[i]]-1
    if b<=1:
        print(ans-kind2[a[i]])
        continue
    num=(b*(b-1))//2
    print(ans-kind2[a[i]]+num)

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

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

    total = 0
    dp = {}
    dp1 = {}
    for k, v in balls.items():
        u = v * (v - 1) // 2
        if u > 0:
            dp[k] = - u + (v - 2) * (v - 1) // 2
        else:
            dp[k] = 0
        total += u

    for k in A:
        print(total + dp[k])

main()

Solution (Python):
kazu = dict()
N=input()
A = [int(i) for i in input().split()]
for a in A:
    if a in kazu:
        kazu[a] += 1
    else:
        kazu[a] = 1

tori = 0
for k in kazu.values():
    tori += k*(k-1)//2
for a in A:
    print(tori - kazu[a]+1)

Solution (Python):

import sys
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))

N = II()
A = LI()

from collections import Counter
S = Counter(A)
result = sum(list(map(lambda x: int(x*(x-1)/2), S.values())))

for a in A:
	n = S[a]
	print(result-n+1)

Solution (Python):

import sys



def solve(N: int, A: "List[int]"):
    c=[0] * N
    for i in range(N):
        c[A[i]-1] += 1

    kmax=0
    for i in range(N):
        kmax += c[i] * (c[i]-1) // 2

    
    for i in range(N):
        toruc = c[A[i]-1]
        if toruc == 0:
            print(kmax)
        else:
            print(kmax-toruc+1)
    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):
N = int(input())
l = [int(x) for x in input().split()]
num = [0] * N
C = 0
for i in range(N):
  num[l[i] - 1] += 1
for i in range(N):
  C += num[i] * (num[i] - 1) // 2
for i in range(N):
  print(C - num[l[i]-1] + 1)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
maxA=max(A)
minA=min(A)
ans=0
Num=[0 for i in range(maxA-minA+1)]
nC2=[0 for i in range(maxA-minA+1)]
nC21=[0 for i in range(maxA-minA+1)]

for i in A:
    Num[i-minA]+=1

for i in range(maxA-minA+1):
    nC2[i]=(Num[i]*(Num[i]-1))//2
    ans+=nC2[i]
    if Num[i]!=0:
        nC21[i]=((Num[i]-1)*(Num[i]-2))//2

for i in range(N):
    print(ans-nC2[A[i]-minA]+nC21[A[i]-minA])

Solution (Python):
from collections import defaultdict
n = int(input())
dic=defaultdict(int)
A = list(map(int, input().split()))
for i in range(n):
    dic[A[i]]+=1

tmp=0
for key in dic.keys():
    tmp+=dic[key]*(dic[key]-1)//2

for k in range(n):
    x=dic[A[k]]
    if x>=2:
        ans=tmp-(x*(x-1)//2)+((x-1)*(x-2)//2)
    else: ans=tmp
    print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
b4 = 100000000
tmp = 0
d = {}
for i in b:
    if(b4!=i):
        d[b4] = tmp
        b4 = i
        tmp = 0
    tmp+=1
d[b4] = tmp
ans = 0

alls = 0
for i in d:
    t = d[i]
    alls += int(t*(t-1)/2)

for i in a:
    t = d[i]
    print(alls - (int(t*(t-1)/2) - int((t-1)*(t-2)/2)))

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

import collections

c = collections.Counter(a)
total = 0

for i in c:
  if c[i] <=1:
    total += 0
  else:
  	total += c[i]*(c[i]-1)//2

delta = {}
for i in c:
  if c[i]==2:
    delta[i] = 1
  elif c[i]>=3:
    delta[i] = c[i]*(c[i]-1)//2 - (c[i]-1)*(c[i]-2)//2
  else:
    delta[i] = 0

for i in a:
  print(total - delta[i])

Solution (Python):
import collections

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

D = collections.defaultdict(int)

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

pans = sum(D.values())

for a in A:
    p = (C[a] - 1) * (C[a] - 2) // 2
    ans = pans - D[a] + p

    print(ans)

Solution (Python):
from collections import defaultdict
import math


def combinations_count(n, r):
    if n < r:
        return 0
    else:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


n = int(input())
al = list(map(str, input().split()))
d = defaultdict(lambda: 0)
dd = defaultdict(lambda: 0)
dd_1 = defaultdict(lambda: 0)
for a in al:
    d[a] += 1

sam = 0
for key, value in d.items():
    dd[key] = combinations_count(value, 2)
    dd_1[key] = combinations_count(value-1, 2)
    sam += dd[key]



for a in al:
    value = d[a]
    print(sam - dd[a] + dd_1[a])

Solution (Python):
from math import factorial
def nc2(n):
	if n<2:
		return 0
	else:
		return (n*(n-1))//2

n = int(input())
arr = list(map(int,input().split()))
d = {}
for i in range(n):
	try:
		d[arr[i]].append(i)
	except KeyError:
		d[arr[i]] = [i]

pick2 = {}
pick1 = {}
for i in d.keys():
	pick2[i] = nc2(len(d[i]))
	pick1[i] = nc2(len(d[i])-1)

sum_pick2 = sum(pick2.values())

done = {}

for i in d.keys():
	done[i] = (sum_pick2 - pick2[i] + pick1[i])

for i in arr:
	print(done[i])

Solution (Python):
import collections

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

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

for i in range(n):
    a = Acoun[A[i]] - 1
    ans = x - (a * (a - 1) // 2 - (a - 1) * (a - 2) // 2)
    print(ans - 1)