Problem:
 Score : 300 points    Problem Statement The season for Snuke Festival has come again this year. First of all, Ringo will perform a ritual to summon Snuke. For the ritual, he needs an altar, which consists of three parts, one in each of the three categories: upper, middle and lower.  He has N parts for each of the three categories. The size of the i -th upper part is A_i , the size of the i -th middle part is B_i , and the size of the i -th lower part is C_i .  To build an altar, the size of the middle part must be strictly greater than that of the upper part, and the size of the lower part must be strictly greater than that of the middle part. On the other hand, any three parts that satisfy these conditions can be combined to form an altar.  How many different altars can Ringo build? Here, two altars are considered different when at least one of the three parts used is different.      Constraints  1 \leq N \leq 10^5  1 \leq A_i \leq 10^9(1\leq i\leq N)  1 \leq B_i \leq 10^9(1\leq i\leq N)  1 \leq C_i \leq 10^9(1\leq i\leq N)  All input values are integers.         Input Input is given from Standard Input in the following format:  N  A_1  ...  A_N  B_1  ...  B_N  C_1  ...  C_N       Output Print the number of different altars that Ringo can build.        Sample Input 1 2
1 5
2 4
3 6      Sample Output 1 3  The following three altars can be built:   Upper: 1 -st part, Middle: 1 -st part, Lower: 1 -st part  Upper: 1 -st part, Middle: 1 -st part, Lower: 2 -nd part  Upper: 1 -st part, Middle: 2 -nd part, Lower: 2 -nd part        Sample Input 2 3
1 1 1
2 2 2
3 3 3      Sample Output 2 27       Sample Input 3 6
3 14 159 2 6 53
58 9 79 323 84 6
2643 383 2 79 50 288      Sample Output 3 87

Solution (Python):
import bisect

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

a = sorted(a)

for i in range(n):
    b[i] = (b[i], bisect.bisect_right(a, b[i]-1))

b = sorted(b)

bsum = [b[0][1]]
b[0] = b[0][0]
for i in range(1, n):
    bsum.append(bsum[i-1] + b[i][1])
    b[i] = b[i][0]

cnt = 0
for i in range(n):
    ind = bisect.bisect_right(b, c[i]-1)

    if ind > 0:
        cnt += bsum[ind-1]

print(cnt)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
a.sort()
b.sort()
c.sort()
ab=[]
bc=[]
for i in range(n):
    ab.append([a[i],n])
    bc.append([b[i],n])
    ab.append([b[i],i])
    bc.append([c[i],i])
numa=1
numb=1
numc=1
la=[1]*n
lb=[1]*n
lc=[1]*n
for i in range(1,n):
    if a[i-1]!=a[i]:
        la[i]=i+1
        numa=i+1
    else:
        la[i]=numa
    if b[i-1]!=b[i]:
        lb[i]=i+1
        numb=i+1
    else:
        lb[i]=numb
    if c[i-1]!=c[i]:
        lc[i]=i+1
        numc=i+1
    else:
        lc[i]=numc
ab.sort(key=lambda x: x[0])
numab=1
lab=[1]*(n+1)
for i in range(1,n*2):
    if ab[i-1][0]!=ab[i][0]:
        lab[ab[i][1]]=i+1
        numab=i+1
    else:
        lab[ab[i][1]]=numab
ansb=[0]*n
numb=0
for i in range(n):
    numb+=lab[i]-lb[i]
    ansb[i]=numb
bc.sort(key=lambda x: x[0])
numbc=1
lbc=[1]*(n+1)
for i in range(1,n*2):
    if bc[i-1][0]!=bc[i][0]:
        lbc[bc[i][1]]=i+1
        numbc=i+1
    else:
        lbc[bc[i][1]]=numbc
ansc=[0]*n
for i in range(n):
    ansc[i]=lbc[i]-lc[i]
ans=0
for i in range(n):
    if ansc[i]!=0:
        ans+=ansb[ansc[i]-1]
print(ans)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
a.sort()
c.sort()
ans = 0
for j in range(n):
    l = 0
    r = n - 1
    i = (l + r) // 2
    while l <= r:
        if a[i] >= b[j]:
            r = i - 1
        else:
            l = i + 1
        i = (l + r) // 2
    l = 0
    r = n - 1
    k = (l + r) // 2
    while l <= r:
        if c[k] <= b[j]:
            l = k + 1
        else:
            r = k - 1
        k = (l + r) // 2
    ans += (i + 1) * (n - (k + 1))
print(ans)

Solution (Python):






from bisect import bisect_left,bisect





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





ans = 0
for b in B:
    i = bisect_left(A,b)
    j = bisect(C,b)
    ans += i*(N-j)
print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
a.sort()
a.reverse()
b.sort()
b.reverse()
c.sort()
c.reverse()
c.append(0)

i=0
l=[]
for j in b:
  while c[i]>j:
    i+=1
  l.append(i)
b.append(0)
s=0
cnt=0
i=0
for j in a:
  while b[i]>j:
    s+=l[i]
    i+=1
  cnt+=s
print(cnt)

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

A = sorted(list(map(int, input().split())))+[10**10]
B = sorted(list(map(int, input().split())))+[10**10]
C = sorted(list(map(int, input().split())))

cnt = [0]*(N+1)

ai = 0
for bi in range(N):
    while A[ai] < B[bi]:
        ai += 1
    cnt[bi] = ai

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

ans = 0

bi = 0
for ci in range(N):
    while B[bi] < C[ci]:
        bi += 1
    ans += cnt[bi-1]

print(ans)

Solution (Python):
import sys
from sys import exit
from collections import deque
from copy import deepcopy
from bisect import bisect_left, bisect_right, insort_left, insort_right
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import gcd, sin, cos, tan, asin, acos, atan, degrees, radians

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def lgcd(l):
    return reduce(gcd,l)
def llcm(l):
    return reduce(lcm,l)
def powmod(n,i,mod=MOD):
    return pow(n,mod-1+i,mod) if i<0 else pow(n,i,mod)
def div2(x):
    return x.bit_length()
def div10(x):
    return len(str(x))-(x==0)
def intput():
    return int(input())
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x,d=1):
        self.setdefault(x,0)
        self[x] += d
    def list(self):
        l = []
        for k in self:
            l.extend([k]*self[k])
        return l
class comb():
    def __init__(self, n, mod=None):
        self.l = [1]
        self.n = n
        self.mod = mod
    def get(self,k):
        l,n,mod = self.l, self.n, self.mod
        k = n-k if k>n//2 else k
        while len(l)<=k:
            i = len(l)
            l.append(l[i-1]*(n+1-i)//i if mod==None else (l[i-1]*(n+1-i)*powmod(i,-1,mod))%mod)
        return l[k]
def pf(x,mode='counter'):
    C = counter()
    p = 2
    while x>1:
        k = 0
        while x%p==0:
            x //= p
            k += 1
        if k>0:
            C.add(p,k)
        p = p+2-(p==2) if p*p<x else x
    if mode=='counter':
        return C
    S = set([1])
    for k in C:
        T = deepcopy(S)
        for x in T:
            for i in range(1,C[k]+1):
                S.add(x*(k**i))
    if mode=='set':
        return S
    if mode=='list':
        return sorted(list(S))



N=intput()
A=lint()
B=lint()
C=lint()
A.sort()
B.sort()
C.sort()
AB=[0]*N
A.append(INF)
a=0
b=0
while b<N:
    if A[a]<B[b]:
        a+=1
    else:
        AB[b]=(AB[b-1] if b>0 else 0)+a
        b+=1
ABC=[0]*N
AB=[0]+AB[:]
B.append(INF)
b=0
c=0
while c<N:
    if B[b]<C[c]:
        b+=1
    else:
        ABC[c]=(ABC[c-1] if c>0 else 0)+AB[b]
        c+=1
print(ABC[-1])

Solution (Python):
N=int(input())
*A,=map(int,input().split())
*B,=map(int,input().split())
*C,=map(int,input().split())

A.sort()
C.sort()
from bisect import*
ans=0
for b in B:
    x=bisect_left(A,b)
    y=N-bisect_right(C,b)
    ans+=x*y
print(ans)

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

u_cnt = [0] * N
d_cnt = [0] * N

t = 0
for i in range(N):
    while t < N and A[t] < B[i]:
        t += 1
    u_cnt[i] = t

t = N-1
for i in range(N-1,-1,-1):
    while t >= 0 and C[t] > B[i]:
        t -= 1
    d_cnt[i] = N - 1 - t

ans = 0
for i in range(N):
    ans += u_cnt[i] * d_cnt[i]

print(ans)

Solution (Python):
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:sorted(list(map(int,stdin.readline().split())))
from bisect import bisect_left, bisect_right

n=int(input())
a=lnii()
b=lnii()
c=lnii()

ans=0
for i in range(n):
  m=b[i]
  num1=bisect_left(a,m)
  num2=n-bisect_right(c,m)
  ans+=num1*num2

print(ans)

Solution (Python):
import sys
from bisect import bisect_left,bisect_right


def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  


N = I()
A,B,C = LI(),LI(),LI()
A.sort()
C.sort()

print(sum(bisect_left(A,b)*(N-bisect_right(C,b)) for b in B))

Solution (Python):
from bisect import bisect_right
N = int(input())
A = sorted(list(map(int,input().split())))
B = sorted(list(map(int,input().split())))
C = sorted(list(map(int,input().split())))
B1 = []
for i in range(N):
    ind = bisect_right(C,B[i])
    B1.append(ind)
B2 = [0 for _ in range(N+1)]
for i in range(N-1,-1,-1):
    B2[i] = B2[i+1]+N-B1[i]
cnt = 0
for i in range(N):
    ind = bisect_right(B,A[i])
    cnt += B2[ind]
print(cnt)

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
As = sorted(list(mapint()))
Bs = sorted(list(mapint()))
Cs = sorted(list(mapint()))

from bisect import bisect_left, bisect_right

ans = 0
for b in Bs:
    a_idx = bisect_left(As, b)
    c_idx = N-bisect_right(Cs, b)
    ans += a_idx*c_idx
print(ans)

Solution (Python):
from bisect import bisect_left
n=int(input())
a=sorted(list(map(int,input().split())))
b=sorted(list(map(int,input().split())))
c=sorted(list(map(int,input().split())))
d=[0]
e=[]
for i in range(n):
    d.append(bisect_left(a,b[i])+d[-1])
    e.append(bisect_left(b,c[i]))
ans=0
for i in e:
    ans+=d[i]
print(ans)

Solution (Python):
n = int(input())
alst = list(map(int, input().split()))
blst = list(map(int, input().split()))
clst = list(map(int, input().split()))
alst.sort()
blst.sort()
clst.sort()

b_s = [0 for _ in range(n)]
a_pos = 0
alst.append(10 ** 10)
for i in range(n):
    while blst[i] > alst[a_pos]:
        a_pos += 1
    b_s[i] = a_pos + b_s[i - 1]
b_pos = 0
blst.append(10 ** 10)
ans = 0
for i in range(n):
    while clst[i] > blst[b_pos]:
        b_pos += 1
    if b_pos == 0:
        continue
    ans += b_s[b_pos - 1]
print(ans)

Solution (Python):
from bisect import bisect_left, bisect_right
n = int(input())
a = sorted(list(map(int, input().split())))
b = sorted(list(map(int, input().split())))
c = sorted(list(map(int, input().split())))

ans = 0
for i in range(n):
    x = b[i]
    l = bisect_left(a, x)
    r = bisect_right(c, x)
    ans += l * (n - r)
print(ans)

Solution (Python):
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  
def LS2(): return list(sys.stdin.readline().rstrip())  


N = I()
A,B,C = LI(),LI(),LI()
A.sort()
C.sort()

from bisect import bisect_left,bisect_right

ans = 0
for i in range(N):
    b = B[i]
    ans += bisect_left(A,b)*(N-bisect_right(C,b))

print(ans)

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

from bisect import bisect_left, bisect
ans = 0
for b in B:
    i = bisect_left(A,b)
    j = bisect(C,b)
    ans += i * (N-j)
print(ans)

Solution (Python):

import sys

sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read

from collections import defaultdict
sys.setrecursionlimit(10**7)


import bisect






INF = 1 << 50
EPS = 1e-8
mod = 10 ** 9 + 7


def run():
    N = int(sysread())
    A = list(map(int, sysread().split()))
    B = list(map(int, sysread().split()))
    C = list(map(int, sysread().split()))
    A.sort()
    B.sort()
    C.sort()
    ansBa = []
    ansBc = []
    for i in range(N):
        b = B[i]
        idx = bisect.bisect_left(A, b)
        ansBa.append(idx)

        idx2 = bisect.bisect_right(C, b)
        ansBc.append(N-idx2)

    
    ans = 0
    for aa, cc in zip(ansBa, ansBc):
        ans += aa * cc


    print(ans)

if __name__ == "__main__":
    run()

Solution (Python):
import bisect
from collections import deque
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
A.sort()
B.sort()
C.sort()

d = deque([])
for i in range(N):
  n = bisect.bisect_right(C, B[N-1-i])
  if len(d) == 0:
    d.appendleft(N-n)
  else:
    d.appendleft(N-n+d[0])
  
ans = 0
for i in range(N):
  m = bisect.bisect_right(B, A[i])
  if m < N:
    ans += d[m]
print(ans)

Solution (Python):
from bisect import bisect_right
from itertools import accumulate

def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    C = list(map(int, input().split()))
    A.sort()
    B.sort()
    C.sort()
    B_count = [0] * N
    for i in range(N):
        index = bisect_right(C,B[i])
        if index < N:
            B_count[i] = N - index
    D = [0] + list(accumulate(B_count))
    ans = 0
    for a in A:
        index = bisect_right(B,a)
        ans += D[N] - D[index]

    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))
A.sort()
B.sort()
C.sort()
ans=0
from bisect import bisect_left,bisect_right
for i in B:
   ans+=(bisect_left(A,i))*(N-bisect_right(C,i))
print(ans)

Solution (Python):
import bisect
n=int(input())
a=[int(x) for x in input().split()]
b=[int(x) for x in input().split()]
c=[int(x) for x in input().split()]
a.sort()
b.sort()
c.sort()


ans=0
ind=[0]
for i in range(n):
  ind.append(n-bisect.bisect_right(c,b[i])+ind[i])

for i in a:
  b_ind=bisect.bisect_right(b,i)

  ans+=ind[n]-ind[b_ind]

print(ans)

Solution (Python):
from bisect import *
N = int(input())
A = sorted(map(int, input().split()))
B = sorted(map(int, input().split()))
C = sorted(map(int, input().split()))
 
cnt = []
 
for b in B:
    cnt.append(bisect_left(A, b) * (N - bisect_right(C, b)))
print(sum(cnt))

Solution (Python):
from bisect import bisect_right, bisect_left
N = int(input())
*A, = map(int, input().split())
*B, = map(int, input().split())
*C, = map(int, input().split())
A.sort()
B.sort()
C.sort()
lc = len(C)
ans = 0
for b in B:
    a = bisect_left(A, b)
    c = len(C) - bisect_right(C, b)
    ans += a * c
print(ans)

Solution (Python):
n = int(input())
a = sorted(list(map(int,input().split())))
b = sorted(list(map(int,input().split())))
c = sorted(list(map(int,input().split())))
bn = [0]*n
j = 0
for i in range(n):
  bi = b[i]
  while(j<n):
    if bi < c[j]:
      bn[i] = n-j
      break
    j += 1
bnt = sum(bn)
bs = 0
j = 0
ans = 0
for i in range(n):
  ai = a[i]
  while(j<n):
    if ai < b[j]:
      ans += bnt - bs
      break
    bs += bn[j]
    j += 1
print(ans)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))
A=sorted(A)
B=sorted(B)
C=sorted(C)
A2=[0]*N
s=0
i=0
n=A[s]
while True:
  if B[i]>n:
    A2[i]+=1
    s+=1
    if s==N:
      break
    n=A[s]
  else:
    i+=1
  if i==N:
    break
for i in range(1,N):
  A2[i]=A2[i-1]+A2[i]
B2=[0]*N
s=0
i=0
n=B[s]
while True:
  if C[i]>n:
    B2[i]+=A2[s]
    s+=1
    if s==N:
      break
    n=B[s]
  else:
    i+=1
  if i==N:
    break
for i in range(1,N):
  B2[i]=B2[i-1]+B2[i]
print(sum(B2))

Solution (Python):
N=int(input())
A,B,C=[sorted(map(int,input().split())) for i in range(3)]
i,j=0,0
r=0
for b in B:
    while i<N and A[i]<b:
        i+=1
    while j<N and C[j]<=b:
        j+=1
    r+=i*(N-j)
print(r)

Solution (Python):


from bisect import bisect_left , bisect_right
def main():
	n = int(input())
	a = list(map(int , input().split()))
	b = list(map(int, input().split()))
	c = list(map(int ,input().split()))
	a.sort()
	b.sort()
	c.sort()
	ans=0
	for i in b:
		k = bisect_left(a,i)
		l = bisect_right(c,i)
		ans = ans+(k*(n-l))
	print(ans)
if __name__=="__main__":
	main()

Solution (Python):

from bisect import bisect_left , bisect_right
def main():
	n = int(input())
	a = list(map(int , input().split()))
	b = list(map(int, input().split()))
	c = list(map(int ,input().split()))
	a.sort()
	b.sort()
	c.sort()
	ans=0
	for i in b:
		k = bisect_left(a,i)
		l = bisect_right(c,i)
		ans = ans+(k*(n-l))
	print(ans)
if __name__=="__main__":
	main()

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

N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
A.sort();B.sort();C.sort()



Bok = []
for b in B:
  temp = bisect.bisect_left(A,b) 
  Bok.append(temp) 
SB = [0]
for i in range(N):
  temp = SB[-1]+Bok[i]
  SB.append(temp)

ans = 0
for c in C:
  temp = bisect.bisect_left(B,c) 
  
  ans += SB[temp]
print(ans)

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


n = int(input())
A = sorted([int(i) for i in input().split()])
B = sorted([int(i) for i in input().split()])
C = sorted([int(i) for i in input().split()])

A_B = [0] * n
B_C = [0] * n
ans = 0


for idx, a in enumerate(A):
    A_B[idx] = bisect.bisect_right(B, a)

for idx, b in enumerate(B):
    B_C[idx] = n - bisect.bisect_right(C, b)

cul_sum = [0] * (n + 1)

for i in range(1, n + 1):
    cul_sum[i] = cul_sum[i - 1] + B_C[i - 1]


for idx in A_B:
    ans += cul_sum[n] - cul_sum[idx]

print(ans)

Solution (Python):

        
def main():
    import sys
    from bisect import bisect_left, bisect_right
    def input(): return sys.stdin.readline().rstrip()

    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    c = list(map(int, input().split()))
    a.sort()
    b.sort()
    c.sort()
    ans = 0
    for j in range(n):
        i = bisect_left(a, b[j])
        k = bisect_right(c, b[j])
        ans += i*(n-k)

    print(ans)
        




                
if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
a=sorted(list(map(int,input().split())))
b=list(map(int,input().split()))
c=sorted(list(map(int,input().split())))
import bisect
ans=0
for i in range(n):
    inda=bisect.bisect_left(a,b[i])
    indc=bisect.bisect_right(c,b[i])
    ans += inda*(n-indc)
print(ans)

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


from bisect import bisect_left, bisect_right
import heapq

sys.setrecursionlimit(1000000)


input = sys.stdin.readline

getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = 10 ** 20
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)


def nck(n, k, kaijyo):
    return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD


def npk(n, k, kaijyo):
    if k == 0 or k == n:
        return n % MOD
    return (kaijyo[n] * divide(kaijyo[n-k])) % MOD


def fact_and_inv(SIZE):
    inv = [0] * SIZE  
    fac = [0] * SIZE  
    finv = [0] * SIZE  
    inv[1] = 1
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    for i in range(2, SIZE):
        inv[i] = MOD - (MOD // i) * inv[MOD % i] % MOD
        fac[i] = fac[i - 1] * i % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    return fac, finv

def kaijyo(n):
    ret = [1]
    for i in range(1, n + 1):
        ret.append((ret[-1] * i)% MOD)
    return ret

def warshall_floyd(d, n):
    
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d

class TwoDimGrid:
    
    def __init__(self, h, w, wall="
        self.h = h
        self.w = w
        self.size = (h+2) * (w+2)
        self.wall = wall
        self.get_grid()

    def get_grid(self):
        grid = [self.wall * (self.w + 2)]
        for i in range(self.h):
            grid.append(self.wall + getS() + self.wall)

        grid.append(self.wall * (self.w + 2))
        self.grid = grid

    def pos(self, x, y):
        return y * (self.w + 2) + x + 1

    def search(self):
        grid = self.grid
        move = [(0, 1), (0, -1), (1, 0), (-1, 0)]
        move_eight = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]

        ans = 0
        for i in range(1, self.h + 1):
            for j in range(1, self.w + 1):
                pass 

def solve():
    n = getN()
    anums = getList()
    bnums = getList()
    cnums = getList()
    anums.sort()
    bnums.sort()
    cnums.sort()
    ka = [0]
    for bnum in bnums:
        ka.append(bisect_left(anums, bnum))


    for i in range(len(ka) - 1):
        ka[i+1] += ka[i]
    
    ans = 0
    for cnum in cnums:
        ans += ka[bisect_left(bnums, cnum)]

    print(ans)

def main():
    n = getN()
    for _ in range(n):
        solve()


if __name__ == "__main__":
    
    solve()

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

N = int(readline())
A = sorted(list(map(int,readline().split())))
B = list(map(int,readline().split()))
C = sorted(list(map(int,readline().split())))

ans = 0
import bisect
for i in range(len(B)):
  ind_a = bisect.bisect_left(A, B[i])
  ind_c = bisect.bisect_right(C, B[i])
  ans += ind_a * (len(C) - ind_c)
  
print(ans)

Solution (Python):
from bisect import *
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))

a.sort()
c.sort()
res = 0
for i in range(n):
    temp = b[i]
    res += bisect_left(a, temp) * (n - bisect_right(c, temp))
    
print(res)

Solution (Python):
from bisect import*
(n,), a, b, c = [sorted(map(int, o.split())) for o in open(0)]
print(sum(bisect_left(a, i) * (n - bisect(c, i)) for i in b))

Solution (Python):


import bisect

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

a.sort()
b.sort()
c.sort()

ba = []
baccum = []
tmp = 0
for bb in b:
    ba.append(bisect.bisect_right(a, bb-1))
    tmp += ba[-1]
    baccum.append(tmp) 

cb = []
for cc in c:
    cb.append(bisect.bisect_right(b, cc-1))

ret = 0
for cc in cb:
    if cc == 0:
        continue
    ret += baccum[cc-1] 

print(ret)

Solution (Python):
import sys, math
import bisect
sys.setrecursionlimit(500000)
MOD = 10**9+7

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

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]

def main():
    N = ii()
    A, B, C = [sorted(list(mi())) for i in range(3)]
    la = [bisect.bisect_left(A, b) for b in B]
    lc = [N-bisect.bisect_right(C, b) for b in B]
    print(sum(la[i]*lc[i] for i in range(N)))

if __name__ == '__main__':
    main()

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

a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
a.sort()
b.sort()
c.sort()

from bisect import bisect_left,bisect_right
ans = 0
for i in range(n):
    pos_a = bisect_left(a,b[i])
    pos_c = bisect_right(c,b[i])
    ans += pos_a*(n-pos_c)

print(ans)

Solution (Python):
import bisect
n=int(input())
a=sorted(list(map(int,input().split())))
b=list(map(int,input().split()))
c=sorted(list(map(int,input().split())))
sm=0
for i in range(n):
  b1=bisect.bisect_left(a,b[i])
  c1=n-bisect.bisect_left(c,b[i]+1)
  sm+=b1*c1
print(sm)

Solution (Python):
import bisect
n=int(input())
a=sorted(list(map(int,input().split())))
b=sorted(list(map(int,input().split())))
c=sorted(list(map(int,input().split())))
sm=0
for i in range(n):
  b1=bisect.bisect_left(a,b[i])
  c1=n-bisect.bisect_left(c,b[i]+1)
  sm+=b1*c1
print(sm)

Solution (Python):
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007



def main():
	n = int(input())
	
	
	al = lin()
	bl = lin()
	cl = lin()
	al.sort()
	bl.sort()
	cl.sort()
	cl_num=bl[:]
	cl_pos=0
	for i, vb in enumerate(bl):
		while cl_pos <n and cl[cl_pos] <= vb:
			cl_pos += 1
		cl_num[i] = n-cl_pos
		
	cl_nokori=[0]*n
	cl_nokori[-1] = cl_num[-1]
	for i, v in enumerate(cl_num[::-1]):
		if i == 0:
			continue
		cl_nokori[n-i-1] = cl_nokori[n-i]+v
	
	ret = 0
	bl_pos=0
	for va in al:
		while bl_pos < n and va >= bl[bl_pos]:
			bl_pos += 1
		if bl_pos == n:
			break
		ret += cl_nokori[bl_pos]
	print(ret)
		
	
	

isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

Solution (Python):
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
a = sorted(LIST())
b = sorted(LIST())
c = sorted(LIST())

ans = 0
for x in b:
    i = bisect_left(a, x)
    j = n - bisect_right(c, x)
    ans += i * j
print(ans)

Solution (Python):
import sys
import bisect

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

A.sort()
B.sort()
C.sort()



ans = 0
for i in range(N):
    mid = B[i]
    top = bisect.bisect_left(A, mid)
    down = bisect.bisect_right(C, mid)
    
    ans += top * (N - down)

print(ans)

Solution (Python):
import bisect
import sys
n = int(input())
up = sorted(list(map(int, input().split())))
mid = sorted(list(map(int, input().split())))
low = sorted(list(map(int, input().split())))
ans = 0
for i in range(len(mid)):
    pre = bisect.bisect_left(up, mid[i])
    over = len(low) - bisect.bisect(low, mid[i])
    ans += pre * over

print(ans)

Solution (Python):


import sys
read = sys.stdin.read
readline = sys.stdin.readline

n, = map(int, readline().split())
*a, = map(int, readline().split())
*b, = map(int, readline().split())
*c, = map(int, readline().split())

a.sort()
c.sort()

from bisect import bisect_left, bisect_right

ans = 0
for bi in b:
    ans += bisect_left(a,bi) * (n-bisect_right(c,bi))

print(ans)

Solution (Python):
import bisect
n=int(input())
a=sorted(list(map(int,input().split())))
b=sorted(list(map(int,input().split())))
c=sorted(list(map(int,input().split())))
d=0
for t in b:
    d+=bisect.bisect_left(a,t)*(n-bisect.bisect_right(c,t))
print(d)

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

A = sorted(list(map(int, input().split())))
B = sorted(list(map(int, input().split())))
C = sorted(list(map(int, input().split())))

ans = 0
for b in B:
    a = bisect.bisect_left(A, b)
    c = bisect.bisect_right(C, b)
    ans += a*(N-c)
print(ans)

Solution (Python):
from bisect import bisect_left, bisect_right

N = int(input())


A = sorted(list(map(int, input().split())))
B = sorted(list(map(int, input().split())))
C = sorted(list(map(int, input().split())))

ans = 0

for i in range(N):
    
    b = B[i]
    
    
    a_ix = bisect_left(A, b)
    
    c_ix = bisect_right(C, b)
    
    
    
    ans += a_ix * (N - c_ix)

print(ans)

Solution (Python):
import bisect
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
A.sort()
C.sort()
ans = 0
for b in B:
    ans += (bisect.bisect_left(A,b)*(n-bisect.bisect_right(C,b)))
print(ans)

Solution (Python):
'''
研究室PCでの解答
'''
import math

import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit


ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n = int(ipt())
    a = [int(i) for i in ipt().split()]
    b = [int(i) for i in ipt().split()]
    c = [int(i) for i in ipt().split()]

    a.sort()
    b.sort()
    c.sort()

    sb = [0]*n
    na = 0
    for i,bi in enumerate(b):
        for j in range(na,n):
            if a[j] >= bi:
                sb[i] = j
                na = j
                break
            if j == n-1:
                na = n
                break
        if na == n:
            sb[i] = n
    ssb = [0]
    for i in sb:
        ssb.append(ssb[-1]+i)

    ans = 0
    nb = 0
    for i,ci in enumerate(c):
        for j in range(nb,n):
            if b[j] >= ci:
                ans += ssb[j]
                nb = j
                break
            if j == n-1:
                nb = n
                break
        if nb == n:
            ans += ssb[-1]
    print(ans)

    return None

if __name__ == '__main__':
    main()

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

A = sorted(A)
B = sorted(B)
C = sorted(C)


import bisect as bis

BSC = [0]*N
ASB = [0]*N

for i in range (0, N):
	BSC[i] = N-bis.bisect_right(C, B[i])

for i in range (0, N):
	ASB[i] = bis.bisect_right(B, A[i])
    
for i in range (1, N):
	BSC[N-1-i] += BSC[N-i]

count = 0
for i in range (0, N):
	if ASB[i] ==N:
		continue
	else:
		count+=BSC[ASB[i]]

print(count)

Solution (Python):
def main():
    from bisect import bisect_right
    N = int(input())
    A = sorted(list(map(int,input().split())))
    B = sorted(list(map(int,input().split())))
    C = sorted(list(map(int,input().split())))
    
    num_greater_than_b = []
    for b in B:
        num_greater_than_b.append(N-bisect_right(C,b))

    csum = [0]*(N+1)
    for i in range(N-1,-1,-1):
        csum[i] = csum[i+1]+num_greater_than_b[i]
        
    ans = 0
    for i,a in enumerate(A):
        ans += csum[bisect_right(B,a)]
    print(ans)
main()

Solution (Python):
from bisect import bisect_left, bisect_right  


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

ans = 0

for i in range(N):
    b = B[i]
    a_ix = bisect_left(A, b)
    c_ix = bisect_right(C, b)
    ans += a_ix * (N - c_ix)

print(ans)