#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#include <cfloat>
#define zero(x) (((x)>0?(x):-(x))<eps)

#define pause cout << " press ansy key to continue...",  cin >> chh
#define file_r(x) freopen(x,  "r",  stdin)
#define file_w(x) freopen(x,  "w",  stdout)
#define lowbit(x) ((x) & (-x))

#define repit(i, c) for (__typeof__((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, j, n, m) rep(i, n) rep (j, m)
#define repe(i, u) for (int i = head[u]; i != -1; i = nxt[i])
#define repd(i, n) for (int i = (n - 1); i >= 0; i--)
#define FOR(i, n, m) for (int i = (n); i <= (m); i++)
#define FORD(i, n, m) for (int i = (n); i >= (m); i--)

#define pb push_back
#define X first
#define Y second
#define ins insert
#define rb rbegin
#define be begin
#define er erase
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define SZ(c) (c).size()
#define ALL(c) (c).begin(), (c).end()
#define sqr(r) ((r) * (r))
#define dis(x1, y1, x2, y2) (((x1) - (x2)) * ((x1) - (x2)) + ((y1) - (y2)) * ((y1) - (y2)))
#define FASTIO ios::sync_with_stdio(false);cin.tie(0)

#define sc(x) cout << #x" = " << x << endl, system("pause")
#define sc2(x, y) cout << #x" = " << x << " " << #y" = " << y << endl, system("pause")
#define sc3(x, y, z) cout << #x" = " << x << " " << #y" = " << y << " " << #z" = " << z << endl, system("pause")
#define sc4(x, y, z, w) cout << #x" = " << x << " " << #y" = " << y << " " << #z" = " << z << " " << #w" = " << w << endl, system("pause")
#define sca(A, n, m) {                      \
  rep(i, n){                                    \
        rep(j, m - 1) cout << A[i][j] << " ";     \
        cout << A[i][m-1] << endl;              \
    }                                           \
}
#define sca1(A, n, m) {                    \
    FOR(i, 1, n){                                \
        FOR(j, 1, m-1) cout << A[i][j] << " ";   \
        cout << A[i][m] << endl;                \
    }                                           \
}

#define in(n) scanf("%d", &n)
#define in2(n, m) scanf("%d %d", &n, &m)
#define in3(x, y, z) scanf("%d %d %d", &x, &y, &z)
#define in4(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)

using namespace std;
int chh;

typedef vector<int> vi;
typedef set<int> si;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<int, pii> pi3;
typedef vector< pair<int, int> > vpii;
typedef long long LL;

const int N = 100005, MOD = 1000000007;

int n;
int a[N], b[N], c[N], d[N];

bool gao() {
    int flag = 1;
    c[0] = d[0] = a[0];
    FOR (i, 1, n - 1) {
        if (a[i] > a[i - 1]) c[i] = d[i] = a[i];
        else c[i] = 1, d[i] = a[i];
        //sc3(i, c[i], d[i]);
    }
    if (b[n - 1] < c[n - 1] || b[n - 1] > d[n - 1]) return 0;
    c[n - 1] = d[n - 1] = b[n - 1];
    FORD (i, n - 2, 0) {
        if (b[i] > b[i + 1]) {
            if (b[i] < c[i] || b[i] > d[i]) return 0;
            c[i] = d[i] = b[i];
        } else {
            d[i] = min(d[i], b[i]);
            if (c[i] > d[i]) return 0;
        }
        //sc3(i, c[i], d[i]);
    }
    return 1;
}

int main() {
    int ans, flag;
    while (~in(n)) {
        rep (i, n) in(a[i]);
        rep (i, n) in(b[i]);
        flag = gao();
        if (!flag) puts("0");
        else {
            ans = 1;
            rep (i, n) ans = (LL) ans * (d[i] - c[i] + 1) % MOD;
            printf("%d\n", ans);
        }
    }
    return 0;
}


Measured memory_bytes: 1920