n = int(eval(input()))

t = list(map(int, input().split()))

a = list(map(int, input().split()))

mod = 10 ** 9 + 7



ans, l, r = 1, 0, n - 1

for i in range(1, n):

    if t[i] == t[i - 1]:

        ans = ans * t[i] % mod

    if t[i] == a[i]:

        l = i

        break

for i in range(n - 1)[::-1]:

    if a[i] == a[i + 1]:

        ans = ans * a[i] % mod

    if t[i] == a[i]:

        r = i

        break



if t[l] != max(t) or a[r] != max(a) or max(t) != max(a):

    print((0))

else:

    print((ans * pow(t[l], max(r - l - 1, 0), mod) % mod))

n = int(eval(input()))

t = list(map(int, input().split()))

a = list(map(int, input().split()))

mod = 10 ** 9 + 7



ans = 1

if n == 1:

    print((int(t == a)))

    exit()

for i in range(1, n - 1):

    ht, ha = 0, 0

    if t[i - 1] < t[i] and a[i] > a[i + 1]:

        if t[i] != a[i]:

            print((0))

            break

    elif t[i - 1] < t[i]:

        if t[i] > a[i]:

            print((0))

            break

    elif a[i] > a[i + 1]:

        if t[i] < a[i]:

            print((0))

            break

    else:

        ans = ans * min(t[i], a[i]) % mod

else:

    print(ans)

n = int(eval(input()))
t = list(map(int, input().split()))
a = list(map(int, input().split()))
mod = 10**9 + 7
ans, l, r = 1, 0, n - 1
for i in range(1, n):
    if t[i] == t[i - 1]:
        ans = ans * t[i] % mod
    if t[i] == a[i]:
        l = i
        break
for i in range(n - 1)[::-1]:
    if a[i] == a[i + 1]:
        ans = ans * a[i] % mod
    if t[i] == a[i]:
        r = i
        break
if t[l] != max(t) or a[r] != max(a) or max(t) != max(a):
    print((0))
else:
    print((ans * pow(t[l], max(r - l - 1, 0), mod) % mod))

n = int(eval(input()))
t = list(map(int, input().split()))
a = list(map(int, input().split()))
mod = 10**9 + 7
ans = 1
if n == 1:
    print((int(t == a)))
    exit()
for i in range(1, n - 1):
    ht, ha = 0, 0
    if t[i - 1] < t[i] and a[i] > a[i + 1]:
        if t[i] != a[i]:
            print((0))
            break
    elif t[i - 1] < t[i]:
        if t[i] > a[i]:
            print((0))
            break
    elif a[i] > a[i + 1]:
        if t[i] < a[i]:
            print((0))
            break
    else:
        ans = ans * min(t[i], a[i]) % mod
else:
    print(ans)

-ans, l, r = 1, 0, n - 1 -for i in range(1, n): -    if t[i] == t[i - 1]: -        ans = ans * t[i] % mod -    if t[i] == a[i]: -        l = i -        break -for i in range(n - 1)[::-1]: -    if a[i] == a[i + 1]: -        ans = ans * a[i] % mod -    if t[i] == a[i]: -        r = i -        break -if t[l] != max(t) or a[r] != max(a) or max(t) != max(a): -    print((0)) +ans = 1 +if n == 1: +    print((int(t == a))) +    exit() +for i in range(1, n - 1): +    ht, ha = 0, 0 +    if t[i - 1] < t[i] and a[i] > a[i + 1]: +        if t[i] != a[i]: +            print((0)) +            break +    elif t[i - 1] < t[i]: +        if t[i] > a[i]: +            print((0)) +            break +    elif a[i] > a[i + 1]: +        if t[i] < a[i]: +            print((0)) +            break +    else: +        ans = ans * min(t[i], a[i]) % mod -    print((ans * pow(t[l], max(r - l - 1, 0), mod) % mod)) +    print(ans)