f (u:ts@(t:_)) (a:as@(b:_))
  | t > u && a > b && t /= a = 0
  | t > u || a > b = f ts as
  | otherwise = min t a * f ts as
f _ _ = 1

main = do
  _ <- getLine
  ts <- fmap (map read . words) getLine
  as <- fmap (map read . words) getLine
  if last ts /= head as then
    print 0
  else
    print $ f ts (tail as) `mod` (10^9+7)