#include <iostream>
using namespace std;
int main(){
  int N;
  cin >> N;
  int A[N], T[N];
  for(int i = 0; i < N; ++i){
    cin >> T[i];
  }
  for(int i = 0; i < N; ++i){
    cin >> A[i];
  }
  if(N == 1){
    if(A[0] == T[0]) cout << 1 << endl;
    else cout << 0 << endl;
    return 0;
  }
  int H[N][2];
  for(int i = 0; i < N; ++i){
    H[i][1] = min(T[i],A[i]);
    H[i][0] = 1;
  }
  H[0][0] = T[0];
  H[N-1][0] = A[N-1];
  long long int ans = 1LL, mod = 1000000007LL;
  for(int i = 1; i < N-1; ++i){
    bool f = false;
    if(T[i-1] != T[i]){
      H[i][0] = T[i];
      f = true;
    }
    if(A[i] != A[i+1]){
      H[i][0] = A[i];
    }
  }
  
  for(int i = 0; i < N; ++i){
    ans *= max(0LL,(long long int)(H[i][1]-H[i][0]+1));
    ans %= mod;
  }
  
  cout << ans << endl;
  return 0;
}
