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)
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)
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)

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)

-(*T,) = list(map(int, input().split())) -(*A,) = list(map(int, input().split())) +(*T,) = [0] + list(map(int, input().split())) + [0] +(*A,) = [0] + list(map(int, input().split())) + [0] -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)] +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 *= max(0, b - a + 1) -    ans %= mod +    ans = ans * max(0, b - a + 1) % mod