let m = 1000000007
let ( *^ ) x y = (x * y) mod m

let () = Scanf.scanf "%d\n" @@ fun n ->
  let ts = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun t -> t in
  let as_ = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun t -> t in
  let dp = Array.make n `Undefined in
  Printf.printf "%d\n" @@
  try
    if ts.(n - 1) <> as_.(0) then raise Not_found;
    dp.(0) <- `Defined ts.(0);
    for i = 0 to n - 2 do
      dp.(i + 1) <-
        if ts.(i) = ts.(i + 1)
        then `Under ts.(i)
        else `Defined ts.(i + 1)
    done;
    begin match dp.(n - 1) with
    | `Defined h ->
        if h <> as_.(n - 1) then raise Not_found
    | `Under h ->
        if h < as_.(n - 1) then raise Not_found
        else dp.(n - 1) <- `Defined as_.(n - 1)
    end;
    for i = n - 2 downto 0 do
      match dp.(i), as_.(i) = as_.(i + 1) with
      | `Under h, true ->
          dp.(i) <- `Under (min h as_.(i))
      | `Defined h, true ->
          if as_.(i) < h then raise Not_found
      | `Under h, false ->
          if h < as_.(i) then raise Not_found
          else dp.(i) <- `Defined as_.(i)
      | `Defined h, false ->
          if as_.(i) <> h then raise Not_found
    done;
    Array.fold_left (fun acc -> function
      | `Under h -> acc *^ h
      | _ -> acc) 1 dp
  with Not_found -> 0
