Problem:
 Score : 400 points    Problem Statement Mountaineers Mr. Takahashi and Mr. Aoki recently trekked across a certain famous mountain range.
The mountain range consists of N mountains, extending from west to east in a straight line as Mt. 1 , Mt. 2 , ..., Mt. N .
Mr. Takahashi traversed the range from the west and Mr. Aoki from the east.  The height of Mt. i is h_i , but they have forgotten the value of each h_i .
Instead, for each i ( 1 ≤ i ≤ N ), they recorded the maximum height of the mountains climbed up to the time they reached the peak of Mt. i (including Mt. i ).
Mr. Takahashi's record is T_i and Mr. Aoki's record is A_i .  We know that the height of each mountain h_i is a positive integer.
Compute the number of the possible sequences of the mountains' heights, modulo 10^9 + 7 .  Note that the records may be incorrect and thus there may be no possible sequence of the mountains' heights.
In such a case, output 0 .      Constraints  1 ≤ N ≤ 10^5  1 ≤ T_i ≤ 10^9  1 ≤ A_i ≤ 10^9  T_i ≤ T_{i+1} ( 1 ≤ i ≤ N - 1 )  A_i ≥ A_{i+1} ( 1 ≤ i ≤ N - 1 )         Input The input is given from Standard Input in the following format:  N  T_1  T_2  ...  T_N  A_1  A_2  ...  A_N       Output Print the number of possible sequences of the mountains' heights, modulo 10^9 + 7 .        Sample Input 1 5
1 3 3 3 3
3 3 2 2 2      Sample Output 1 4  The possible sequences of the mountains' heights are:   1, 3, 2, 2, 2   1, 3, 2, 1, 2   1, 3, 1, 2, 2   1, 3, 1, 1, 2    for a total of four sequences.       Sample Input 2 5
1 1 1 2 2
3 2 1 1 1      Sample Output 2 0  The records are contradictory, since Mr. Takahashi recorded 2 as the highest peak after climbing all the mountains but Mr. Aoki recorded 3 .       Sample Input 3 10
1 3776 3776 8848 8848 8848 8848 8848 8848 8848
8848 8848 8848 8848 8848 8848 8848 8848 3776 5      Sample Output 3 884111967  Don't forget to compute the number modulo 10^9 + 7 .       Sample Input 4 1
17
17      Sample Output 4 1  Some mountain ranges consist of only one mountain.

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
T = LIST()
A = LIST()

h = [0]*N
ma = -1
for i, t in enumerate(T):
	if t > ma:
		h[i] = t
		ma = t

ma = -1
for i, a in enumerate(A[::-1]):
	if a > ma:
		if h[-i-1] and a != h[-i-1]:
			print(0)
			exit()
		h[-i-1] = a
		ma = a

acc_t = list(accumulate(h, max))
acc_a = list(accumulate(h[::-1], max))[::-1]

if acc_t != T or acc_a != A:
	print(0)
	exit()

mi = [min(t, a) for t, a in zip(T, A)]

ans = 1
for i in range(N):
	if h[i] == 0:
		ans *= mi[i]
		ans %= mod
print(ans)

Solution (Python):
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
t = na()
a = na()
mod = 10**9+7

ans = 1
for i in range(n):
    tt,aa = 1,1
    if i == 0 or t[i-1] < t[i]:
        tt = t[i]
    if i == n-1 or a[i+1] < a[i]:
        aa = a[i]
    if tt > a[i] or aa > t[i]:
        print(0)
        exit()
    p = min(t[i],a[i]) - max(tt,aa) + 1
    ans = ans*p%mod

print(ans)

Solution (Python):
n = int(input())
T = list(map(int,input().split()))
A = list(map(int,input().split()))

ans = 1
mod = 10**9 + 7
inf = float('inf')

for i in range(n-1):
    if A[i] == A[i+1]:
        A[i] = -A[i]
    if T[n-i-1] == T[n-i-2]:
        T[n-i-1] = -T[n-i-1]

for t,a in zip(T,A):
    if t < 0 and a < 0:
        ans *= min(-t,-a)
        ans %= mod
    elif t < 0:
        if -t < a:
            ans *= 0
    elif a < 0:
        if -a < t:
            ans *= 0
    elif t != a:
        ans *= 0
print(ans)

Solution (Python):
N=int(input())
T=list(map(int,input().split()))
A=list(map(int,input().split()))
cand=[-1]*N
cand2=[-1]*N
flag=1
ans=1
p=10**9+7
for i in range(N):
    if i==0:
        cand[i]=T[i]
    else:
        if T[i]!=T[i-1]:
            cand[i]=T[i]
for i in range(N-1,-1,-1):
    if i==N-1:
        cand2[i]=A[i]
    else:
        if A[i]!=A[i+1]:
            cand2[i]=A[i]



ans=1
for i in range(N):
    if (cand[i]==-1)and(cand2[i]==-1):
        ans=(ans*min(T[i],A[i]))%p
    elif (cand[i]==-1):
        if T[i]<cand2[i]:
            ans=0
    elif (cand2[i]==-1):
        if A[i]<cand[i]:
            ans=0
    else:
        if cand[i]!=cand2[i]:
            ans=0
print(ans)



"""
lmax=[0]*N
rmax=[0]*N
for i in range(N):
    if i==0:
        lmax[i]=cand[i]
        rmax[N-1-i]=cand[N-1-i]
    else:
        if cand[i]==-1:
            lmax[i]=lmax[i-1]
        else:
            lmax[i]=cand[i]

        if cand[N-i-1]==-1:
            rmax[N-i-1]=rmax[N-i]
        else:
            rmax[N-i-1]=cand[N-i-1]

for i in range(N):
    if cand[i]==-1:
        ans=(ans*min(A[i],T[i]))%p
if flag==0:
    print("0")
else:
    print(ans%p)
"""

Solution (Python):


n = int(input())
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))
a = [[-1,-1] for i in range(n)]
b = [[-1,-1] for i in range(n)]

nowa = 0
nowb = 0
for i in range(n):
    if(aa[i]>nowa):
        a[i] = [aa[i],aa[i]]
        nowa = aa[i]
    else:
        a[i] = [1,aa[i]]
    if(bb[n-1-i]>nowb):
        b[n-1-i] = [bb[n-1-i],bb[n-1-i]]
        nowb = bb[n-1-i]
    else:
        b[n-1-i] = [1,nowb]


for i in range(n):
    a[i][0] = max(a[i][0],b[i][0])
    a[i][1] = min(a[i][1],b[i][1])
    
ans = 1
for i in range(n):
    ans *= max(0, a[i][1]-a[i][0]+1)
    ans%= 1000000007
print(ans)

Solution (Python):
from itertools import accumulate
import sys
N,*L = map(int, open(0).read().split())
MOD = 10**9+7
T = L[:N]
A = L[N:]
h = [0]*N
p = -1
for i in range(N):
  if p != T[i]:
    p = T[i]
    h[i] = T[i]
p = -1
for i in range(N-1,-1,-1):
  if p!=A[i]:
    if h[i]!=0 and h[i]!=A[i]:
      print(0)
      sys.exit()
    p = A[i]
    h[i] = A[i]
xt = list(accumulate(h,max))
xa = list(accumulate(h[::-1],max))[::-1]
if xt!=T or xa!=A:
  print(0)
  sys.exit()
B = [min(t,a) for t,a in zip(T,A)]
ans = 1
for i in range(N):
  if h[i]==0:
    ans *= B[i]
    ans %= MOD
print(ans)

Solution (Python):
N = int(input())
T = list(map(int,input().split()))
A = list(map(int,input().split()))
exact = [0] * N 
seem = [0] * N 

cur = 0
for i in range(len(T)):
  if cur < T[i]:
    exact[i] = T[i]
    cur = T[i]
  else:
    seem[i] = cur
    
cur = 0
for i in range(len(A)-1,-1,-1):
  if cur < A[i]:
    if exact[i] != 0 and exact[i] != A[i]: 
      print(0)
      exit(0)
    if exact[i] == 0 and seem[i] < exact[i]:
      print(0)
      exit(0)
    exact[i] = A[i]
    cur = A[i]
  else:
    
    if exact[i] != 0 and exact[i] > cur:
      print(0)
      exit(0)
    seem[i] = min(seem[i],cur)
    
DIV = 10 ** 9 + 7
ans = 1
for i in range(N):
  if exact[i] != 0: 
    continue
  else:
    ans *= seem[i]
    ans %= DIV

print(ans)

Solution (Python):
MOD = 10 **9 + 7
INF = 10 ** 10

def main():
    n = int(input())
    T = list(map(int,input().split()))
    A = list(map(int,input().split()))
    mountainT = [-1] * n
    mountainT[0] = T[0]
    for i in range(1,n):
        if T[i - 1] != T[i]:
            mountainT[i] = T[i]
    
    mountainA = [-1] * n
    mountainA[-1] = A[-1]
    for i in range(n - 1,0,-1):
        if A[i - 1] != A[i]:
            mountainA[i - 1] = A[i - 1]
    
    ans = 1
    for i in range(n):
        if mountainA[i] != -1 and mountainT[i] != -1:
            if mountainT[i] != mountainA[i]:
                ans = 0
                break
        elif mountainA[i] != -1:
            if mountainA[i] > T[i]:
                ans = 0
                break
        elif mountainT[i] != -1:
            if mountainT[i] > A[i]:
                ans = 0
                break
        else:
            ans *= min(T[i],A[i])
            ans %= MOD
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
t = list(map(int,input().split()))
if n == 1:
  if a[0] == t[0]:
    print(1)
  else:
    print(0)
  quit()
mod = 10**9+7
h = [[1,10**12] for i in range(n)]
h[0] = [a[0],a[0]]
h[-1] = [t[-1],t[-1]]
for i in range(1,n-1):
  h[i][1] = min(h[i][1],a[i])
  if a[i-1] < a[i]:
    h[i][0] = a[i]
for i in range(n-2,0,-1):
  h[i][1] = min(h[i][1],t[i])
  if t[i+1] < t[i]:
    h[i][0] = t[i]
ans = 1
for i in range(n):
  if h[i][0] > h[i][1]:
    print(0)
    quit()
  ans = (ans*(h[i][1]-h[i][0]+1))%mod
print(ans)

Solution (Python):
n=int(input())
t=[int(j) for j in input().split()]
a=[int(j) for j in input().split()]
tmp=0
l=[]
for i in t:
    if tmp<i:
        l.append(-i)
        tmp=i
    else:
        l.append(i)
tmp=0
ans=1
mod=10**9+7
for i in range(n)[::-1]:
    if tmp<a[i]:
        tmp=a[i]
        if l[i]<a[i]:
            if l[i]>0 or -l[i]!=a[i]:
                print(0)
                exit()
    else:
        if l[i]>0:
            ans=(ans*min(a[i],l[i]))%mod
print(ans)

Solution (Python):
def solve(n, aaa, bbb):
    
    max_heights = [0] * n
    fixed = [False] * n
    MOD = 10 ** 9 + 7

    h = 0
    for i, a in enumerate(aaa):
        if h != a:
            max_heights[i] = a
            fixed[i] = True
        else:
            max_heights[i] = a
        h = a
        
    h = 0
    for i, b in reversed(list(enumerate(bbb))):
        if h != b:
            if max_heights[i] < b:
                return 0
            if fixed[i] and max_heights[i] != b:
                return 0
            max_heights[i] = b
            fixed[i] = True
        else:
            max_heights[i] = min(max_heights[i], b)
        h = b

    ans = 1
    for h, f in zip(max_heights, fixed):
        if f:
            continue
        ans = ans * h % MOD
    return ans


n = int(input())
aaa = list(map(int, input().split()))
bbb = list(map(int, input().split()))
print(solve(n, aaa, bbb))

Solution (Python):
import sys


input = sys.stdin.readline




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


def II():
    return int(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):
    
            

    for d in directions:
        nx = d+x
        
        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(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
    
    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)

Solution (Python):
N=int(input())
T=list(map(int,input().split()))
A=list(map(int,input().split()))
mod=10**9+7


for i in range(0,N):
    if i==0:
        T[i]=(T[i],0)
    else:
        h,query=T[i-1]
        if h==T[i]:
            T[i]=(T[i],1)
        else:
            T[i]=(T[i],0)

for i in range(0,N):
    if i==0:
        A[-i-1]=(A[-i-1],0)
    else:
        h,query=A[-i]
        if h==A[-i-1]:
            A[-i-1]=(A[-i-1],1)
        else:
            A[-i-1]=(A[-i-1],0)

ans=1
for i in range(0,N):
    h1,q1=T[i]
    h2,q2=A[i]
    if q1==0:
        if q2==0:
            if h1!=h2:
                ans=0
        else:
            if h1>h2:
                ans=0
    else:
        if q2==0:
            if h2>h1:
                ans=0
        else:
            ans=(ans*min(h1,h2))%mod

print(ans)

Solution (Python):
N=int(input())
info1=list(map(int,input().split()))
info2=list(map(int,input().split()))
ans=0
if info1[-1]!=info2[0]: 
    print(ans)
else:
    a=[[0,info1[0]]]
    highest=info1[0]
    for i in range(1,N):
        if highest<info1[i]:
            highest=info1[i]
            a.append([0,info1[i]])
        else:
            a.append([1,highest])
    b=[[0,info2[-1]]]
    highest=info2[-1]
    for i in range(1,N):
        if highest<info2[-(i+1)]:
            highest=info2[-(i+1)]
            b.append([0,info2[-(i+1)]])
        else:
            b.append([1,highest])
    flag=True
    c=[]
    for i in range(N): 
        if a[i][0]==0 and b[-(i+1)][0]==0:
            if a[i][1]==b[-(i+1)][1]:
                c.append([0,a[i][1]])
            else:
                flag=False
                break
        elif a[i][0]==1 and b[-(i+1)][0]==1:
            c.append([1,min(a[i][1],b[-(i+1)][1])])
        else:
            if a[i][0]==0:
                if a[i][1]<=b[-(i+1)][1]:
                    c.append([0,a[i][1]])
                else:
                    flag=False
                    break
            else:
                if b[-(i+1)][1]<=a[i][1]:
                    c.append([0,b[-(i+1)][1]])
                else:
                    flag=False
                    break
    if flag:
        mod=10**9+7
        ans=1
        for i in range(N):
            if c[i][0]==1:
                ans=(ans*c[i][1])%mod
        print(ans)
    else:
        print(0)

Solution (Python):
N = int(input())
T = list(map(int, input().split()))
A = list(map(int, input().split()))
INF = 10**10
MOD = 10**9 + 7

H = [0] * N
_H = [INF] * N
pre_h = 0
for i, h in enumerate(T):
  if h != pre_h:
    H[i] = h
    pre_h = h
  else:
    _H[i] = min(_H[i], h)

pre_h = 0
for i, h in enumerate(A[::-1], 1):
  if h != pre_h:
    if (H[-i] > 0 and H[-i] != h) or _H[-i] < h:
      print(0)
      quit()
    H[-i] = h
    pre_h = h
  else:
    _H[-i] = min(_H[-i], h)

ans = 1
for h, _h in zip(H, _H):
  if h == 0:
    ans *= _h
    ans %= MOD

print(ans)

Solution (Python):
N = int(input())
T = list(map(int, input().split(" ")))
A = list(map(int, input().split(" ")))
h = [0 for i in range(N)]
mod = 10 ** 9 + 7

h[0] = T[0]
for i in range(N - 1):
    if T[i] < T[i + 1]:
        h[i + 1] = T[i + 1]
    else:
        h[i + 1] = -T[i + 1]

if h[N - 1] == 0:
    h[N - 1] = A[N - 1]
else:
    if -h[N - 1] >= A[N - 1]:
        h[N - 1] = A[N - 1]
    elif h[N - 1] != A[N - 1]:
        h[N - 1] = 0

for i in range(1, N):
    if A[i - 1] > A[i]:
        if -h[i - 1] >= A[i - 1]:
            h[i - 1] = A[i - 1]
        elif h[i - 1] != A[i - 1]:
            h[i - 1] = 0
    else:
        if h[i - 1] < 0:
            h[i - 1] = -min(-h[i - 1], A[i - 1])


ans = 1
for hi in h:
    if hi <= 0:
        ans *= -hi
        ans %= mod

print(ans)

Solution (Python):
N = int(input())
T = list(map(int, input().split()))
A = list(map(int, input().split()))
if T[0] > A[0] or T[-1] < A[-1]:
    print(0)
    exit()
for i in range(1, N):
    if T[i-1] != T[i] and T[i] > A[i]:
        print(0)
        exit()
    if A[N-i-1] != A[N-i] and A[N-i-1] > T[N-i-1]:
        print(0)
        exit()
h = [-1] * N
h[0] = T[0]
h[-1] = A[-1]
for i in range(1, N-1):
    if T[i] != T[i-1]:
        h[i] = T[i]
    if A[i] != A[i+1]:
        h[i] = A[i]
ans, mod = 1, 10**9+7
for i in range(1, N-1):
    if h[i] == -1:
        ans = ans * min(A[i], T[i]) % mod
print(ans)

Solution (Python):
n = int(input())
T = list(map(int,input().split()))
A = list(map(int,input().split()))
mod = 10**9+7
if max(A)!=max(T):
    print(0)
    exit()
l = [True] + [False]*(n-1)
r = [False]*(n-1) + [True]

for i in range(1, n):
    if T[i] != T[i-1]:
        l[i] = True
for i in range(n-1):
    if A[i] != A[i+1]:
        r[i] = True
for i in range(n):
    if r[i] and A[i]>T[i]:
        print(0)
        exit()       
ans = 1
for i in range(n):
    if l[i]==False and r[i]==False:
        ans *= min(T[i], A[i])
        ans %= mod
print(ans%mod)

Solution (Python):
n = int(input())
t = list(map(int, input().split()))
a = list(map(int, input().split()))
mod = 10 ** 9 + 7

tt = []
prev = 0
for e in t:
    if e > prev:
        tt.append([e, e])
        prev = e
    else:
        tt.append([1, e])

aa = []
prev = 0
for e in a[::-1]:
    if e > prev:
        aa.append([e, e])
        prev = e
    else:
        aa.append([1, e])
aa = aa[::-1]

ans = 1
for et, ea in zip(tt, aa):
    mn = max(et[0], ea[0])
    mx = min(et[1], ea[1])
    ans *= max(0, mx - mn + 1)
    ans %= mod

print(ans)

Solution (Python):
n = int(input())
t = list(map(int, input().split()))
a = list(map(int, input().split()))
mod = 10 ** 9 + 7

if max(t) != max(a):
    print(0)
    exit()


def f(li, rev):
    if rev:
        li = li[::-1]

    cnts = [0] * n
    cands = [0] * n
    mx = 0
    cnt = 0
    for i, e in enumerate(li):
        if e > mx:
            cnts[i] = 1
            cands[i] = e
            cnt = e
            mx = e
        else:
            cnts[i] = cnt

    if rev:
        cnts = cnts[::-1]
        cands = cands[::-1]

    return cnts, cands


t_cnt, t_cand = f(t, False)
a_cnt, a_cand = f(a, True)

for e1, e2 in zip(a, t_cand):
    if e2 > e1:
        print(0)
        exit()

ans = 1
for e1, e2 in zip(t_cnt, a_cnt):
    ans *= min(e1, e2)
    ans %= mod

print(ans)

Solution (Python):
n = int(input())
t = list(map(int, input().split()))
a = list(map(int, input().split()))
mod = 10 ** 9 + 7

if max(t) != max(a):
    print(0)
    exit()


def f(li, rev):
    if rev:
        li = li[::-1]

    cnts = [0] * n
    cands = [0] * n
    mx = 0
    cnt = 0
    for i, e in enumerate(li):
        if e > mx:
            cnts[i] = 1
            cands[i] = e
            cnt = e
            mx = e
        else:
            cnts[i] = cnt

    if rev:
        cnts = cnts[::-1]
        cands = cands[::-1]

    return cnts, cands


t_cnt, t_cand = f(t, False)
a_cnt, a_cand = f(a, True)

for e1, e2 in zip(t, a_cand):
    if e2 > e1:
        print(0)
        exit()

ans = 1
for e1, e2 in zip(t_cnt, a_cnt):
    ans *= min(e1, e2)
    ans %= mod

print(ans)

Solution (Python):
import sys
mod=10**9+7
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))


if a[0]>b[0] or b[n-1]>a[n-1]: print(0);sys.exit()
elif n<=2: print(1);sys.exit()

ans=1
for i in range(1,n-1):
    if a[i]==a[i-1] and b[i]==b[i+1]:
        ans=ans*min(a[i],b[i])%mod
    else:
        if a[i]!=a[i-1] and a[i]>b[i]: print(0);sys.exit()
        if b[i]!=b[i+1] and b[i]>a[i]: print(0);sys.exit()
print(ans)

Solution (Python):

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


prev = -1
candidates_t = [-1 for _ in range(N)]
for i in range(N):
    if T[i] != prev:
        candidates_t[i] = T[i]

    prev = T[i]


prev = -1
candidates_a = [-1 for _ in range(N)]
for i in range(N-1, -1, -1):
    if A[i] != prev:
        candidates_a[i] = A[i]

    prev = A[i]

ret = 1
for i in range(N):
    if candidates_t[i] == -1 and candidates_a[i] == -1:
        ret *= min(T[i], A[i])
        ret %= 10**9 + 7

    elif candidates_t[i] == -1:
        if T[i] < candidates_a[i]:
            ret *= 0

    elif candidates_a[i] == -1:
        if A[i] < candidates_t[i]:
            ret *= 0

    elif candidates_t[i] != candidates_a[i]:
        ret *= 0


print(ret)

Solution (Python):
import sys

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

MOD = 10 ** 9 + 7

t_max = [0] * N
t_min = [1] * N
tmp = -1
for i in range(N):
    t = T[i]
    if t > tmp:
        t_max[i] = t
        t_min[i] = t
    elif t == tmp:
        t_max[i] = t
    else:
        print(0)
        sys.exit(0)
    tmp = t

a_max = [0] * N
a_min = [1] * N
tmp = -1
for i in range(N-1, -1, -1):
    a = A[i]
    if a > tmp:
        a_max[i] = a
        a_min[i] = a
    elif a == tmp:
        a_max[i] = a
    else:
        print(0)
        sys.exit(0)
    tmp = a

answer = 1
for i in range(N):
    tmp = min(t_max[i], a_max[i]) - max(t_min[i], a_min[i])
    if tmp < 0:
        print(0)
        sys.exit(0)
    answer *= tmp + 1
    answer %= MOD

print(answer)

Solution (Python):
import sys

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

MOD = 10 ** 9 + 7

t_max = [0] * N
t_min = [1] * N
tmp = -1
for i in range(N):
    t = T[i]
    if t > tmp:
        t_max[i] = t
        t_min[i] = t
    elif t == tmp:
        t_max[i] = t
    else:
        print(0)
        sys.exit(0)
    tmp = t

a_max = [0] * N
a_min = [1] * N
tmp = -1
for i in range(N-1, -1, -1):
    a = A[i]
    if a > tmp:
        a_max[i] = a
        a_min[i] = a
    elif a == tmp:
        a_max[i] = a
    else:
        print(0)
        sys.exit(0)
    tmp = a

answer = 1
for i in range(N):
    tmp = min(t_max[i], a_max[i]) - max(t_min[i], a_min[i])
    if tmp < 0:
        answer = 0
        break
    answer *= tmp + 1
    answer %= MOD

print(answer)

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


n = I()
T = LI()
A = LI()
flg = [0] * n
pre = 0
for i in range(n):
    if T[i] > pre:
        flg[i] = 1
        pre = T[i]

pre = 0
for j in range(n - 1, -1, -1):
    if A[j] == pre:
        if flg[j]:
            if T[j] > A[j]:
                print(0)
                exit()
        else:
            T[j] = min(T[j], A[j])
    else:
        if flg[j] and T[j] != A[j]:
            print(0)
            exit()
        else:
            if A[j] > T[j]:
                print(0)
                exit()
            else:
                T[j] = A[j]
                flg[j] = 1
                pre = A[j]

ans = 1
for k in range(n):
    if flg[k] == 0:
        ans = ans * T[k] % mod

print(ans)

Solution (Python):
mod = 10**9+7
N = int(input())
T = list(map(int,input().split()))
A = list(map(int,input().split()))
Tmax = (0,-1)
Amax = (0,-1)
h = [0]*N
not_confirmed = [True]*N
for i in range(N):
    if Tmax[0] < T[i]:
        Tmax = (T[i],i)
        not_confirmed[i] = False
    if Amax[0] < A[N-1-i]:
        Amax = (A[N-1-i],N-1-i)
        not_confirmed[N-1-i] = False
    h[i] = min(T[i],A[i])
if (Amax[1] < Tmax[1]) or (Tmax[0] != Amax[0]):
    print(0)
    exit()

ans = 1
for i in range(N):
    ans *= h[i] if not_confirmed[i] else 1
    ans %= mod
print(ans)

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]

is_ok_a = [False] * n
max_a = [0]*n
pre = -1
for i, j in enumerate(a):
    if j != pre:
        pre = j
        is_ok_a[i] = True
    max_a[i] = pre

is_ok_b = [False] * n
max_b = [0]*n
pre = -1
for i, j in reversed(list(enumerate(b))):
    if j != pre:
        pre = j
        is_ok_b[i] = True
    max_b[i] = pre
mod = 10**9+7
ans = 1
for i in range(n):
    if is_ok_a[i] and is_ok_b[i]:
        if max_a[i] == max_b[i]:
            ans *= 1
        else:
            ans *= 0
    elif is_ok_a[i]:
        if max_a[i] <= max_b[i]:
            ans *= 1
        else:
            ans *= 0
    elif is_ok_b[i]:
        if max_a[i] >= max_b[i]:
            ans *= 1
        else:
            ans *= 0
    else:
        ans *= min(max_a[i], max_b[i])
    ans %= mod
print(ans)

Solution (Python):
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

IN=lambda : map(int, input().split())
mod=1000000007



def main():
	n = int(input())
	al = list(IN())
	bl=list(IN())
	mm=[]
	for i in range(n):
		mm.append([0,0])
	im=0
	for i, h in enumerate(al):
		if im < h:
			mm[i] = [h,h]
			im=h
		else:
			mm[i] = [1,h]
	jm=0
	for i, h in enumerate(bl[::-1]):
		j=n-i-1
		if jm < h:
			if mm[j][0]<= h and h <= mm[j][1]:
				mm[j] = [h,h]
				jm=h
			else:
				return 0
		else:
			if mm[j][0]<= h and h <= mm[j][1]:
				mm[j][1]=h
			elif h < mm[j][0]:
				return 0
			else:
				pass
	r=1
	for a,b in mm:
		r *= (b-a+1)
		r %= mod
	print(r)
		
	
	
	

isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

Solution (Python):
"""from collections import *
from itertools import *
from bisect import *
from heapq import *

import math
from fractions import gcd"""

N=int(input())
T=list(map(int,input().split()))
A=list(map(int,input().split()))
mod=10**9+7
lst=[0 for i in range(N)]


lst[0]=T[0]
lst[N-1]=A[N-1]
if T[N-1]!=A[0]:
    print(0)
    exit()
for i in range(1,N):
    if T[i]>T[i-1]:
        if lst[i] and lst[i]!=T[i]:
            print(0)
            exit()
        lst[i]=T[i]

for i in range(N-2,-1,-1):
    if A[i]>A[i+1]:
        if lst[i] and lst[i]!=A[i]:
            print(0)
            exit()
        elif not lst[i] and T[i]<A[i]:
            print(0)
            exit()
        lst[i]=A[i]

count=1
for i in range(N):
    if not lst[i]:
        count=count*min(T[i],A[i])%mod
print(count)

Solution (Python):
import sys
input = sys.stdin.buffer.readline
from operator import itemgetter

def main():
    N = int(input())
    
    t = list(map(int,input().split()))
    a = list(map(int,input().split()))
    MOD = 10**9+7
    
    tt,tl = 0,[False for _ in range(N)]
    for x,num in enumerate(t):
        if num > tt:
            tl[x] = True
            tt = num
            
    at,al = 0,[False for _ in range(N)]
    for x,num in enumerate(a[::-1]):
        if num > at:
            al[-x-1] = True
            at = num

    ans,now = 1,0
    for i in range(N):
        if tl[i] == True and al[i] == True:
            if now < t[i] == a[i]:
                now = a[i]
            else:
                print(0)
                exit()
        elif tl[i] == True and al[i] == False:
            if t[i] > a[i]:
                print(0)
                exit()
            else:
                now = t[i]
        elif tl[i] == False and al[i] == True:
            if t[i] < a[i]:
                print(0)
                exit()
            else:
                now = a[i]
        else:
            ans *= min(t[i],a[i])
            ans %= MOD
    
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = sys.stdin.readline

N = int(input())
T = list(map(int, input().split()))
A = list(map(int, input().split()))
maxh = [0] * N
if T[0] > A[0] or T[-1] < A[-1]:
    print(0)
    exit()

maxh[0] = -T[0]
for i in range(1, N):
    if T[i-1] == T[i]:
        maxh[i] = T[i]
    else:
        maxh[i] = -T[i]

maxh[-1] = -A[-1]
for i in range(N-1)[::-1]:
    if maxh[i] > 0:
        if A[i] == A[i+1]:
            maxh[i] = min(maxh[i], A[i])
        else:
            if maxh[i] < A[i]:
                print(0)
                exit()
            else:
                maxh[i] = -A[i]
    else:
        if A[i] == A[i+1]:
            if -maxh[i] > A[i]:
                print(0)
                exit()
        else:
            if -maxh[i] != A[i]:
                print(0)
                exit()

ans = 1
mod = 10**9+7
for i in range(N):
    if maxh[i] > 0:
        ans = ans * maxh[i] % mod
print(ans)

Solution (Python):
n = int(input())
t = list(map(int, input().split()))
a = list(map(int, input().split()))
mod = 10**9 + 7

h = [-1 for i in range(n)]
t = [0] + t
a += [0]
for i in range(n):
    if t[i+1] != t[i]:
        h[i] = t[i+1]
for i in range(n,0,-1):
    if h[i-1] != -1 and h[i-1] > a[i-1]:
        print(0)
        exit()
    if a[i] != a[i-1]:
        if h[i-1] == -1:
            h[i-1] = a[i-1]
        else:
            if h[i-1] != a[i-1]:
                print(0)
                exit()
ans = 1
for i in range(n):
    if h[i] == -1:
        
        ans *= min(t[i+1], a[i])
        ans %= mod
print(ans)

Solution (Python):
import sys

def solve():
    input = sys.stdin.readline
    N = int(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(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()

Solution (Python):
def main():
    MOD = 10**9 + 7
    N = int(input())
    T = [int(i) for i in input().split()]
    A = [int(i) for i in input().split()]
    if T[-1] != A[0]:
        return print(0)
    ma_idx_T = T.index(max(T))
    
    if A[ma_idx_T] != T[ma_idx_T]:
        return print(0)
    non_fix = [True] * N
    non_fix[0] = False
    non_fix[-1] = False
    for i in range(1, N):
        if T[i-1] < T[i]:
            non_fix[i] = False
            ma_idx_T = i
        else:
            continue

    for i in range(N-1)[::-1]:
        if A[i] > A[i+1]:
            non_fix[i] = False
        else:
            continue
    ans = 1
    
    for i, f in enumerate(non_fix):
        if f:
            ans *= min(T[i], A[i])
            ans %= MOD
    print(ans % MOD)


if __name__ == '__main__':
    main()