import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void log(A...)(A arg) {
    stderr.writeln(arg);
}
int size(T)(in T s) {
    return cast(int)s.length;
}

void main() {
    int N; scanf("%d\n", &N);
    auto T = readln.chomp.split(" ").map!(to!int).array;
    auto A = readln.chomp.split(" ").map!(to!int).array;

    auto fixed = new bool[N];
    auto limit = new long[N];

    int t = 0;
    foreach (i; 0 .. N) {
        int prev = t;
        t = max(t, T[i]);
        if (prev == t) {
            limit[i] = t;
        } else {
            fixed[i] = true;
            limit[i] = t;
        }
    }
    int a = 0;
    foreach_reverse(i; 0 .. N) {
        int prev = a;
        a = max(a, A[i]);
        if (fixed[i]) {
            if (limit[i] > a) {
                writeln(0);
                return;
            }
        }
        if (prev == a) {
            limit[i] = min(limit[i], a);
        } else {
            if (limit[i] < a) {
                writeln(0);
                return;
            }
            fixed[i] = true;
        }
    }

    const long mod = cast(long)(1e9 + 7);
    long ans = 1;
    foreach (i; 0 .. N) {
        if (fixed[i]) continue;
        ans = (ans * limit[i]) % mod;
    }
    writeln(ans);

}
