#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;

int N;
int T[110000]={},Tmax=0;
int A[110000]={},Amax=0;
long long int P[110000]={}; // 高さ、確定か

int main()
{
	
	cin >> N;
	
	int ansF = 0;
	int flag=0;
	for(int i=0; i<N; i++) {
		cin >> T[i];
		P[i] = ((flag != T[i])? 1: T[i]);
		flag = T[i];
	}
	for(int i=0; i<N; i++) {
		cin >> A[i];
	}
	
	flag = 0;
	for(int i=N-1; i>=0; i--) {
		if(flag != A[i] && P[i] == 1 && A[i] != T[i]) {
			ansF = 1;
		}
		if(flag != A[i] && A[i] > T[i]) {
			ansF = 2;
		}
		
		P[i] = ((flag != A[i])? 1: min(P[i], (long long int)A[i]));
		flag = A[i];
	}
	
	if(ansF >= 1){
		cout << 0 << endl;
		return 0;
	}
	
	long long int waru = pow(10,9) + 7;
	long long int ans = 1;
	for(int i=0; i<N; i++) {
		ans *= P[i];
		ans %= waru;
	}
	cout << ans << endl;
	
	return 0;
}

