#include <iostream>
#include <vector>
#include <string.h>
#include <stack>
#include <queue>
#include <algorithm>
#include <climits>
#include <cmath>
#include <map>
#include <set>
#include <assert.h>
#include <sstream>
#define REP(i,n) for(ll i=0;i<(n);i++)
#define MOD 1000000007
#define int long long
#ifdef int
const long long INF = LLONG_MAX / 10;
#else
const int INF = 1010101010;
#endif
using namespace std;
typedef long long ll;
typedef vector<vector<ll>> mat;
typedef pair<int, int> P;
//typedef pair<double, double> P;
template <class T> inline void smin(T &a, T const &b) { a = min(a, b); }
template <class T> inline void smax(T &a, T const &b) { a = max(a, b); }

#ifdef LOCAL
#define dump(...)                                         \
    do {                                                  \
        std::ostringstream os;                            \
        os << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; \
        print_to(os, ", ", "\n", __VA_ARGS__);            \
        std::cerr << os.str();                            \
    } while (0)
#define dump_(a)                                          \
    do {                                                  \
        std::ostringstream os;                            \
        os << __LINE__ << ":\t" << #a << " = [";          \
        print_to_(os, ", ", "]\n", all(a));               \
        std::cerr << os.str();                            \
    } while (0)
#else
#define dump(...)
#define dump_(...)
#endif

template <typename T>
void print_to(std::ostream &os, const char *, const char *tail, const T &fst) {
    os << fst << tail;
}
template <typename Fst, typename... Rst>
void print_to(std::ostream &os, const char *del, const char *tail, const Fst &fst, const Rst &... rst) {
    os << fst << del;
    print_to(os, del, tail, rst...);
}
template <typename Iter>
void print_to_(std::ostream &os, const char *del, const char *tail, Iter bgn, Iter end) {
    for (Iter it = bgn; it != end;) {
        os << *it;
        if (++it != end) {
            os << del;
        } else {
            os << tail;
        }
    }
}
template <typename Fst, typename... Rst>
void println(const Fst &fst, const Rst &... rst) {
    print_to(std::cout, "\n", "\n", fst, rst...);
}
template <typename Fst, typename... Rst>
void print(const Fst &fst, const Rst &... rst) {
    print_to(std::cout, " ", "\n", fst, rst...);
}
template <typename Iter>
void println_(Iter bgn, Iter end) {
    print_to_(std::cout, "\n", "\n", bgn, end);
}
template <typename Iter>
void print_(Iter bgn, Iter end) {
    print_to_(std::cout, " ", "\n", bgn, end);
}

// const int MOD = 1000000007;
namespace trush {
    int _ = (std::cout.precision(10), std::cout.setf(std::ios::fixed), std::cin.tie(0),
             std::ios::sync_with_stdio(0), 0);
}

int N;
int T[101010], A[101010];
int X[101010];  //確定した値
int ma[101010]; //確定していないところの最大値

signed main()
{
    cin >> N;
    REP(i,N) cin >> T[i];
    REP(i,N) cin >> A[i];
    
    if (T[N-1] != A[0]) {
        cout << 0 << endl;
        return 0;
    }

    REP(i,101010) ma[i] = INF;

    X[0] = T[0];
    for (int i=1; i<N; i++) {
        if (T[i-1] < T[i]) X[i] = T[i];
        else ma[i] = T[i];
    }

    if (X[N-1] != 0 && X[N-1] != A[N-1]) {
        cout << 0 << endl;
        return 0;
    } else {
        X[N-1] = A[N-1];
    }
    for (int i=N-2; i>=0; i--) {
        if (A[i] > A[i+1]) {
            if (X[i] != 0 && X[i] != A[i]) {
                cout << 0 << endl;
                return 0;
            } else {
                if (ma[i] != INF && ma[i] < A[i]) {
                    cout << 0 << endl;
                    return 0;
                }
                X[i] = A[i];
            }
        } else {
            ma[i] = min(ma[i], A[i]);
        }
    }

    /*
    REP(i,N) {
        cout << X[i] << " ";
    }
    cout << endl;
    */

    int ans = 1;
    REP(i,N) {
        //cout << ma[i] << " ";
        if (X[i] == 0) {
            ans *= ma[i];
            ans %= MOD;
        }
    }
    //cout << endl;
    cout << ans << endl;
}


Measured memory_bytes: 3456