#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
ll mo=1e9+7;
ll sum=1;

void mod(ll x){
    sum=sum*x;
    sum=sum%mo;
}

int main(){
    int n; cin >> n;
    ll a[n],b[n];
    ll am=0,bm=0,ap,bp;
    for(int i=0;i<n;i++){
        cin >> a[i];
        if(a[i]>am){am=a[i];ap=i;}
    }
    for(int i=0;i<n;i++){cin >> b[i];if(b[i]>bm)bm=b[i];}
    for(int i=n-1;i>=0;i--){if (b[i]==bm){bp=i;break;}}

    if (am!=bm){sum=0;}
    else if(ap>bp){sum=0;}
    else{
        for(int i=ap+1;i<bp;i++){mod(am);}
        for(int i=ap;i>0;i--){
            if(a[i]==a[i-1]){mod(a[i]);}
        }
        for(int i=bp+1;i<n-1;i++){
            if(b[i]==b[i+1]){mod(b[i]);}
        }
    }
    cout <<sum <<endl;
return 0;
}
