#include <bits/stdc++.h>
using namespace std;

#define int long long
const double PI = 3.14159265358979323846;
typedef vector<int> vint;
typedef pair<int, int> pint;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

int N;
int T[110000],A[110000];
bool sw[110000];
int mod=1e9+7;

signed main() {
    cin>>N;
    for(int i=0;i<N;i++)cin>>T[i];
    for(int i=0;i<N;i++)cin>>A[i];
    int ans=1;
    sw[0]=true;
    for(int i=1;i<N;i++){
        if(T[i]>T[i-1]){
            if(T[i]>A[i])ans=0;
            sw[i]=true;
        }
    }
    for(int i=N-2;i>=0;i--){
        if(A[i]>A[i+1]){
            if(A[i]>T[i])ans=0;
        }else if(!sw[i])(ans*=min(T[i],A[i]))%=mod;
    }
    if(T[N-1]!=A[0])ans=0;
    cout<<ans<<endl;
    return 0;
}
