// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
import std.datetime, std.bigint;

immutable long mod = 10^^9 + 7;
int n;
int[] t, a;

void main() {
    scan(n);
    t = readln.split.to!(int[]);
    a = readln.split.to!(int[]);

    auto L = new int[](n);
    auto H = new int[](n);

    L[0] = H[0] = t[0];

    foreach (i ; 1 .. n) {
        if (t[i - 1] < t[i]) {
            L[i] = H[i] = t[i];
        }
        else {
            L[i] = 1;
            H[i] = t[i];
        }
    }

    if (!(L[n - 1] <= a[n - 1] && a[n - 1] <= H[n - 1])) {
        writeln(0);
        return;
    }

    L[n - 1] = H[n - 1] = a[n - 1];

    foreach_reverse (i ; 0 .. n - 1) {
        if (a[i] > a[i + 1]) {
            if (L[i] <= a[i] && a[i] <= H[i]) {
                L[i] = H[i] = a[i];
            }
            else {
                writeln(0);
                return;
            }
        }
        else {
            H[i] = min(H[i], a[i]);
        }
    }

    long ans = 1;

    foreach (i ; 0 .. n) {
        (ans *= (H[i] - L[i] + 1)) %= mod;
    }

    writeln(ans);
}



void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}


void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}