#include <iostream>
#include <array>
#include <algorithm>
#include <vector>
#include <bitset>
#include <set>
#include <unordered_set>
#include <cmath>
#include <complex>
#include <deque>
#include <iterator>
#include <numeric>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <limits>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;

using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i=k; i<(ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(),v.end()
template < class T > inline bool chmax(T& a, T b) {if (a < b) { a=b; return true; } return false; }
template < class T > inline bool chmin(T& a, T b) {if (a > b) { a=b; return true; } return false; }
#define DEBUG_VLL(vec) REP(sz, vec.size()) std::cerr<<vec[sz]<<(sz==vec.size()-1?'\n':' ');

constexpr long long MOD = 1000000007;
constexpr long long HIGHINF = (long long)1e18;
constexpr int INF = (int)1e9;

class Mint {
    long long x;

public:
    constexpr Mint(long long x=0) : x((x%MOD+MOD)%MOD) {}
    constexpr Mint operator-() const {
        return Mint(-x);
    }
    constexpr Mint& operator+=(const Mint& a) {
        if ((x += a.x) >= MOD) x -= MOD;
        return *this;
    }
    constexpr Mint& operator-=(const Mint& a) {
        if ((x += MOD-a.x) >= MOD) x -= MOD;
        return *this;
    }
    constexpr Mint& operator*=(const Mint& a) {
        (x *= a.x) %= MOD;
        return *this;
    }
    constexpr Mint& operator+(const Mint& a) const {
        Mint res(*this);
        return res += a;
    }
    constexpr Mint& operator-(const Mint& a) const {
        Mint res(*this);
        return res -= a;
    }
    constexpr Mint& operator*(const Mint& a) const {
        Mint res(*this);
        return res *= a;
    }
    constexpr Mint pow(long long t) const {
        if (!t) return 1;
        Mint a = pow(t >> 1);
        a *= a;
        if (t & 1) a *= *this;
        return a;
    }
    constexpr Mint inv() const {
        return pow(MOD - 2);
    }
    constexpr Mint& operator/=(const Mint& a) {
        return (*this) *= a.inv();
    }
    constexpr Mint operator/(const Mint& a) const {
        Mint res(*this);
        return res /= a;
    }

    friend ostream& operator<<(ostream& os, const Mint& m) {
        os << m.x;
        return os;
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n; cin >> n;
    vll t(n), a(n);
    for (ll i = 0; i < n; i++) cin >> t[i];
    for (ll i = 0; i < n; i++) cin >> a[i];
    Mint ans(1);
    for (ll i = 1; i < n - 1; i++) {
        if (t[i] > t[i - 1]) {
            if (t[i] > a[i]) {
                cout << 0 << '\n';
                return 0;
            } else if (a[i + 1] < a[i] && a[i] != t[i]) {
                cout << 0 << '\n';
                return 0;
            }
        } else if (a[i] > a[i + 1]) {
            if (t[i] < a[i]) {
                cout << 0 << '\n';
                return 0;
            } else if (t[i - 1] < t[i] && a[i] != t[i]) {
                cout << 0 << '\n';
                return 0;
            }
        } else {
            ans *= Mint(min(a[i], t[i]));
        }
    }

    if (t.back() != a[0]) {
        cout << 0 << '\n';
        return 0;
    }
    cout << ans << '\n';
    return 0;
}


Measured memory_bytes: 1792