Problem:
Score : 400 points

Problem Statement

Mountaineers Mr. Takahashi and Mr. Aoki recently trekked across a certain famous mountain range.
The mountain range consists of N mountains, extending from west to east in a straight line as Mt. 1, Mt. 2, ..., Mt. N.
Mr. Takahashi traversed the range from the west and Mr. Aoki from the east.

The height of Mt. i is h_i, but they have forgotten the value of each h_i.
Instead, for each i (1 ≤ i ≤ N), they recorded the maximum height of the mountains climbed up to the time they reached the peak of Mt. i (including Mt. i).
Mr. Takahashi's record is T_i and Mr. Aoki's record is A_i.

We know that the height of each mountain h_i is a positive integer.
Compute the number of the possible sequences of the mountains' heights, modulo 10^9 + 7.

Note that the records may be incorrect and thus there may be no possible sequence of the mountains' heights.
In such a case, output 0.

Constraints

1 ≤ N ≤ 10^5

1 ≤ T_i ≤ 10^9

1 ≤ A_i ≤ 10^9

T_i ≤ T_{i+1} (1 ≤ i ≤ N - 1)

A_i ≥ A_{i+1} (1 ≤ i ≤ N - 1)

Input

The input is given from Standard Input in the following format:

N
T_1 T_2 ... T_N
A_1 A_2 ... A_N

Output

Print the number of possible sequences of the mountains' heights, modulo 10^9 + 7.

Sample Input 1

5
1 3 3 3 3
3 3 2 2 2

Sample Output 1

4

The possible sequences of the mountains' heights are:

1, 3, 2, 2, 2

1, 3, 2, 1, 2

1, 3, 1, 2, 2

1, 3, 1, 1, 2

for a total of four sequences.

Sample Input 2

5
1 1 1 2 2
3 2 1 1 1

Sample Output 2

0

The records are contradictory, since Mr. Takahashi recorded 2 as the highest peak after climbing all the mountains but Mr. Aoki recorded 3.

Sample Input 3

10
1 3776 3776 8848 8848 8848 8848 8848 8848 8848
8848 8848 8848 8848 8848 8848 8848 8848 3776 5

Sample Output 3

884111967

Don't forget to compute the number modulo 10^9 + 7.

Sample Input 4

1
17
17

Sample Output 4

1

Some mountain ranges consist of only one mountain.

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#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;

 	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]);
			}
		}
		//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;
}