#include <iostream>

#define mod 1000000007
using namespace std;
long long a[100010],t[100010],l[100010],r[100010],ans = 1;
int main(){
	int i,n;
	cin >> n;
	for(i=0;i<n;i++){
		cin >> t[i];
	}
	for(i=0;i<n;i++){
		cin >> a[i];
	}
	l[0] = t[0]; r[0] = t[0];
	for(i=1;i<n;i++){
		if(t[i]!=t[i-1]){
			l[i] =  t[i]; r[i] =  t[i];
		}else{
			l[i] = 1; r[i] = t[i];
		}
	}
	l[n-1] = max(l[n-1],a[n-1]); r[n-1] = min(r[n-1],a[n-1]);
	for(i=n-2;i>=0;i--){
		if(a[i]!=a[i+1]){
			l[i] = max(l[i],a[i]); r[i] = min(r[i],a[i]);
		}else{
			l[i] = max(1LL,l[i]); r[i] =  min(r[i],a[i]);
		}
	}
	
	for(i=0;i<n;i++){
		ans = (ans*max(0LL,r[i]-l[i]+1))%mod;
	}
	cout << ans << endl;
}
