implicit none
integer :: n
integer,allocatable :: a(:),b(:),c(:),dp(:)
integer :: i,j,ans,s

read *,n
allocate(a(n))
read *,a
call cs11(n,a)
allocate(b(n))
read *,b
call cs11(n,b)
allocate(c(n))
read *,c
call cs11(n,c)

allocate(dp(0:n))
dp(0) = 0

j = 1
do i = 1, n
   do while (j < n .and. a(j) < b(i))
      j = j + 1
   enddo
   if (j == n .and. a(n) < b(i)) then
      dp(i:n) = n
      exit
   endif
   dp(i) = j - 1
enddo

ans = 0
j = 1
s = 0
do i = 1, n
   do while (j < n .and. b(j) < c(i))
      s = s + dp(j)
      j = j + 1
   enddo
   if (j == n .and. b(j) < c(i)) then
      s = s + dp(n)
      ans = ans + s * (n-i+1)
      exit
   endif
   ans = ans + s
enddo

print '(i0)',ans

stop

contains

subroutine cs11(Ncomb,a)
integer, intent(in) :: Ncomb
integer, intent(inout) :: a(Ncomb)
integer :: icomb, jcomb,rswp
logical :: flg_swp

icomb = Ncomb
flg_swp = .false.
do while (icomb > 1 .or. flg_swp)
   icomb = (icomb * 10) / 13
   if (icomb < 1) then
      icomb = 1
   else if (icomb == 9 .or. icomb == 10) then 
      icomb = 11
   endif
   flg_swp = .false.
   do jcomb = 1, Ncomb-icomb
      if (a(jcomb) > a(jcomb+icomb)) then
         rswp = a(jcomb) 
         a(jcomb) = a(jcomb+icomb)
         a(jcomb+icomb) = rswp
         flg_swp = .true.
      endif
   enddo
enddo
endsubroutine cs11

end