{-# LANGUAGE BangPatterns #-}

module Main
  ( main
  )
where
 
import           Control.Applicative
import           Control.Monad
import           Data.Bits
import qualified Data.ByteString.Char8         as BS
import           Data.Char
import           Data.List
import           Data.Maybe
import qualified Data.Vector                   as V
 
parse = fst . fromJust
parseInt = parse . BS.readInt
parseInteger = parse . BS.readInteger
readInt = parseInt <$> BS.getLine
readInteger = parseInteger <$> BS.getLine
readIntList = map parseInt . BS.words <$> BS.getLine
readIntegerList = map parseInteger . BS.words <$> BS.getLine

res = 1000000000 + 7

main = do
  n <- readInt
  p <- readIntList
  q <- readIntList
  let p_rank = calc n p
      q_rank = calc n q
  print $ abs (p_rank - q_rank)

calc n (x:xs) = let rank = foldl' (\acc val -> if val < x then acc + 1 else acc) 0 xs in rank * fact (n - 1) + calc (n - 1) xs
calc n [] = 0

fact 0 = 1
fact !n = n * fact (n - 1)
