main = do
    n <- getLine
    p <- map read <$> words <$> getLine
    q <- map read <$> words <$> getLine
    print $ abs $ solve p - solve q

solve :: [Int] -> Int
solve []      = 0
solve (h : t) = (h - 1) * product [1 .. length t] + solve (next h t)

next a []      = []
next a (h : t) = (if h > a then h - 1 else h) : next a t
