import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;

enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;


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

    auto m = new int[](n);
    auto h = new int[](n);
    h[0] = t[0];

    foreach (i ; 1 .. n) {
        if (t[i] > t[i - 1]) {
            h[i] = t[i];
        }
        else {
            m[i] = t[i];
        }
    }

    long ans = 1;

    if (h[n - 1] > 0) {
        if (a[n - 1] != h[n - 1]) {
            ans = 0;
        }
    }
    else {
        if (a[n - 1] > m[n - 1]) {
            ans = 0;
        }
    }

    foreach_reverse (i ; 0 .. n - 1) {
        if (h[i] > 0) {
            if (a[i] < h[i]) {
                ans = 0;
                break;
            }
        }
        else if (a[i] > a[i + 1]) {
            if ((h[i] > 0 && a[i] != h[i]) || (m[i] > 0 && m[i] < a[i])) {
                ans = 0;
                break;
            }
        }
        else {
            m[i] = min(m[i], a[i]);
            ans *= m[i];
            ans %= mod;
        }
    }

    writeln(ans);
}







void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    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);
        }
    }
}
