{-# OPTIONS_GHC -O2 #-}
{-# LANGUAGE NumDecimals #-}
module Main (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 n ts as
  return ()
solve :: Integer -> [Integer] -> [Integer] -> Integer
solve n ts as
  | n == 1 = 1
  | otherwise = prod' s12
  where
    s1 = determine ts
    s2 = reverse $ determine $ reverse as
    s12 = zipWith f s1 s2
      where
        f (a,False) (b,False)        = min a b
        f (a,True) (b,True) | 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,True) : zip (tail xs) sublist
  where
    sublist = zipWith (/=) xs $ tail xs
