#include <cstdio>
#include <algorithm>

using namespace std;

const int MOD = 1000000007;

int main() {
    int n;

    int hw[100000];
    int he[100000];

    scanf("%d",&n);

    long long sum=1;

    for(int i=0;i<n;i++) {
        scanf("%d",&hw[i]);
    }

    for(int i=0;i<n;i++) {
        scanf("%d",&he[i]);
    }

    for(int i=0;i<n;i++) {
        if((i==0||hw[i]!=hw[i-1])&&(i==n-1||he[i]!=he[i+1])&&(hw[i]!=he[i])) sum*=0;
        if((i==0||hw[i]!=hw[i-1])&&(hw[i]>he[i])) sum*=0;
        if((i==n-1||he[i]!=he[i+1])&&(he[i]>hw[i])) sum*=0;
        if(i==0||hw[i]!=hw[i-1]) sum*=1;
        else if(i==n-1||he[i]!=he[i+1]) sum*=1;
        else sum*=min(hw[i],he[i]);
        sum%=MOD;
    }

    printf("%lld\n",sum);

    return 0;
}