template <unsigned int Modulus>
class IntMod {
	typedef unsigned long long ULL;
private:
	unsigned int value_m;

	void Copy(const IntMod& other) { value_m = other.value_m; }
	bool Modulus_is_valid() { return Modulus != 0; }
public:
	IntMod() { value_m = 0; }
	IntMod(unsigned int value) { value_m = value % Modulus; }
	IntMod(const IntMod& other) { Copy(other); }
	IntMod& operator=(const IntMod& other) { Copy(other); return *this; }
	IntMod& operator==(const IntMod& right) const { return value_m == right.value_m; }
	IntMod& operator!=(const IntMod& right) const { return value_m != right.value_m; }
	IntMod& operator++() {
		++value_m; 
		value_m %= Modulus;
		return *this;
	}
	IntMod& operator--() {
		if (value_m == 0) { value_m = Modulus - 1; }
		else { --value_m; }
		return *this;
	}
	IntMod operator-() const {
		if (value_m == 0) return IntMod(0);
		return IntMod(Modulus - value_m);
	}
	IntMod& operator+=(const IntMod& right) {
		ULL sum = (ULL)value_m + right.value_m;
		if (sum >= (ULL)Modulus) { value_m = (unsigned int)(sum - (ULL)Modulus); }
		else { value_m = (unsigned int)sum; }
		return *this;
	}
	IntMod& operator-=(const IntMod& right) {
		(*this) += (-right);
		return *this;
	}
	IntMod operator+(const IntMod& right) {
		IntMod ret(*this);
		ret += right;
		return ret;
	}
	IntMod operator-(const IntMod& right) {
		IntMod ret(*this);
		ret -= right;
		return ret;
	}
	IntMod& operator*=(const IntMod& right) {
		ULL tmp = (ULL)value_m * right.value_m;
		value_m = tmp % (ULL)Modulus;
		return *this;
	}	
	IntMod& operator/=(const IntMod& right) {
		(*this) *= (right.Inverse());
		return *this;
	}
	IntMod operator*(const IntMod& right) {
		IntMod ret(*this);
		ret *= right;
		return ret;
	}
	IntMod operator/(const IntMod& right) {
		IntMod ret(*this);
		ret /= right;
		return ret;
	}
	/* 素数判定は自分でしろ */
	IntMod Inverse() const {
		return (*this).Pow(Modulus - 2);
	}
	IntMod Pow(unsigned int exp) const {
		unsigned int arr[32];		// 32じゃなくてもいい
		ULL sum = 1;
		arr[0] = value_m;
		for (int i = 1; i < 32; ++i) {
			arr[i] = ((ULL)arr[i - 1] * arr[i - 1]) % (ULL)Modulus;
		}
		for (int i = 0; i < 32; ++i) {
			if (exp & (0x1 << i)) {
				sum *= arr[i]; sum %= (ULL)Modulus;
			}
		}
		return (unsigned int)sum;
	}
	unsigned int Get_value() {
		return value_m;
	}
};

typedef IntMod<1000000007> MInt;

#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>
#include <functional>
#include <cmath>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define REP(i,a,n) for(int i = a; i < n; ++i)
#define EPS 0.0001
#define INF 0x3FFFFFFF
typedef long long LL;
typedef unsigned long long ULL;

int N;
int T[100001];
int A[100001];
bool DET[100001];
int LIM[100001];
bool err;

int main() {
	cin >> N;
	REP(i, 0, N) {
		cin >> T[i];
	}
	REP(i, 0, N) {
		cin >> A[i];
	}
	int cur = 0;
	REP(i, 0, N) {
		if (cur != T[i]) {
			cur = T[i];
			DET[i] = true;
			LIM[i] = T[i];
		} else {
			LIM[i] = T[i];
		}
	}
	cur = 0;
	for (int i = N - 1; i >= 0; --i) {
		if (cur != A[i]) {
			if (A[i] > LIM[i]) {
				err = true;
			} else if (DET[i] == true && A[i] != LIM[i]) {
				err = true;
			}
			cur = A[i];
			DET[i] = true;
			LIM[i] = A[i];
		} else {
			if (DET[i] == true && A[i] < LIM[i]) {
				err = true;
			}
			LIM[i] = min(A[i], LIM[i]);
		}
	}
	MInt product = 1;
	REP(i, 0, N) {
		product *= (DET[i] ? 1 : LIM[i]);
	}
	cout << (err ? 0 : product).Get_value() << endl;
	return 0;
}

Measured memory_bytes: 1536