#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define N ((ll)101*1000)
#define INF ((ll)2e9)
#define MOD ((ll)1e9+7)

ll n,a[N],b[N],ans=1;

int main()
{
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n;
	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=0;i<n;i++)cin>>b[i];
	for(int i=n-1;i>=0;i--)
	{
		if(i==n-1 || b[i+1]!=b[i])
		{
			if(i==0 || a[i]!=a[i-1])
			{
				if(b[i]!=a[i])return cout<<0,0;
			}
			else
			{
				if(b[i]>a[i])return cout<<0,0;
			}
		}
		else
		{
			if(i==0 || a[i]!=a[i-1])
			{
				if(b[i]<a[i])return cout<<0,0;
			}
			else
			{
				ans*=min(a[i],b[i]);ans%=MOD;
			}
		}
	}
	cout<<ans;
	return 0;
}