#include<bits/stdc++.h>

using namespace std;
using ll = long long;

const ll mod = 1e9 + 7;
int main()
{
    int n; cin >> n;
    vector<ll> a(n), b(n);
    for(int i = 0; i < n; i++)cin >> a[i];
    for(int i = 0; i < n; i++)cin >> b[i];
    ll res = a.back() == b.front();

    for(int i = 1; i < n - 1; i++) {
        pair<int,int> as, bs;
        if(a[i - 1] != a[i])as = {a[i], a[i] + 1};
        else as = {1, a[i] + 1};
        if(b[i + 1] != b[i])bs = {b[i], b[i] + 1};
        else bs = {1, b[i] + 1};
        res *= max(0, min(as.second, bs.second) - max(as.first, bs.first));
        res %= mod;
    }
    cout << res << endl;

}