#define LOCAL
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define rrep(i, n) for(int i=(n-1); i>=0; --i)
#define rep2(i, s, n) for(int i=s; i<(n); ++i)
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
static const int INTINF = (INT_MAX >> 1); // 10^9 + 10^7
static const ll LLINF = (LLONG_MAX >> 1);
static const int MAX = 1e5+1;
static const ll MOD = 1e9+7;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
string to_string(string s) { return '"' + s + '"'; }
string to_string(bool b) { return b ? "true" : "false"; }
template <size_t N> string to_string(bitset<N> bs) {
  string res;
  for (size_t i = 0; i < N; ++i) res += '0' + bs[i];
  return res;
}
string to_string(vector<bool> v) {
  string res = "{";
  for (bool e : v) res += to_string(e) + ", ";
  return res += "}";
}
template <class T, class U> string to_string(pair<T, U> p);
template <class C> string to_string(C c) {
  string res = "{";
  for (auto e : c) res += to_string(e) + ", ";
  return res += "}";
}
template <class T, class U> string to_string(pair<T, U> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
void debug() { cerr << '\n'; }
template <class Head, class... Tail> void debug(Head head, Tail... tail) {
  cerr << '\t' << to_string(head), debug(tail...);
}
#ifdef LOCAL
#define DEBUG(...) cerr << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__)
#else
#define DEBUG(...)
#endif

int tdp[MAX], adp[MAX], dp[MAX];


// modint: mod 計算を int を扱うように扱える構造体
template<int MOD> struct Fp {
    long long val;
    constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
        if (val < 0) v += MOD;
    }
    constexpr int getmod() { return MOD; }
    constexpr Fp operator - () const noexcept {
        return val ? MOD - val : 0;
    }
    constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
    constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
    constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
    constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
    constexpr Fp& operator += (const Fp& r) noexcept {
        val += r.val;
        if (val >= MOD) val -= MOD;
        return *this;
    }
    constexpr Fp& operator -= (const Fp& r) noexcept {
        val -= r.val;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr Fp& operator *= (const Fp& r) noexcept {
        val = val * r.val % MOD;
        return *this;
    }
    constexpr Fp& operator /= (const Fp& r) noexcept {
        long long a = r.val, b = MOD, u = 1, v = 0;
        while (b) {
            long long t = a / b;
            a -= t * b; swap(a, b);
            u -= t * v; swap(u, v);
        }
        val = val * u % MOD;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr bool operator == (const Fp& r) const noexcept {
        return this->val == r.val;
    }
    constexpr bool operator != (const Fp& r) const noexcept {
        return this->val != r.val;
    }
    friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
        return os << x.val;
    }
    friend constexpr istream& operator >> (istream &is, Fp<MOD>& x) noexcept {
        return is >> x.val;
    }
    friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
        if (n == 0) return 1;
        auto t = modpow(a, n / 2);
        t = t * t;
        if (n & 1) t = t * a;
        return t;
    }
};


using mint = Fp<MOD>;

int main(int argc, const char * argv[]) {
  std::cout << std::fixed << std::setprecision(15);

  int N; cin >> N;
  vector<int> T(N), A(N);
  rep(i, N) cin >> T[i];
  rep(i, N) cin >> A[i];
  memset(tdp, 0, sizeof(tdp));
  memset(adp, 0, sizeof(adp));
  fill(dp, dp+MAX, INTINF);

  int ts = 0, as = 0;
  rep(i, N){
    if(T[i] != ts){
      ts = T[i];
      tdp[i] = ts;
    }
    else{
      tdp[i] = -ts;
    }
  }

  rrep(i, N){
    if(A[i] != as){
      as = A[i];
      adp[i] = as;
    }
    else{
      adp[i] = -as;
    }
  }

  bool ok = true;
  rep(i, N){
    if(tdp[i] >= 0 && adp[i] >= 0 && tdp[i] == adp[i]){
      dp[i] = 0;
    }
    else{
      int t = -tdp[i];
      int a = -adp[i];
      if(tdp[i] < 0 && adp[i] < 0){
        dp[i] = min(t, a);
      }
      else if(tdp[i] < 0){
        if(adp[i] <= t){
          dp[i] = 0;
        }
        else{
          ok = false;
        }
      }
      else if(adp[i] < 0){
        if(tdp[i] <= a){
          dp[i] = 0;
        }
        else{
          ok = false;
        }
      }
      else{
        ok = false;
      }
    }
  }
  // cout << ok << endl;
  rep(i, N){
    // DEBUG(dp[i]);
  }

  mint ans = 1;
  if(!ok){
    cout << 0 << endl;
    return 0;
  }
  else{
    rep(i, N){
      if(!dp[i]) continue;
      ans *= dp[i];
    }
  }
  cout << ans << endl;
}


Measured memory_bytes: 2176