let modulo = 1000000007

let ( +% ) a b = let c = a + b in if c > modulo then c - modulo else c
let ( -% ) a b = if a >= b then a - b else modulo + a - b
let ( *% ) a b = a * b mod modulo

let n = read_int ()

let rec read_ints accu = function
  | 0 -> List.rev accu
  | k -> read_ints ((Scanf.scanf " %d" (fun x -> x)) :: accu) (k-1)

let tl = Array.of_list (read_ints [] n)
let al = Array.of_list (read_ints [] n)


let b = Array.make n 1


let () = match n with
  | 1 -> print_int (if tl.(0) = al.(0) then 1 else 0)
  | _ ->
  for i = 1 to n-2 do
    b.(i) <- if tl.(i-1) < tl.(i) && al.(i) > al.(i+1) then if tl.(i) = al.(i) then 1 else 0
      else if tl.(i-1) < tl.(i) || al.(i) > al.(i+1) then 1
      else (min tl.(i) al.(i))
  done;
  if al.(0) > al.(1) && al.(0) != tl.(0) then b.(0) <- 0;
  if tl.(n-1) > tl.(n-2) && al.(n-1) != tl.(n-1) then b.(n-1) <- 0;

  print_int (Array.fold_left ( *% ) 1 b);