{-# LANGUAGE NumDecimals #-}
module Main where
import Data.List
import Debug.Trace

getInt :: IO Integer
getInt = read <$> getLine

getInts :: IO [Integer]
getInts = map read . words <$> getLine

main :: IO ()
main = do
  n <- getInt
  ts <- getInts
  as <- getInts
  print $ solve ts as
  return ()
solve :: [Integer] -> [Integer] -> Integer
solve ts as = prod' s12
  where
    s1 = determine ts
    s2 = reverse $ determine $ reverse as
    s12 = zipWith f s1 s2
      where
        f (a,True) (b,True) = min a b
        f (a,False) (b,False)
          | a /= b = 0
        f _ _ = 1

prod' :: [Integer] -> Integer
prod' = foldl f 1
  where
    f a b = a * b `mod` (1e9 + 7)

determine :: [Integer] -> [(Integer,Bool)]
determine xs = (head xs,False) : zipWith f sublist (tail xs)
  where
    sublist = map (>=0) . zipWith (-) xs $ tail xs
    f b x = if b
            then (x,True)
            else (x,False)
