#include <bits/stdc++.h>
using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int,int,int> tpl;

#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)

const double EPS = 1e-9;
const double PI  = acos(-1.0);
const int INT_INF = 2147483647;
const long long LL_INF = 1LL<<60;
const long long MOD = 1000000007;

#define CLR(a) memset((a), 0 ,sizeof(a))

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

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

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

bool check(pair<mint,mint> T, pair<mint,mint> A){
    if(T.first.x == 1 && A.first.x == 1){
        if(T.second.x == A.second.x) return true;
        else return false;
    }
    else if(T.first.x == 1){
        if(T.second.x <= A.second.x) return true;
        else return false;
    }
    else if(A.first.x == 1){
        if(A.second.x <= T.second.x) return true;
        else return false;
    }
    else return true;
}

int main(void){
    ll N; cin >> N;
    vector<ll> A(N), T(N); REP(i,N) cin >> A[i]; REP(i,N) cin >> T[i];

    vector<pair<mint,mint>> AA(N), TT(N);
    AA[0].first = 1; AA[0].second = A[0];
    REP(i,N-1){
        if(A[i] < A[i+1]){
            AA[i+1].first = 1;
            AA[i+1].second = A[i+1];
        }
        else{
            AA[i+1].first = A[i+1];
            AA[i+1].second = A[i+1];
        }
    }

    TT[N-1].first = 1; TT[N-1].second = T[N-1];
    RREP(i,N-1){
        if(T[i+1] < T[i]){
            TT[i].first = 1;
            TT[i].second = T[i];
        }
        else{
            TT[i].first = T[i+1];
            TT[i].second = T[i];
        }
    }

    mint ans(1);
    REP(i,N){
        if(check(AA[i], TT[i])){
            ans *= min(AA[i].first.x, TT[i].first.x);
        }
        else{
            cout << 0 << endl;
            return 0;
        }
    }

    cout << ans.x << endl;
    return 0;
}

Measured memory_bytes: 4992