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

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)

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)

-import sys - -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]: +mod = 10**9 + 7 +yama = [0] * n +# tは、t[i] == t[i + 1]だったら左の以外全部ありうる +yama[0] = t[0] +if t[0] > a[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]: +    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]: -            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]: +            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]: -            sys.exit(0) -# calc ans +            exit() +if a[n - 1] > t[n - 1]: +    print((0)) +    exit() +yama[n - 1] = a[n - 1] -for i in range(n): -    if not fixed[i]: -        ans = ans * h[i] % (10**9 + 7) +for i in range(1, n - 1): +    if yama[i] == 0: +        ans = (ans * min(a[i], t[i])) % mod