import Data.Bool  (bool)
import Data.Maybe (fromJust)

import qualified Data.List as L

main :: IO ()
main = do
    n  <- read <$> getLine
    ts <- fmap read . words <$> getLine
    as <- fmap read . words <$> getLine
    answer $ solve n ts as

type Answer = Int
answer :: Answer -> IO ()
answer = print

modular = 10 ^ 9 + 7

solve :: Int -> [Int] -> [Int] -> Answer
solve n ts as
    | n == 1       = bool 0 1 $ ts == as
    | incompatible = 0
    | otherwise    = L.foldl ((.) (`mod` modular) . (*)) 1 zs
  where
    zs = reduce $ L.zipWith min ts as
    incompatible = fromJust $ do
        let k = L.maximum ts
        i <- L.elemIndex k ts
        return $ as !! i /= k

    reduce (x0 : xs@(x1 : x2 : _))
      | x0 < x1   =      reduce xs
      | x1 > x2   =      reduce xs
      | otherwise = x1 : reduce xs
    reduce _      = []
