#include "bits/stdc++.h"
#include <boost/range/irange.hpp>
using namespace std;
using boost::irange;

// 定義
typedef long long         ll;
typedef pair<ll, ll>      P;
#define ALL(x)            (x).begin(),(x).end()
#define REP(i, n)         for(ll i = 0 ; i < (ll)n ; ++i)
#define REPN(i, m, n)     for(ll i = m ; i < (ll)n ; ++i)
#define REVREP(i, n)      for(ll i = n - 1; i >= 0 ; i--)
#define VL                vector<ll>
#define VLL               vector<vector<ll>>
#define VLLL              vector<vector<vector<ll>>>
#define INF               2e9
#define INF_LL            1LL<<60
#define MOD               1000000007

ll Ceil(ll val, ll div)   { return (val + div - 1) / div; }
ll CeilN(ll val, ll div)  { return Ceil(val, div) * div; }
ll FloorN(ll x, ll n)     { return (x - x % n); }
bool IsOdd(ll x)          { return ((x & 1) == 1); }
bool IsEven(ll x)         { return ((x & 1) == 0); }
template<class T> bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

// MODのべき乗
ll powMod(ll n, ll p)
{
	ll res = 1;
	while (p) {
		if (p & 1) (res *= n) %= MOD;
		(n *= n) %= MOD;
		p >>= 1;
	}
	return res;
}

// MODの2のべき乗
ll powMod2(ll p)
{
	return powMod(2, p);
}

// MODの逆元
ll invMod(ll n)
{
	return powMod(n, MOD - 2);
}

/* コンビネーション */
ll CombinationMod(ll n, ll r)
{
	if (n <= 0) {
		return 1;
	}

	ll sum = 1;
	ll maxR = min(n - r, r);
	for (ll j = 0; j < maxR; j++) {
		sum *= (n - j);
		sum %= MOD;
		sum *= invMod(maxR - j);
		sum %= MOD;
	}

	return sum;
}

// テーブルを作る前処理
const int MAX = 2 * 1000 * 1000;
long long fac[MAX], finv[MAX], inv[MAX];
void CombInit()
{
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (int i = 2; i < MAX; i++){
		fac[i] = fac[i - 1] * i % MOD;
		inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
		finv[i] = finv[i - 1] * inv[i] % MOD;
	}
}

// 二項係数計算(nCk)
long long CombExec(int n, int k)
{
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

const int mod = 1000000007;
class mint {
public:
	long long x;
	mint(long long x = 0) : x((x % mod + mod) % mod) {}
	mint operator-() const {
		return mint(-x);
	}
	mint& operator+=(const mint& a) {
		if ((x += a.x) >= mod) x -= mod;
		return *this;
	}
	mint& operator-=(const mint& a) {
		if ((x += mod - a.x) >= mod) x -= mod;
		return *this;
	}
	mint& operator*=(const  mint& a) {
		(x *= a.x) %= mod;
		return *this;
	}
	mint operator+(const mint& a) const {
		mint res(*this);
		return res += a;
	}
	mint operator-(const mint& a) const {
		mint res(*this);
		return res -= a;
	}
	mint operator*(const mint& a) const {
		mint res(*this);
		return res *= a;
	}
	mint pow(ll t) const {
		if (!t) return 1;
		mint a = pow(t >> 1);
		a *= a;
		if (t & 1) a *= *this;
		return a;
	}
	mint inv() const {
		return pow(mod - 2);
	}
	mint& operator/=(const mint& a) {
		return (*this) *= a.inv();
	}
	mint operator/(const mint& a) const {
		mint res(*this);
		return res /= a;
	}
};

void Solve()
{
	ll N;
	cin >> N;
	VL A(N), T(N);
	REP(i, N) cin >> T[i];
	REP(i, N) cin >> A[i];

	ll ans = 0;
	VL ht(N, -1);
	ht[0] = T[0];
	REP(i, N - 1) {
		if (T[i] != T[i + 1]) {
			ht[i + 1] = T[i + 1];
		}
	}

	VL ha(N, -1);
	ha[N - 1] = A[N - 1];
	for (int i = N - 1; i >= 1; i--) {
		if (A[i] != A[i - 1]) {
			ha[i - 1] = A[i - 1];
		}
	}

	VL high(N, -1);
	REP(i, N) {
		if (ht[i] == -1 && ha[i] == -1) continue;
		if (ht[i] != -1 && ha[i] != -1) {
			if (ht[i] != ha[i]) {
				goto EXIT;
			}
		} 
		high[i] = max(ht[i], ha[i]);
		if (A[i] < high[i]) goto EXIT;
		if (T[i] < high[i]) goto EXIT;
	}

	ans = 1;
	REP(i, N) {
		if (high[i] == -1) {
			ans *= min(T[i], A[i]);
			ans %= MOD;
		}
	}

EXIT:
	cout << ans << endl;
}

// メイン
int main()
{
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(15);
	Solve();
	return 0;
}


Measured memory_bytes: 4224