#include <iostream>
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int main(void){
    // Your code here!
    int n;
    cin>>n;
    vector<int> s(n),t(n),u(n);
    for(int i=0;i<n;i++){
        cin>>s[i];
    }
    for(int i=0;i<n;i++){
        cin>>t[i];
    }
    int b=0;
    for(int i=0;i<n;i++){
        if(s[i]>b){
            if(t[i]<s[i]){cout<<0<<endl;return 0;}
            else{b=s[i];u[i]=1;}
        }
        else u[i]=b;
    }
    b=0;
    for(int i=n-1;i>=0;i--){
        if(b<t[i]){
            if(s[i]<t[i]){cout<<0<<endl;return 0;}
            else{b=t[i];u[i]=1;}
        }
        else u[i]=min(u[i],b);
    }
    ll ans=1;
    ll mod=1000000007;
    for(int i=0;i<n;i++){ans=ans*u[i]%mod;}
    cout<<ans<<endl;
}
