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

main :: IO ()
main = do
  let
    sub :: [a] -> [[a]]
    sub = snd . sub' . init . tails
      where
        -- sub' :: [[a]] -> ([a], [[a]])
        sub' = foldr step ([], [])
          where
            step xs (xs', xss') = (xs, xs' : map (head xs :) xss')
    permutations :: [a] -> [[a]]
    permutations [] = [[]]
    permutations xs = concatMap (map <$> (:) . fst <*> permutations . snd) . (zip <$> id <*> sub) $ xs
  n <- unsafeTextToInt <$> T.getLine :: IO Int
  let
    perms = permutations [1..n]
  ps <- map unsafeTextToInt . T.words <$> T.getLine :: IO [Int]
  let
    a = length . takeWhile (/= ps) $ perms
  qs <- map unsafeTextToInt . T.words <$> T.getLine :: IO [Int]
  let
    b = length . takeWhile (/= qs) $ perms
  print . abs $ a - b

unsafeTextToInt :: T.Text -> Int
unsafeTextToInt s = case T.signed T.decimal s of
  Right (n, _) -> n
{-# INLINE unsafeTextToInt #-}
