/*#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
int st, ed;
int n, m, Num[401000], cnt, Up[401000], chk[401000], par[401000], Deg[401000];
vector<int>E[401000];
vector<int>G[401000];
vector<int>P[401000];
int C;
void DFS(int a){
    Num[a] = ++cnt;
    int i, r = Num[a], x;
    for(i=0;i<E[a].size();i++){
        x = E[a][i];
        if(Num[x]){
            r = min(r, Num[x]);
            continue;
        }
        par[x] = a;
        DFS(x);
        if(Up[x] >= Num[a]){
            chk[x] = 1;
        }
        r = min(r, Up[x]);
    }
    Up[a] = r;
}
void MakeBCC(int a, int ck){
    if(ck){
        G[ck].push_back(a);
        P[a].push_back(ck);
    }
    int i, x;
    for(i=0;i<E[a].size();i++){
        x = E[a][i];
        if(par[x]!=a)continue;
        if(chk[x]){
            C++;
            G[C].push_back(a);
            P[a].push_back(C);
            MakeBCC(x,C);
        }
        else{
            MakeBCC(x,ck);
        }
    }
}
bool vv[401000];
vector<int>F[401000];
void Do2(int be, int ed){
    int i;
}
void Do(int nd, int g){
    int i,x,ee,j,nxt=-1;
    for(i=0;i<G[g].size();i++)vv[G[g][i]]=true;
    if(g == P[ed][0]){
        ee = ed;
    }
    else{
        for(i=0;i<G[g].size();i++){
            x = G[g][i];
            if(x!=ed && P[ed].size() == 2){
                ee = x;
                nxt = P[ed][0];
                if(nxt==g)nxt=P[ed][1];
            }
        }
    }
    for(i=0;i<G[g].size();i++){
        x = G[g][i];
        for(j=0;j<E[x].size();j++){
            int y = E[x][j];
            if(!vv[y])continue;
            F[x].push_back(y);
        }
    }
    Do2(nd,ee);
    for(i=0;i<G[g].size();i++){
        F[G[g][i]].clear();
        vv[G[g][i]]=false;
    }
    if(nxt!=-1){
        Do(ee,nxt);
    }
}
int main(){
    int i, a, b, j, TC;
    scanf("%d",&TC);
    while(TC--){
        scanf("%d%d%d%d",&n,&m,&st,&ed);
        for(i=0;i<m;i++){
            scanf("%d%d",&a,&b);
            E[a].push_back(b);
            E[b].push_back(a);
        }
        cnt = 0;
        DFS(1);
        C = 1;
        MakeBCC(1,0);
        for(i=1;i<=n;i++){
            if(P[i].size()>=3){
                printf("No\n");
                return 0;
            }
        }
        if(P[st].size()>=2 || P[ed].size()>=2){
            printf("No\n");
            return 0;
        }
        for(i=1;i<=C;i++){
            for(j=0;j<G[i].size();j++){
                a = G[i][j];
                if(P[a].size() == 2) Deg[i]++;
            }
        }
        if(Deg[P[st][0]]>1 || Deg[P[ed][0]]>1){
            printf("No\n");
            return 0;
        }
        printf("Yes\n");
        Do(st,P[st][0]);
        for(i=1;i<=n;i++){
            E[i].clear();
            P[i].clear();
            chk[i]=0;
            par[i]=0;
            Num[i]=0;
        }
        for(i=1;i<=C;i++){
            G[i].clear();
            Deg[i] = 0;
        }
    }
}*/
#include<stdio.h>
#include<algorithm>
using namespace std;
long long Mod = 1000000007, Res=1;
int A[101000], B[101000], n, H[101000];
int main(){
    int i;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%d",&A[i]);
        if(i==1 || A[i]!=A[i-1])H[i]=A[i];
    }
    for(i=1;i<=n;i++){
        scanf("%d",&B[i]);
    }
    for(i=n;i>=1;i--){
        if(i==n||B[i]!=B[i+1]){
            if(H[i]!=B[i]&&H[i]){
                printf("0\n");
                return 0;
            }
            H[i]=B[i];
        }
    }
    int t = 0;
    for(i=1;i<=n;i++){
        t = max(t,H[i]);
        if(t!=A[i]){
            printf("0\n");
            return 0;
        }
    }
    t=0;
    for(i=n;i>=1;i--){
        t=max(t,H[i]);
        if(t!=B[i]){
            printf("0\n");
            return 0;
        }
    }
    for(i=1;i<=n;i++){
        if(i==1||i==n||A[i-1]!=A[i]||B[i+1]!=B[i])continue;
        Res=Res*min(A[i],B[i])%Mod;
    }
    printf("%lld\n",Res);
}

Measured memory_bytes: 1280