#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
using namespace std;


//負数の合同も考慮に入れるmod関数
inline long long imod(long long a, long long b)
{
	return (a >= 0) ? (a % b) : (a % b + b);
}

//mintが引数の繰り返し2乗法
template <long long MOD> class mint;
template <long long MOD> mint<MOD> m_pow(mint<MOD> x, long long n);

//modの整数クラス
template <long long MOD = 1000000007>
class mint
{
public:
	mint operator+(const mint &other)const
	{
		return mint(imod(a + other.a, MOD));
	}
	mint operator-(const mint &other)const
	{
		return mint(imod(a - other.a, MOD));
	}
	mint operator*(const mint &other)const
	{
		return mint(imod(a * other.a, MOD));
	}
	mint operator+=(const mint &other)
	{
		a = imod(a + other.a, MOD);
		return mint(a);
	}
	mint operator-=(const mint &other)
	{
		a = imod(a - other.a, MOD);
		return mint(a);
	}
	mint operator*=(const mint &other)
	{
		a = imod(a * other.a, MOD);
		return mint(a);
	}
	mint operator+()const
	{
		return *this;
	}
	mint operator-()const
	{
		return mint(-a);
	}
	mint& operator++()
	{
		*this += 1;
		return *this;
	}
	mint operator++(int)
	{
		auto tmp = *this;
		*this += 1;
		return tmp;
	}
	mint& operator--()
	{
		*this -= 1;
		return *this;
	}
	mint operator--(int)
	{
		auto tmp = *this;
		*this -= 1;
		return tmp;
	}
	mint operator~()const
	{
		return pow(a, e_phi - 1);
	}
	mint& operator=(const mint &other)
	{
		a = other.a;
		return *this;
	}
	operator long long()const
	{
		return a;
	}
	explicit operator int()const
	{
		return (int)a;
	}
	mint(long long a_) :a(imod(a_, MOD))
	{
		//無効な値
		static_assert(MOD >= 2, "MOD cannot be below 2.");

		if (e_phi > 0)return;
		//オイラー関数Φ(mod)を計算する
		e_phi = MOD;
		long long m_ = MOD;
		for (int i = 2; i * i <= m_; ++i)
		{
			if (m_ % i == 0)
			{
				e_phi = e_phi / i * (i - 1);
				for (; m_ % i == 0; m_ /= i);
			}
		}
		if (m_ != 1)e_phi = e_phi / m_ * (m_ - 1);
	}
	mint() :a(0) {}
private:
	static long long e_phi;
	long long a;
};

//mintの累乗
//x^nを計算します
template<long long MOD>mint<MOD> pow(mint<MOD> x, long long n)
{
	mint<MOD> res = 1;
	while (n > 0)
	{
		if (n & 1)res *= x;
		x *= x;
		n >>= 1;
	}
	return res;
}


//mintの階乗計算
//O(x)程度の時間がかかります
//非推奨、同時間でfact_setが計算可能
template<long long MOD>mint<MOD> fact(mint<MOD> x)
{
	mint<MOD> res(1);
	for (long long i = 1; i <= (long long)x; ++i)
	{
		res *= i;
	}
	return res;
}

//mintの階乗計算のリスト
//setの末尾に0～xまでの階乗数の列挙をします
//O(x)程度の時間がかかります
template<long long MOD>void fact_set(std::vector<mint<MOD>> &set, mint<MOD> x = mint<MOD>(-1))
{
	mint<MOD> res(1);
	set.push_back(1);
	for (long long i = 1; i <= (long long)x; ++i)
	{
		res *= i;
		set.push_back(res);
	}
}

template<long long MOD>long long mint<MOD>::e_phi = -1;

//mint型のstreamへの入出力対応
template<long long MOD> std::ostream& operator<<(std::ostream& os, mint<MOD> i)
{
	os << (long long)i;
	return os;
}

template<long long MOD> std::istream& operator >> (std::istream& is, mint<MOD>& i)
{
	long long tmp;
	is >> tmp;
	i = tmp;
	return is;
}


mint<> ans(1);

int n;
mint<> t[123456];
mint<> a[123456];

mint<> tr[123456][2];
mint<> ar[123456][2];

mint<> res[123456][2];

mint<> total[123456];

int main(void)
{
	cin >> n;
	for (int i = 0; i < n; ++i)cin >> t[i];
	for (int i = 0; i < n; ++i)cin >> a[i];
	for (int i = 0; i < n; ++i)
	{
		if (i == 0)
		{
			tr[i][0] = tr[i][1] = t[i];
		}
		else if (t[i - 1] < t[i])
		{
			tr[i][0] = tr[i][1] = t[i];
		}
		else if(t[i - 1] == t[i])
		{
			tr[i][0] = 1;
			tr[i][1] = t[i];
		}
		else
		{
			return 0 * printf("0\n");
		}
	}
	for (int i = n - 1; i >= 0; --i)
	{
		if (i == n - 1)
		{
			ar[i][0] = ar[i][1] = a[i];
		}
		else if (a[i + 1] < a[i])
		{
			ar[i][0] = ar[i][1] = a[i];
		}
		else if (a[i + 1] == a[i])
		{
			ar[i][0] = 1;
			ar[i][1] = a[i];
		}
		else
		{
			return 0 * printf("0\n");
		}
	}

	for (int i = 0; i < n; ++i)
	{
		res[i][0] = max(tr[i][0], ar[i][0]);
		res[i][1] = min(tr[i][1], ar[i][1]);
		total[i] = res[i][1] - res[i][0] + mint<>(1);
		if(res[i][1] < res[i][0])return 0 * printf("0\n");
		ans *= total[i];
	}
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 8960