#include <bits/stdc++.h>
using namespace std;
#define rep(i,a) for(int i=0;i<(int)a;i++)
#define zero(x) memset(x, 0, sizeof x)
typedef long long ll;
int const MOD = 1e9+7;
int const inf = 1<<29;

int main() {
 
  int N; cin >> N;
  int A[N], B[N];
  rep(i, N) cin >> A[i];
  rep(i, N) cin >> B[i];
 
  int L = -inf;
  int pat[N]; zero(pat);
  rep(i, N) if(L < A[i]) pat[i] = 1, L = A[i];
 
  int R = -inf;
  for(int i=N-1; i>=0; i--) {
    if(R < B[i]) {
      if(pat[i] && A[i] != B[i]) { cout << 0 << endl; exit(0); }
      pat[i] = 1, R = B[i];
    } else {
      if(pat[i] && A[i] > B[i]) { cout << 0 << endl; exit(0); }      
    }
  }
 
  ll ans = 1;
  rep(i, N) {
    if(!pat[i]) pat[i] = min(A[i], B[i]);
    (ans *= pat[i]) %= MOD;
  }
 
  cout << ans << endl;
  
  return 0;
}