CodeEff/ECCO huggingface dataset:
# Original solution
n = int(eval(input()))
T = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
p = 10 ** 9 + 7

B = [10**9] * n

if max(T) != max(A):
    print((0))
    exit()

h = 0

for i, t in enumerate(T):
    if h < t:
        B[i] = 1
        h = t
    else:
        B[i] = h

h = 0

for i, a in zip(list(range(n-1, -1, -1)), A[::-1]):
    if h < a:
        B[i] = 1
        h = a
    elif B[i] == 1 and T[i] > A[i]:
        print((0))
        exit(0)
    elif B[i] > h:
        B[i] = h

ans = 1

for b in B:
    ans *= b

print((ans%p))

# Optimized solution
n = int(eval(input()))
T = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
p = 10 ** 9 + 7

B = [10**9] * n

if max(T) != max(A):
    print((0))
    exit()

h = 0

for i, t in enumerate(T):
    if h < t:
        B[i] = 1
        h = t
    else:
        B[i] = h

h = 0

for i, a in zip(list(range(n-1, -1, -1)), A[::-1]):
    if h < a:
        B[i] = 1
        h = a
    elif B[i] == 1 and T[i] > A[i]:
        print((0))
        exit(0)
    elif B[i] > h:
        B[i] = h

ans = 1

for b in B:
    ans *= b
    ans %= p

print(ans)
# Original solution
from itertools import groupby
from functools import reduce

MOD = 1000000007

def max_t():
    i, t_max = 0, 0
    for j, v in enumerate(t):
        if v < t_max:
            print((0))
            quit(0)
        if v > t_max:
            t_max = v
            i = j
    return i, t_max


def validate_a(i, t_max):
    for v in a[:i + 1]:
        if v != t_max:
            print((0))
            quit(0)
    for v in a[i + 1:]:
        if v > t_max:
            print((0))
            quit(0)


if __name__ == '__main__':
    n = int(eval(input()))
    t = [int(s) for s in input().split()]
    a = [int(s) for s in input().split()]
    i, t_max = max_t()
    validate_a(i, t_max)

    highests = [min(x, y) for x, y in zip(a, t)]
    print((reduce(lambda x, y: x * y % MOD, (v ** ((length - 1) if v < t_max else max(length - 2, 0)) for v, length in ((v, len(list(l))) for v, l in groupby(highests))))))


# Optimized solution
from itertools import groupby
from functools import reduce

MOD = 1000000007

def mod_pow(x, k):
    ret = 1
    while k > 0:
        if k & 1:
            ret *= x
            ret %= MOD
        k >>= 1
        x *= x
        x %= MOD
    return ret


def max_t():
    i, t_max = 0, 0
    for j, v in enumerate(t):
        if v < t_max:
            print((0))
            quit(0)
        if v > t_max:
            t_max = v
            i = j
    return i, t_max


def validate_a(i, t_max):
    for v in a[:i + 1]:
        if v != t_max:
            print((0))
            quit(0)
    for v in a[i + 1:]:
        if v > t_max:
            print((0))
            quit(0)


if __name__ == '__main__':
    n = int(eval(input()))
    t = [int(s) for s in input().split()]
    a = [int(s) for s in input().split()]
    i, t_max = max_t()
    validate_a(i, t_max)

    highests = [min(x, y) for x, y in zip(a, t)]
    print((reduce(lambda x, y: x * y % MOD, (mod_pow(v, ((length - 1) if v < t_max else max(length - 2, 0))) for v, length in ((v, len(list(l))) for v, l in groupby(highests))))))
# Original solution
N = int(eval(input()))
T = list(map(int,input().split()))
A = list(map(int,input().split()))
A.append(1)
count = 0
Tmax = 1
Amax = 1
T2 = [0] * N
A2 = [0] * N
ans = 1

for i in range(N):
    if T[i] > Tmax and A[i] > A[i+1] and T[i] != A[i]:
        ans = 0
        break
    elif T[i] > Tmax and A[i] > A[i+1] and T[i] == A[i]:
        count = 1
        Tmax = T[i]
        Amax = A[i+1]
    elif T[i] > Tmax and A[i] == A[i+1] and T[i] > A[i]:
        ans = 0
        break
    elif T[i] > Tmax and A[i] == A[i+1] and T[i] <= A[i]:
        count = 1
        Tmax = T[i]
    elif T[i] == Tmax and A[i] > A[i+1] and A[i] > T[i]:
        ans = 0
        break
    elif T[i] == Tmax and A[i] > A[i+1] and A[i] <= T[i]:
        count = 1
        Amax = A[i+1]
    else:
        count = min(A[i],T[i])
    ans *= count
    ans = ans % (10**9+7)
print(ans)

# Optimized solution
N = int(eval(input()))
T = list(map(int,input().split()))
A = list(map(int,input().split()))
A.append(1)
count = 0
Tmax = 1
Amax = 1
T2 = [0] * N
A2 = [0] * N
ans = 1

for i in range(N):
    if T[i] > Tmax:
        Tmax = T[i]
        if A[i] > A[i+1]:
            Amax = A[i+1]
            if T[i] == A[i]:
                count = 1
            else:
                ans = 0
                break
        else:
            if T[i] > A[i]:
                ans = 0
                break
            else:
                count = 1
    else:
        if A[i] > A[i+1]:
            Amax = A[i+1]
            if A[i] > T[i]:
                ans = 0
                break
            else:
                count = 1
        else:
            count = min(A[i],T[i])
    ans *= count
    ans = ans % (10**9+7)
print(ans)
# Original solution

#import numpy as np
from functools import *
import sys
sys.setrecursionlimit(100000)
input = sys.stdin.readline




def acinput():
    return list(map(int, input().split(" ")))


def II():
    return int(eval(input()))



mod = 10**9+7


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact



def serch(x, count):
    #print("top", x, count)
            

    for d in directions:
        nx = d+x
        #print(nx)
        if np.all(0 <= nx) and np.all(nx < (H, W)):
            if field[nx[0]][nx[1]] == "E":
                count += 1 
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)  
                continue
            if field[nx[0]][nx[1]] == "#":
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)    
                 
    return count

N=II()

t=acinput()
a=acinput()

st = [[] for i in range(N)]
st[0]=[t[0],t[0]]
for i in range(1,N):
    if t[i-1]<t[i]:
        st[i]=[t[i],t[i]]
    else:
        st[i]=[1,st[i-1][1]]
        

sa = [[] for i in range(N)]
sa[N-1] = [a[N-1], a[N-1]]
for i in reversed(list(range(N-1))):
    if a[i] > a[i+1]:
        sa[i] = [a[i], a[i]]
    else:
        sa[i] = [1,sa[i+1][1]]

def diff(x):
    return x[1]-x[0]
res=1
for i in range(N):
    
    x=max(sa[i])-min(st[i])+1
    y=max(st[i])-min(sa[i])+1
    #print(x,y,np.diff(sa))
    if x<=0 or y<=0:
        res=0
        break
    res*= min(x,y,diff(sa[i])+1,diff(st[i])+1)%mod
    
print((res%mod))


# Optimized solution
import sys


input = sys.stdin.readline




def acinput():
    return list(map(int, input().split(" ")))


def II():
    return int(eval(input()))



mod = 10**9+7


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact



def serch(x, count):
    #print("top", x, count)
            

    for d in directions:
        nx = d+x
        #print(nx)
        if np.all(0 <= nx) and np.all(nx < (H, W)):
            if field[nx[0]][nx[1]] == "E":
                count += 1 
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)  
                continue
            if field[nx[0]][nx[1]] == "#":
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)    
                 
    return count

N=II()

t=acinput()
a=acinput()

st = [[] for i in range(N)]
st[0]=[t[0],t[0]]
for i in range(1,N):
    if t[i-1]<t[i]:
        st[i]=[t[i],t[i]]
    else:
        st[i]=[1,st[i-1][1]]
        

sa = [[] for i in range(N)]
sa[N-1] = [a[N-1], a[N-1]]
for i in reversed(list(range(N-1))):
    if a[i] > a[i+1]:
        sa[i] = [a[i], a[i]]
    else:
        sa[i] = [1,sa[i+1][1]]

def diff(x):
    return x[1]-x[0]
res=1
for i in range(N):
    
    x=max(sa[i])-min(st[i])+1
    y=max(st[i])-min(sa[i])+1
    #print(x,y,np.diff(sa))
    if x<=0 or y<=0:
        res=0
        break
    res=res* min(x,y,diff(sa[i])+1,diff(st[i])+1)
    res=res%mod
    
print((res%mod))

#print(st)
#print(sa)
# Original solution
def inpl(): return [int(i) for i in input().split()]
mod = 10**9+7
N = int(eval(input()))
T = inpl()
A = inpl()
summit =  T[-1]
S = [i == j == summit for i, j in zip(A, T)]
ans, pre = 1, 0
if A[0] != summit:
    ans = 0
if not any(S):
    ans = 0

k = sum([min(i, j) == summit for i, j in zip(A, T)]) 
for i in T:
    if i == summit:
        break
    if i == pre:
        ans = ans*pre %mod
    else:
        pre = i
pre = 0
for i in reversed(A):
    if i == summit:
        break
    if i == pre:
        ans = ans*pre %mod
    else:
        pre = i
print((ans*summit**max(k-2, 0)%mod))

# Optimized solution
import sys
mod = 10**9 + 7

def nai():
    print((0))
    sys.exit()

N = int(eval(input()))
T = list(map(int, input().split()))
A = list(map(int, input().split()))

M = max(A)
if M != max(T):
    nai()

iT = T.index(M)
iA = N - 1 - A[::-1].index(M)
if iA < iT:
    nai()
ans = pow(M, max(0, iA - iT - 1), mod)
pre = -1
for t in T[:iT]:
    if pre == t:
        ans = t*ans%mod
    pre = t
pre = -1
for a in A[::-1]:
    if a == M:
        break
    if pre == a:
        ans = a*ans%mod
    pre = a
print(ans)
# Original solution
N=int(eval(input()))
*T, = list(map(int,input().split()))
*A, = list(map(int,input().split()))

mod = 10**9+7
mT = [T[0]]*N
MT = [T[0]]*N
for i,t in enumerate(T[1:],1):
    if t>T[i-1]:
        mT[i] = t
        MT[i] = t
    else:
        mT[i] = 1
        MT[i] = t
mA = [A[-1]]*N
MA = [A[-1]]*N
for i,a in enumerate(A[-2::-1],2):
    if a>A[-i+1]:
        mA[-i] = a
        MA[-i] = a
    else:
        mA[-i] = 1
        MA[-i] = a
m = [max(a,t) for a,t in zip(mA,mT)]
M = [min(a,t) for a,t in zip(MA,MT)]
ans = 1
for a,b in zip(m,M):
    ans *= max(0,b-a+1)
    ans %= mod
print(ans)

# Optimized solution
N=int(eval(input()))
*T, = [0] + list(map(int,input().split())) + [0]
*A, = [0] + list(map(int,input().split())) + [0]
mod = 10**9+7
M = list(map(min,list(zip(A,T))))[1:-1]
m = [max(T[i] if T[i]>T[i-1] else 1, A[i] if A[i]>A[i+1] else 1) for i in range(1,N+1)]
ans = 1
for a,b in zip(m,M): ans = ans * max(0,b-a+1) % mod
print(ans)
# Original solution
def read():
	return [int(i) for i in input().split(" ")]
N = int(eval(input()))
T = read()
A = read()
t = [(i != j, i) for i, j in zip(T, [None] + T)]
a = [(i != j, i) for i, j in zip(A, (A + [None])[1:])]
P = 1
for p, q in zip(t, a):
	if(p[0]):
		if(q[0]):
			if(p[1] != q[1]):
				print((0))
				exit()
		elif(q[1] < p[1]):
			print((0))
			exit()
	elif(q[0]):
		if(p[1] < q[1]):
			print((0))
			exit()
	else:
		P = P * min(p[1], q[1]) % (10 ** 9 + 7)
print(P) 


# Optimized solution
def read():
	return [int(i) for i in input().split(" ")]
N = int(eval(input()))
T = read()
A = read()
T = [(i != j, i) for i, j in zip(T, [None] + T)]
A = [(i != j, i) for i, j in zip(A, (A + [None])[1:])]
P = 1
for p, q in zip(T, A):
	if(p[0]):
		if(q[0]):
			if(p[1] != q[1]):
				print((0))
				exit()
		else:
			if(q[1] < p[1]):
				print((0))
				exit()
	elif(q[0]):
		if(p[1] < q[1]):
			print((0))
			exit()
	else:
		P = P * min(p[1], q[1]) % (10 ** 9 + 7)
print(P)
# Original solution
mod = 10 ** 9 + 7

N = int(eval(input()))
T = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]

HT = [False] * N
for i in range(1, N):
    if T[i - 1] < T[i]:
        HT[i] = True
HT[0] = True

HA = [False] * N
for i in range(N - 1):
    if A[i] > A[i + 1]:
        HA[i] = True
HA[-1] = True

ans = 1
for i in range(N):
    if HT[i] and HA[i]:
        ans *= 1 if T[i] == A[i] else 0
    elif HT[i]:
        ans *= 1 if T[i] <= A[i] else 0
    elif HA[i]:
        ans *= 1 if T[i] >= A[i] else 0
    else:
        ans = (ans * min(T[i], A[i])) % mod
    if ans == 0:
        break
print(ans)


# Optimized solution
mod = 10 ** 9 + 7

N = int(eval(input()))
T = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]

HT = [False] * N
for i in range(1, N):
    if T[i - 1] < T[i]:
        HT[i] = True
HT[0] = True

HA = [False] * N
for i in range(N - 1):
    if A[i] > A[i + 1]:
        HA[i] = True
HA[-1] = True

ans = 1
for ht, ha, t, a in zip(HT, HA, T, A):
    if ht and ha:
        ans *= 1 if t == a else 0
    elif ht:
        ans *= 1 if t <= a else 0
    elif ha:
        ans *= 1 if t >= a else 0
    else:
        ans = (ans * min(t, a)) % mod
print(ans)
# Original solution
N = int(eval(input()))
T = [int(t) for t in input().split()]
A = [int(a) for a in input().split()]
mod = 7 + 10**9

TS, AS = [None for i in range(N)], [None for j in range(N)] #山の高さが一意に決まるならTrue
TS[0], AS[-1] = True, True
for i in range(1, N):
    if T[i] > T[i-1]: TS[i] = True
    else: TS[i] = False
    if A[N-1-i] > A[N-i]: AS[N-1-i] = True
    else: AS[N-1-i] = False

Ans = 1
for i in range(N): 
    if TS[i] and AS[i]:
        if T[i] != A[i]: 
            Ans = 0
            break
    elif TS[i]:
        if A[i] < T[i]:
            Ans = 0
            break
    elif AS[i]:
        if T[i] < A[i]:
            Ans = 0
            break
    else:
        Ans *= min(T[i], A[i])
        Ans %= mod

print(Ans)

# Optimized solution
import sys

def solve():
    input = sys.stdin.readline
    N = int(eval(input()))
    T = [int(t) for t in input().split()]
    A = [int(a) for a in input().split()]
    mod = 7 + 10 ** 9
    Ans = 1
    H = [-1] * N
    H[0] = T[0]
    for i in range(1, N):
        if T[i] > T[i-1]: H[i] = T[i]
    if H[N-1] > -1 and H[N-1] != A[N-1]: Ans = 0
    else: H[N-1] = A[N-1]
    for i in reversed(list(range(N-1))):
        if A[i] > A[i+1]:
            if H[i] > -1 and H[i] != A[i]: Ans = 0
            elif T[i] < A[i]: Ans = 0
            else: H[i] = A[i]
        else:
            if H[i] == -1: 
                Ans *= min(T[i], A[i])
                Ans %= mod
    print(Ans)
            
    return 0

if __name__ == "__main__":
    solve()
# Original solution
n=int(eval(input()))
a=[0]+list(map(int,input().split()))
b=list(map(int,input().split()))+[0]
c=[[0,0]for i in range(n)]
p=0
mod=10**9+7
for i in range(n,0,-1):
    if a[i-1]!=a[i]:
        c[i-1]=[1,a[i]]
    else:
        c[i-1]=[0,a[i]]
for i in range(n):
    if b[i]!=b[i+1]:
        if c[i][0]==1:
            if b[i]!=c[i][1]:
                print((0))
                break
        else:
            if b[i]<=c[i][1]:
                c[i]=[1,b[i]]
            else:
                print((0))
                break
    else:
        if c[i][0]!=1:
            c[i]=[0,min(b[i],c[i][1])]
        else:
            if c[i][1]>b[i]:
                print((0))
                break
else:
    d=1
    for i in range(n):
        if c[i][0]!=1:
            d*=c[i][1]%mod
    print((d%mod))

# Optimized solution
n=int(eval(input()))
a=[0]+list(map(int,input().split()))
b=list(map(int,input().split()))+[0]
c=[[0,0]for i in range(n)]
p=0
mod=10**9+7
d=1
for i in range(n,0,-1):
    if a[i-1]!=a[i]:
        c[i-1]=[1,a[i]]
    else:
        c[i-1]=[0,a[i]]
for i in range(n):
    if b[i]!=b[i+1]:
        if c[i][0]==1:
            if b[i]!=c[i][1]:
                print((0))
                break
        else:
            if b[i]<=c[i][1]:
                c[i]=[1,b[i]]
            else:
                print((0))
                break
    else:
        if c[i][0]!=1:
            c[i]=[0,min(b[i],c[i][1])]
        else:
            if c[i][1]>b[i]:
                print((0))
                break
    if c[i][0]!=1:
        d=d*c[i][1]%mod
else:
    print(d)
# Original solution
p = 10**9+7
N = int(eval(input()))
T = list(map(int,input().split()))
A = list(map(int,input().split()))
H = [0 for _ in range(N)]
cnt = 1
H[0] = T[0]
for i in range(1,N):
    if T[i]>T[i-1]:
        H[i] = T[i]
if H[-1]>0 and H[-1]!=A[-1]:
    cnt = 0
else:
    H[-1] = A[-1]
    for i in range(-2,-N-1,-1):
        if H[i]==0 and A[i]>A[i+1]:
            if A[i]<=T[i]:
                H[i] = A[i]
            else:
                cnt = 0
                break
        elif H[i]>0 and A[i]>A[i+1]:
            if H[i]==A[i]:continue
            else:
                cnt = 0
                break
    if cnt==1:
        for i in range(1,N-1):
            if H[i]==0:
                cnt = (cnt*min(T[i],A[i]))%p
print(cnt) 

# Optimized solution
p = 10**9+7
N = int(eval(input()))
T = list(map(int,input().split()))
A = list(map(int,input().split()))
H = [0 for _ in range(N)]
flag = 0
if T[N-1]!=A[0]:
    flag = 1
hmax = A[0]
if flag==0:
    indmaxT=0
    H[0] = T[0]
    for i in range(1,N):
        if T[i]>T[i-1]:
            H[i] = T[i]
            if H[i]==hmax:
                indmaxT = i
                break
    indmaxA = N-1
    H[N-1] = A[N-1]
    for i in range(N-2,-1,-1):
        if A[i]>A[i+1]:
            H[i]=A[i]
            if H[i]==hmax:
                indmaxA = i
                break
    if indmaxA<indmaxT:
        flag = 1
if flag==1:
    print((0))
else:
    cnt = 1
    cur = 0
    for i in range(1,indmaxT):
        if H[i]==0:
            cnt = (cnt*H[cur])%p
        elif H[i]>0:
            cur = i
    cur = N-1
    for i in range(N-2,indmaxA,-1):
        if H[i]==0:
            cnt = (cnt*H[cur])%p
        elif H[i]>0:
            cur = i
    for i in range(indmaxT+1,indmaxA):
        cnt = (cnt*hmax)%p
    print(cnt)
# Original solution
MOD = 1000000007

def union(x,y):
    if len(x)==1:
        if len(y)==1:
            return (1 if x[0]==y[0] else 0)
        else:
            return (1 if y[-1]>=x[0] else 0)
    if len(x)>1:
        if len(y)==1:
            return (1 if x[-1]>=y[0] else 0)
        else:
            return min(len(x),len(y))

n = int(eval(input()))
a = list([int(x) % MOD for x in input().split()])
b = list([int(x) % MOD for x in input().split()])
x = [[] for _ in range(n)]
y = [[] for _ in range(n)]
z = 1
x[0] += [a[0]]
y[n-1] += [b[n-1]]

for i in range(1,n):
    if a[i-1]<a[i]:
        x[i] += [a[i]]
    else:
        x[i] += list(range(1,a[i]+1))

for j in range(n-1, 0, -1):
    if b[j-1]>b[j]:
        y[j-1] += [b[j-1]]
    else:
        y[j-1] += list(range(1,b[j-1]+1))

for i in range(0,n):
    z *= union(x[i],y[i]) % MOD
    z = z % MOD

print(z)


# Optimized solution
import sys

MOD = 1000000007

def union(x,y):
    if len(x)==1:
        if len(y)==1:
            return (1 if x[0]==y[0] else 0)
        else:
            return (1 if y[-1]>=x[0] else 0)
    if len(x)>1:
        if len(y)==1:
            return (1 if x[-1]>=y[0] else 0)
        else:
            return min(len(x),len(y))

n = int(eval(input()))
a = list([int(x) % MOD for x in input().split()])
b = list([int(x) % MOD for x in input().split()])
x = [0 for _ in range(n)]
y = [0 for _ in range(n)]
x[0] = 1
y[-1] = 1
z = 1

for i in range(1,n):
    if a[i-1]<a[i]:
        x[i] = 1
    else:
        x[i] = 0

for j in range(n-1, 0, -1):
    if b[j-1]>b[j]:
        y[j-1] += 1
    else:
        y[j-1] += 0

for i in range(n):
    if x[i] == 1 and y[i] == 0:
        if a[i] > b[i]:
            print((0))
            sys.exit()
    elif x[i] == 0 and y[i] == 1:
        if a[i] < b[i]:
            print((0))
            sys.exit()
    elif x[i] == 1 and y[i] == 1:
        if a[i] != b[i]:
            print((0))
            sys.exit()
    else:
        z *= min(a[i],b[i])
        z = z % MOD

print(z)
# Original solution
import sys
n = int(eval(input()))
t = list(map(int, input().split()))
a = list(map(int, input().split()))

h = [min(t[i],a[i]) for i in range(n)]
fixed = [False] * n
fixed[0] = True; fixed[n-1] = True

if a[n-1] > t[n-1] or t[0] > a[0]:
    print((0))
    sys.exit(0)
# takahashi check
for i in range(1,n):
    if t[i] > t[i-1]: 
        fixed[i] = True
        if t[i] > a[i]:
            print((0))
            sys.exit(0)
# aoki check
for i in range(n-2, -1, -1):
    if a[i] > a[i+1]:
        fixed[i] = True
        if a[i] > t[i]:
            print((0))
            sys.exit(0)

# calc ans
ans = 1
for i in range(n):
    if not fixed[i]:
        ans = ans * h[i] % (10**9+7)
print(ans)

# Optimized solution
n = int(eval(input()))
t = list(map(int, input().split()))
a = list(map(int, input().split()))

mod = 10 ** 9 + 7

yama = [0] * n

# tは、t[i] == t[i + 1]だったら左の以外全部ありうる
yama[0] = t[0]
if t[0] > a[0]:
    print((0))
    exit()
for i in range(n - 1):
    if t[i] < t[i + 1]:
        # i + 1の高さ確定
        yama[i + 1] = t[i + 1]
        if t[i + 1] > a[i + 1]:
            print((0))
            exit()
for i in range(n - 1, 0, -1):
    if a[i - 1] > a[i]:
        yama[i - 1] = a[i - 1]
        if a[i - 1] > t[i - 1]:
            print((0))
            exit()
if a[n - 1] > t[n - 1]:
    print((0))
    exit()
yama[n - 1] = a[n - 1]

ans = 1
for i in range(1, n - 1):
    if yama[i] == 0:
        ans = (ans * min(a[i], t[i])) % mod
print(ans)
# Original solution
mod=10**9+7
n=int(eval(input()))
T=[int(i) for i in input().split()]
A=[int(i) for i in input().split()]
Tres=[0]*n
Ares=[0]*n
Tres[0]=[0,T[0]]
Ares[-1]=[0,A[-1]]
for i in range(1,n):
  if T[i]!=T[i-1]:
    Tres[i]=[0,T[i]]
  else:
    Tres[i]=[1,T[i]]
  if A[-i-1]!=A[-i]:
    Ares[-i-1]=[0,A[-i-1]]
  else:
    Ares[-i-1]=[1,A[-i-1]]
ans=1
for i in range(n):
  ta,tb=Tres[i]
  aa,ab=Ares[i]  
  if ta==1 and aa==1:
    ans=ans*min(tb,ab)
  elif ta==0 and aa==1 and tb>ab:
    ans=0
  elif ta==1 and aa==0 and tb<ab:
    ans=0
  elif ta==0 and aa==0 and tb!=ab:
    ans=0
print((ans%mod))


# Optimized solution
mod=10**9+7
n=int(eval(input()))
T=[int(i) for i in input().split()]
A=[int(i) for i in input().split()]
Tres=[0]*n
Ares=[0]*n
Tres[0]=[0,T[0]]
Ares[-1]=[0,A[-1]]
for i in range(1,n):
  if T[i]>T[i-1]:
    Tres[i]=[0,T[i]]
  else:
    Tres[i]=[1,T[i]]
  if A[-i-1]>A[-i]:
    Ares[-i-1]=[0,A[-i-1]]
  else:
    Ares[-i-1]=[1,A[-i-1]]
ans=1
for i in range(n):
  ta,tb=Tres[i]
  aa,ab=Ares[i]  
  if ta==1 and aa==1:
    ans=ans*min(tb,ab)%mod
  elif ta==0 and aa==1 and tb>ab:
    ans=0
  elif ta==1 and aa==0 and tb<ab:
    ans=0
  elif ta==0 and aa==0 and tb!=ab:
    ans=0
print((ans%mod))
# Original solution
from collections import defaultdict, deque
import sys
N, M = list(map(int, input().split()))
edges = [list(range(N)) for i in range(N)]
for i in range(N):
    edges[i].remove(i)
for _ in range(M):
    a, b = list(map(int, input().split()))
    edges[a-1].remove(b-1)
    edges[b-1].remove(a-1)
size = defaultdict(lambda: [0, 0])
color = [-1]*N
def set_color(root):
    
    que = deque([root])
    color[root] = 0
    size[root][0]+=1
    while que:
        v = que.pop()
        for nv in edges[v]:
            if color[nv]<0:
                c = 1- color[v]
                color[nv] = c
                size[root][c]+=1
                que.append(nv)
            elif color[nv] == color[v]:
                print((-1))
                sys.exit()
for i in range(N):
    if color[i]<0:
        set_color(i)
S = set([0])
for a, b in list(size.values()):
    S = set(s+b for s in S)|set(s+a for s in S)
ans = min(x*(x-1)//2+(N-x)*(N-x-1)//2 for x in S)            
print(ans)                

# Optimized solution
from collections import defaultdict, deque
import sys
input = sys.stdin.readline
N, M = list(map(int, input().split()))
edges = [list(range(N)) for i in range(N)]
for i in range(N):
    edges[i].remove(i)
for _ in range(M):
    a, b = list(map(int, input().split()))
    edges[a-1].remove(b-1)
    edges[b-1].remove(a-1)
size = defaultdict(lambda: [0, 0])
color = [-1]*N
def set_color(root):
    
    que = deque([root])
    color[root] = 0
    size[root][0]+=1
    while que:
        v = que.pop()
        for nv in edges[v]:
            if color[nv]<0:
                c = 1- color[v]
                color[nv] = c
                size[root][c]+=1
                que.append(nv)
            elif color[nv] == color[v]:
                print((-1))
                sys.exit()
for i in range(N):
    if color[i]<0:
        set_color(i)
S = set([0])
for a, b in list(size.values()):
    S = set(s+b for s in S)|set(s+a for s in S)
ans = min(x*(x-1)//2+(N-x)*(N-x-1)//2 for x in S)            
print(ans)