#include<iostream>
#include<iomanip>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<vector>
#include<array>
#include<string>
#include<stack>
#include<queue>
#include<algorithm>
#include<cassert>
#include<functional>
#include<random>
#include<complex>
#include<bitset>
#include<chrono>
//#include<boost/multiprecision/cpp_int.hpp>
#define int int64_t
#define uint uint64_t
#define REP(i, a, b) for (int64_t i = (int64_t)(a); i < (int64_t)(b); i++)
#define rep(i, a) REP(i, 0, a)
#define EACH(i, a) for (auto i: a)
#define ITR(x, a) for (auto x = a.begin(); x != a.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define HAS(a, x) (a.find(x) != a.end())
#define Min(x) *min_element(ALL(x))
#define Max(x) *max_element(ALL(x))
#define Unique(L) (L.erase(unique(ALL(L)), L.end()))
#define veccat(v1, v2) std::copy((v2).begin(),(v2).end(),std::back_inserter(v1))
#define intmax (std::numeric_limits<int64_t>::max() / 4)
using namespace std;
//typedef boost::multiprecision::cpp_int bigint;
const double EPS = 1e-9;
const double PI = acos(-1.0);

class modint {
	//MODが素数であることを前提として実装してあるが、その判定はしていない。
	//あまりが出るような除算をしてはいけない。
private:
	static const int MOD = 1000000007;
public:
	modint() {
		//assert(is_prime(MOD));
		this->number = 0;
	}
	modint(const int src) {
		//assert(is_prime(MOD));
		this->number = opposit(src);
	}
	modint(const modint &src) {
		this->number = src.number;
	}

	modint& operator += (const modint& obj) {
		this->number = san2(this->number + obj.number);
		return *this;
	}
	modint& operator -= (const modint& obj) {
		this->number = san2(this->number - obj.number + MOD);
		return *this;
	}
	modint& operator *= (const modint& obj) {
		this->number = (this->number * obj.number) % MOD;
		return *this;
	}
	modint& operator /= (const modint& obj) {
		this->number = (this->number * inverse(obj.number)) % MOD;
		return *this;
	}
	modint& operator += (const int n) {
		this->number = san2(this->number + opposit(n));
		return *this;
	}
	modint& operator -= (const int n) {
		this->number = san2(this->number - opposit(n) + MOD);
		return *this;
	}
	modint& operator *= (const int n) {
		this->number = (this->number * opposit(n)) % MOD;
		return *this;
	}
	modint& operator /= (const int n) {
		this->number = (this->number * inverse(n)) % MOD;
		return *this;
	}

	modint operator + (const modint obj) { modint re(*this); return re += obj; }
	modint operator - (const modint obj) { modint re(*this); return re -= obj; }
	modint operator * (const modint obj) { modint re(*this); return re *= obj; }
	modint operator / (const modint obj) { modint re(*this); return re /= obj; }
	modint operator + (const int n) { modint re(*this); return re += n; }
	modint operator - (const int n) { modint re(*this); return re -= n; }
	modint operator * (const int n) { modint re(*this); return re *= n; }
	modint operator / (const int n) { modint re(*this); return re /= n; }

	modint operator = (const int n) {
		this->number = opposit(n);
		return *this;
	}
	int get() {
		return number;
	}

private:
	int number;

	int opposit(int n) {
		if (n < 0)n = MOD - ((-n) % MOD);
		return n % MOD;
	}
	int inverse(int n) {
		n = opposit(n);
		int result = 1;
		for (int i = MOD - 2; i; i /= 2) {
			if (i % 2)result = (result * n) % MOD;
			n = (n * n) % MOD;
		}
		return result;
	}
	inline int san2(const int n) {
		return MOD <= n ? n - MOD : n;
	}
	bool is_prime(int n) {
		if (n <= 1)return false;
		if (n == 2)return true;
		if (n % 2 == 0) return false;
		const int upperbound = int(sqrt(n));
		for (int i = 3; i <= upperbound; i += 2) {
			if (n % i == 0) return false;
		}
		return true;
	}
};

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int N;
	cin >> N;
	vector<int>T(N), A(N);
	rep(i, N)cin >> T[i];
	rep(i, N)cin >> A[i];

	//高橋君の記録から導かれる、山iの高さの範囲:=[T_lb[i],T_ub[i]]
	vector<int>T_lb(N), T_ub(N), A_lb(N), A_ub(N);

	T_lb[0] = T[0];
	T_ub[0] = T[0];
	REP(i, 1, N) {
		if (T[i] == T[i - 1]) {
			T_lb[i] = 1;
			T_ub[i] = T[i];
		}
		else {
			T_lb[i] = T[i];
			T_ub[i] = T[i];
		}
	}

	A_lb[N - 1] = A[N - 1];
	A_ub[N - 1] = A[N - 1];
	for (int i = N - 2; 0 <= i; i--) {
		if (A[i] == A[i + 1]) {
			A_lb[i] = 1;
			A_ub[i] = A[i];
		}
		else {
			A_lb[i] = A[i];
			A_ub[i] = A[i];
		}
	}

	modint ans = 1;
	rep(i, N) {
		const int lb = max(T_lb[i], A_lb[i]);
		const int ub = min(T_ub[i], A_ub[i]);
		if (ub < lb) {
			cout << 0 << endl;
			return 0;
		}
		ans *= (ub - lb + 1);
	}

	cout << ans.get() << endl;

	return 0;
}

Measured memory_bytes: 4992