#include <iostream>
#include <vector>
#include <map>
#include <cstdio>
#include <cstring>
#include <math.h>
#include <bitset>
#include <time.h>
#include <set>
#include <algorithm>
#define MODD 1000000007
#define ll long long
using namespace std;

int T[111111];
int A[111111];

int main() {
    int N;
    cin>>N;
    T[0]=0;
    for(int i=1;i<=N;i++) {
        cin>>T[i];
    }
    
    for(int i=1;i<=N;i++)
        cin>>A[i];
    A[N+1]=0;
    
    ll ans = 1;
    for(int i=1;i<=N;i++) {
        if (T[i]>T[i-1] || A[i]>A[i+1]) {
            int x = (T[i]>T[i-1] ? T[i] : A[i]);
            
            if (x > T[i] || x > A[i])
                ans = 0;
            
        } else {
            ans = (ans * min(A[i], T[i]))%MODD;
        }
    }
    
    cout << ans << endl;
    
}