    #include<stdio.h>
    int MIN(int a,int b){return a<b?a:b;}
    int main(){
      int n,i,a[100010],b[100010],f[100010]={};
      long long s=1,M=(1e9)+7;
      scanf("%d",&n);
      for(i=0;i<n;i++)scanf("%d",&a[i]);
      for(i=0;i<n;i++)scanf("%d",&b[i]);
      for(i=0;i<n;i++){
        if(i==0||a[i]-a[i-1]){
          f[i]=1;
          if(a[i]>b[i])s=0;
        }
        if(i==n-1||b[i]-b[i+1]){
          f[i]=1;
          if(b[i]>a[i])s=0;
        }    
      }
      if(a[n-1]-b[0])s=0;
      for(i=0;i<n;i++){
        if(f[i])continue;
        s=(s*MIN(a[i],b[i]))%M;
      }
      printf("%lld\n",s);
      return 0;
    }