#include <vector>
#include <stack>
#include <stdio.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <typeinfo>
#include <queue>

using namespace std;

typedef unsigned long long ll;




void ichi(){
	string s;
	cin >> s;
	int tmp_c = 0;
	int tmp_f = 0;
	for(int i=0;i<s.size();i++){
		if(s[i] == 'C'){
			tmp_c = 1;
		}
		else if(s[i] == 'F'){
			if(tmp_c == 1){
				cout << "Yes" << endl;
				tmp_f = 1;
				break;
			}
		}
	}
	if(tmp_f == 0){
		cout << "No" << endl;
	}

}

void ni(){
	int k,t;
	cin >> k >> t;
	//int num[t];
	std::vector<int> num;
	int tmp;
	for(int i=0;i<t;i++){
		cin >> tmp;
		num.push_back(tmp);
	}
	sort(num.begin(),num.end(),greater<int>());
	/*
	int place = 0;
	for(int i = 0;i<k;i++){
		if(num[place] > 0){
			num[place] = num[place] - 1;
		}
	}
	*/
	int maxnum = num[0];
	int diff = 1;
	for(int i=1;i<t;i++){
		diff = diff + num[i];
	}
	int result = max(0,maxnum - diff);
	cout << result << endl;

	/*
	for(int i=0;i<t;i++){
		cout << num[i] << " ";
	}
	cout << endl;
	*/

}

void san(){
	int n;
	std::vector<ll> t;
	std::vector<ll> a;
	ll tmp;
	ll result = 1ll;
	ll waru = 1000000000ll + 7ll;
	cin >> n;
	for(int i=0;i<n;i++){
		cin >> tmp;
		t.push_back(tmp);
	}
	for(int i=0;i<n;i++){
		cin >> tmp;
		a.push_back(tmp);
	}

	//result = 3776ll*8848ll*8848ll*8848ll;
	ll tmp_t = 0ll;
	ll tmp_a = 0ll;
	std::vector<ll> t_val(n);
	std::vector<ll> t_val_a(n);
	bool miss_flg = false;
	//t_val[0] = t[0];
	//cout << t_val[0] << " " << t_val[1] << " " << t_val.size() << endl;
	//std::vector<unsigned long long int> v(n);

 	for(int i=0;i<n;i++){
		if(tmp_t != t[i]){
			t_val[i] = t[i];
			tmp_t = t[i];
			//break;
		}
	}
 	for(int i=n-1;i>=0;i--){
		if(tmp_a != a[i]){
			t_val_a[i] = a[i];
			tmp_a = a[i];
			//t_val[i] = t[i];
			//break;
		}
	}

 	for(int i=0;i<n;i++){
		if(t_val[i] != 0){
			if(a[i] < t_val[i]){
				miss_flg = true;
				break;
			}
			/*
			if(t_val_a[i] !=0){
				if(t_val_a[i]!=t_val[i]){
					miss_flg = true;
					break;
				}
			}
			*/
		}
		if(t_val_a[i] != 0){
			if(t[i] < t_val_a[i]){
				miss_flg = true;
				break;
			}
		}
	}

	if(miss_flg){
		cout << 0 << endl;
	}
	else{
		for(int i=0;i<n;i++){
			if(t_val[i] ==0 && t_val_a[i]==0){
				result = (result * min(t[i],a[i])) % waru;
			}
		}
		cout << result << endl;
		//cout << result%waru << endl;
	}


	//cout << ll(a[0])%waru << endl;
	//cout << result%waru << endl;


}

void san2(){
	int n;
	std::vector<int> t;
	std::vector<int> a;
	int tmp;
	ll result = 1ll;
	ll waru = 1000000000ll + 7ll;
	cin >> n;
	for(int i=0;i<n;i++){
		cin >> tmp;
		t.push_back(tmp);
	}
	for(int i=0;i<n;i++){
		cin >> tmp;
		a.push_back(tmp);
	}

	//result = 3776ll*8848ll*8848ll*8848ll;
	int tmp_t = 0;
	int tmp_a = 0;
	std::vector<int> t_val(n);
	std::vector<int> t_val_a(n);
	bool miss_flg = false;
	//t_val[0] = t[0];
	//cout << t_val[0] << " " << t_val[1] << " " << t_val.size() << endl;

 	for(int i=0;i<n;i++){
		if(tmp_t != t[i]){
			t_val[i] = t[i];
			tmp_t = t[i];
			//break;
		}
		if(tmp_a != a[n-1-i]){
			t_val_a[n-1-i] = a[n-1-i];
			tmp_a = a[n-1-i];
		}
		if(t_val[i] != 0){
			if(a[i] < t_val[i]){
				miss_flg = true;
				break;
			}
		}
		if(t_val_a[i] !=0){
			if(t[i] < t_val_a[i]){
				miss_flg = true;
				break;
			}
		}

	}

	if(miss_flg){
		cout << 0 << endl;
	}
	else{
		for(int i=0;i<n;i++){
			if(t_val[i] ==0 && t_val_a[i]==0){
				result = result * ll(min(t[i],a[i]));
			}
		}
		//cout << result << endl;
		//cout << result%waru << endl;
	}


	//cout << ll(a[0])%waru << endl;
	//cout << result%waru << endl;


}



int main(){
	//ichi();
	//ni();
	//san2();
	san();
	return 0;
}

Measured memory_bytes: 3572