#include <iomanip>
#include <utility>
#include <algorithm>
#include <type_traits>
#include <set>
#include <string>
#include <iostream>
#include <fstream>
#include <cassert>
#include <memory>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <queue>
#include <vector>
#include <array>
#include <chrono>
#include <random>
#include <forward_list>
#include <iomanip>
#include <functional>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <sstream>
#include <numeric>

using namespace std;
#define MFOR(i,end) for(size_t i = 0, end_i = (end); i < end_i; ++ i)

//?????°???????????????????????\??????mod??¢??°
inline constexpr 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> constexpr mint<MOD> m_pow(mint<MOD> x, long long n);

//mod?????´??°?????????
template <long long MOD = 1000000007>
class mint
{
public:
	constexpr mint<MOD> operator+(const mint<MOD> &other)const
	{
		return mint<MOD>(imod(a + other.a, MOD));
	}
	constexpr mint<MOD> operator-(const mint<MOD> &other)const
	{
		return mint<MOD>(imod(a - other.a, MOD));
	}
	constexpr mint<MOD> operator*(const mint<MOD> &other)const
	{
		return mint<MOD>(imod(a * other.a, MOD));
	}
	constexpr mint<MOD>& operator+=(const mint<MOD> &other)
	{
		a = imod(a + other.a, MOD);
		return *this;
	}
	constexpr mint<MOD>& operator-=(const mint<MOD> &other)
	{
		a = imod(a - other.a, MOD);
		return *this;
	}
	constexpr mint<MOD>& operator*=(const mint<MOD> &other)
	{
		a = imod(a * other.a, MOD);
		return *this;
	}
	constexpr mint<MOD> operator+()const
	{
		return *this;
	}
	constexpr mint<MOD> operator-()const
	{
		return mint<MOD>(-a);
	}
	constexpr mint<MOD>& operator++()
	{
		return *this += 1;
	}
	constexpr mint<MOD> operator++(int)
	{
		auto tmp = *this;
		*this += 1;
		return tmp;
	}
	constexpr mint<MOD>& operator--()
	{
		return *this -= 1;
	}
	constexpr mint<MOD> operator--(int)
	{
		auto tmp = *this;
		*this -= 1;
		return tmp;
	}
	constexpr mint<MOD> operator~()const
	{
		return pow(a, e_phi - 1);
	}
	constexpr mint<MOD>& operator=(const mint<MOD> &other)
	{
		a = other.a;
		return *this;
	}
	constexpr operator long long()const
	{
		return a;
	}
	constexpr explicit operator int()const
	{
		return (int)a;
	}
	static constexpr long long getmod()
	{
		return MOD;
	}
	constexpr mint(long long a_) :a(imod(a_, MOD))
	{
		//????????????
	}
	constexpr mint() :a(0) {}

	static_assert(MOD >= 2, "MOD cannot be below 2.");
private:
	static constexpr long long get_e_phi() {
		long long temp = MOD;
		long long m_ = MOD;
		for (int i = 2; i * i <= m_; ++i)
		{
			if (m_ % i == 0)
			{
				temp = temp / i * (i - 1);
				for (; m_ % i == 0; m_ /= i);
			}
		}
		if (m_ != 1)temp = temp / m_ * (m_ - 1);
		return temp;
	}
	static constexpr long long e_phi = get_e_phi();
	long long a;
};

//mint????´????
//x^n????¨?????????????
template<long long MOD>constexpr mint<MOD> m_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>constexpr 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>constexpr 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);
	}
}

//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;
}

typedef mint<1000000000+7> Int;
constexpr Int operator""_mi(unsigned long long x)noexcept {
	return Int(x);
}

int N;
Int T[100000];
Int A[100000];
int main()
{
	std::cin.sync_with_stdio(false);
	std::cout.sync_with_stdio(false);
	std::cin >> N;
	MFOR(i, N) {
		std::cin >> T[i];
	}
	MFOR(i, N) {
		std::cin >> A[i];
	}

	//両端での矛盾検査
	if ((T[0] <= A[0]) && (A[N - 1] <= T[N - 1])) {
		//OK
	}
	else {
		//矛盾！！
		std::cout << 0 << std::endl;
		return 0;
	}

	Int pattern = 1;
	for (size_t i = 1; i < N-1; ++i)
	{
		if (T[i - 1] < T[i]) {
			//M[i] == T[i]以外あり得ない
			if (T[i] == A[i] || (T[i] <= A[i + 1])) {
				//OK 1パターン
			}
			else {
				//矛盾！！
				std::cout << 0 << std::endl;
				return 0;
			}
		}
		else
		{
			if (A[i + 1] < A[i]) {
				//M[i] == A[i]以外あり得ない
				if (/*T[i] == A[i] || */(A[i] <= T[i - 1])) {
					//OK 1パターン
				}
				else {
					//矛盾！！
					std::cout << 0 << std::endl;
					return 0;
				}
			}
			else {
				pattern *= std::min(T[i], A[i]);
			}
		}
	}

	std::cout << pattern << std::endl;
}

Measured memory_bytes: 1792