
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE FlexibleContexts #-}

-- 1. Don't parse Strings, parse Text -- using takes takes 
--    only 40% of the time of Strings.
-- 2. 

import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Read as T
import Data.Text (Text)
import qualified Data.List as L

import qualified Data.Vector.Unboxed as V
import Data.Vector.Unboxed (Vector)
import qualified Data.Vector.Unboxed.Mutable as VMut

import Control.Monad  ( replicateM )

import System.IO.Unsafe
import Data.Bits (shiftR)

{-# INLINE binarySearchPBounds #-}
binarySearchPBounds p vec = loop
 where
 loop !l !u
   | u <= l    = return l
   | otherwise = VMut.unsafeRead vec k >>= \e -> if p e then loop l k else loop (k+1) u
  where k = (u + l) `shiftR` 1

{-# INLINE binarySearchL #-}
binarySearchL vec e = binarySearchPBounds (>= e) vec 0 (VMut.length vec)

{-# INLINE binarySearchR #-}
binarySearchR vec e = binarySearchPBounds (> e) vec 0 (VMut.length vec)


forceEither :: Either a b -> b
forceEither (Right r) = r 

parseLineT :: Text -> [Int]
parseLineT txt =
  map (fst . forceEither . T.decimal) $ T.splitOn (T.pack " ") txt

getStdInputT :: IO [Text]  
getStdInputT = do
  res@[n, as, bs, cs] <- replicateM 4 T.getLine
  return res

bisectLeftV :: Int -> Vector Int -> Int
bisectLeftV n xs = unsafePerformIO $ do 
  xs' <- V.unsafeThaw xs
  binarySearchL xs' n

bisectRightV :: Int -> Vector Int -> Int
bisectRightV n xs = unsafePerformIO $ do 
  xs' <- V.unsafeThaw xs
  binarySearchR xs' n


sortedList :: t -> Text -> Vector Int
sortedList n txt = 
  V.fromList $ L.sort $ parseLineT txt


fhiyoProbTSrt
  :: [Text]
     -> (Int, Vector Int, [Int], Vector Int)
fhiyoProbTSrt [nStr, asStr, bsStr, csStr] = 
  let [n] = parseLineT nStr
      as =  sortedList n asStr
      bs =  parseLineT  bsStr
      cs = sortedList n csStr
  in (n, as, bs, cs)


fhiyoSolve (n, as, bs, cs) = 
  foldl (+) 0 $ map (\ b -> (bisectLeftV b as) * (n - bisectRightV b cs)) bs


stdinF = do
  ls <- getStdInputT 
  let problem = (fhiyoProbTSrt ls)
  print $ (fhiyoSolve problem)



main = 
  stdinF



