import Control.Monad
import Data.Maybe

main :: IO ()
main = do
  n <- readLn :: IO Int
  as <- getInts
  bs <- getInts
  let as'= decide as
      bs' = reverse $ decide (reverse bs)
      ms = zipWith min as bs
      xs = solve as' bs' ms
--  print as'
--  print bs'
--  print xs
  print $ fromMaybe 0 (solve' <$> xs)
  

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

decide :: [Int] -> [Int]
decide ns = go (zip ns (0 : init ns))
  where
    go [] = []
    go ((c, n):xs)
      | c == n = -c : go xs
      | otherwise = c : go xs

solve :: [Int] -> [Int] -> [Int] -> Maybe [Int]
solve as bs mins = go as bs mins
  where
    go [] _ _ = Just []
    go (x:xs) (y:ys) (m:ms)
      | x == y && x > 0= go xs ys ms
      | y < 0 && x < 0 = (m :) <$> go xs ys ms
      | y + x <= 0 = go xs ys ms
      | otherwise = Nothing

solve' :: [Int] -> Int
solve' ns = go ns
  where
    go [] = 1
    go (n:ns) = (n * go ns) `mod` 1000000007
