Scanf.scanf "%d" (fun n ->
    let t = Array.init n (fun _ -> Scanf.scanf " %d" (fun t -> t)) in
    let a = Array.init n (fun _ -> Scanf.scanf " %d" (fun t -> t)) in
    let mi = Array.make n 0 in
    let ma = Array.make n 0 in
    
    let rec loop i mx =
        if i < n then (
            if t.(i) > mx then (
                mi.(i) <- t.(i);
                ma.(i) <- t.(i);
                loop (i + 1) t.(i)
            ) else (
                mi.(i) <- 1;
                ma.(i) <- t.(i);
                loop (i + 1) t.(i)
            )
        )
    in
    loop 0 0;

    let rec loop i mx =
        if i >= 0 then (
            if a.(i) > mx then (
                if mi.(i) <= a.(i) &&
                   ma.(i) >= a.(i) then (
                       mi.(i) <- a.(i);
                       ma.(i) <- a.(i);
                ) else (
                    mi.(i) <- 0;
                    ma.(i) <- -1;
                );
                loop (i - 1) a.(i)
            ) else (
                ma.(i) <- min ma.(i) mx;
                loop (i - 1) a.(i)
            )
        )
    in
    loop (n - 1) 0;

    let m = 1_000_000_007 in
    let ( -@) a b = (a - b + m) mod m in
    let ( *@) a b = ((a mod m) * (b mod m)) mod m in

    let rec loop i acc =
        if i = n then acc else
            loop (i + 1) (acc *@ (ma.(i) -@ mi.(i) + 1))
    in
    loop 0 1 |> Printf.printf "%d\n"
)