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

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)

-def inpl(): -    return [int(i) for i in input().split()] +import sys + +mod = 10**9 + 7 -mod = 10**9 + 7 +def nai(): +    print((0)) +    sys.exit() + + -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: +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: -    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)) +    if pre == a: +        ans = a * ans % mod +    pre = a +print(ans)