import Control.Applicative
import Control.Monad.State
import Control.Monad
import Data.List
import Data.Either
import Data.Int

main = do
  n <- readLn :: IO Int
  ts <- map read . words <$> getLine :: IO [Int64]
  as <- map read . words <$> getLine :: IO [Int64]


  let
    ts' = (`evalState` 0) $ do
      forM ts $ \t -> do
        x <- get
        put t
        return $ if t == x then Left t else Right t

    as' = reverse $ (`evalState` 0) $ do
      forM (reverse as) $ \a -> do
        x <- get
        put a
        return $ if a == x then Left a else Right a  
  
  print $ (foldl (\x y -> (x*y)`mod`(10^9+7)) 1 $ zipWith f as' ts')

  where
    f (Left x) (Left y) = min x y
    f (Left x) (Right y) = if x >= y then 1 else 0
    f (Right x) (Left y) = if x <= y then 1 else 0
    f (Right x) (Right y) = if x == y then 1 else 0
