#include <bits/stdc++.h>

using namespace std;

int n;
int t[112123], a[112123];
bool ft[112123];
bool fa[112123];

int main()
{
	cin >> n;
	for (int i = 0; i < n; i++) cin >> t[i];
	for (int i = 0; i < n; i++) cin >> a[i];
	ft[0] = true;
	fa[n-1] = true;
	for (int i = 1; i < n; i++) ft[i] = ft[i] || t[i] != t[i-1];
	for (int i = n-2; i >= 0; i--) fa[i] = fa[i] || a[i] != a[i+1];
	long long ans = 1;
	for (int i = 0; i < n; i++)
	{
		if (ft[i] && fa[i] && t[i] != a[i])
		{
			cout << "0\n";
			return 0;
		}
		if (ft[i] && t[i] > a[i])
		{
			cout << "0\n";
			return 0;
		}
		if (fa[i] && a[i] > t[i])
		{
			cout << "0\n";
			return 0;
		}
		if (!ft[i] && !fa[i])
		{
			ans = ans * min(t[i], a[i]);
			ans %= 1000000000 + 7;
		}
	}
	cout << ans << '\n';

	return 0;
}

