#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <string>
#include <set>

using namespace std;


int main(){
    long long int n, m = 1000000007;
    cin >> n;
    vector <long long int> t(n), a(n);
    for (int i = 0; i < n; ++i){
        cin >> t[i];
    }
    long long int ans = 1;
    for (int i = 0; i < n; ++i){
        cin >> a[i];
    }
    if (t[n-1] != a[0]){
        cout << "0";
        return 0;
    }
    for (int i = 1; i < n-1; ++i){
        if (t[i]<=t[i-1] && a[i]<= a[i+1]){
            ans = (ans*min(a[i],t[i]))%m;
        }
        else if ((t[i]>t[i-1] && a[i]<t[i]) || (a[i]>a[i+1] && a[i]>t[i])){
            cout << "0";
            return 0;
        }
    }
    cout << ans;
   return 0;
}