#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;
typedef long long ll;
int n;
ll T[100001],A[100001];

ll solve(){
  ll mxT=-1,idxT=0;
  for(int i=0;i<n;i++) if(mxT<T[i])mxT=T[i],idxT=i;
  ll mxA=-1,idxA=0;
  for(int i=n-1;i>=0;i--) if(mxA<A[i])mxA=A[i],idxA=i;
  if(mxA!=mxT)return 0;
  ll ans=1;
  for(int i=1;i<n;i++){
    if(T[i]!=T[i-1]&&T[i]>A[i])return 0;
    if(A[i]!=A[i+1]&&A[i]>T[i])return 0;
    if(T[i]!=T[i-1])continue;
    if(A[i]!=A[i+1])continue;
    ans=ans*min(T[i],A[i])%mod;
  } 
    
  return ans;
}

int main(){
  cin>>n;
  for(int i=0;i<n;i++) cin>>T[i];
  for(int j=0;j<n;j++) cin>>A[j];
 
  cout<<solve()<<endl;

  return 0;
}
