{-# OPTIONS_GHC -O2 #-}
{-# LANGUAGE NumDecimals #-}
module Main (main) where
import Data.List
-- import Debug.Trace
import qualified Data.ByteString             as S
import qualified Data.ByteString.Char8       as S
import Data.Maybe (fromJust)
getInts :: IO [Integer]
getInts = fmap readInts S.getLine

readInts :: S.ByteString -> [Integer]
readInts = map (fst . fromJust . S.readInteger). S.words

main :: IO ()
main = do
  n:_ <- getInts
  ts <- getInts
  as <- getInts
  print $ solve n ts as
  return ()

solve :: Integer -> [Integer] -> [Integer] -> Integer
solve n ts as = prod' $ zipWith f ts' as'
  where
    ts' = determine ts
    as' = reverse $ determine $ reverse as
    f (t,False) (a,False)        = min t a
    f (t,True) (a,True) | t /= a = 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 = zip xs $ zipWith (/=) (0:xs) xs

