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):
n = int(input())
qs1 = list(map(int,input().split(" ")))
qs2 = list(map(int,input().split(" ")))
qs2.reverse()
ans = 1
MOD = 10** 9 + 7
num1 = 0
num2  = 0
qss = [[],[]]
for i,qs in enumerate([qs1,qs2]):
    qss[i].append((qs[0], 1))
    for t1,t2 in zip(qs[0:n-1],qs[1:n]):
        if t1 >t2:
            ans *= 0
        elif t1 == t2:
            qss[i].append((t2,0))
        else:
            qss[i].append((t2, 1))

qss[1].reverse()

for q1,q2 in zip(qss[0],qss[1]):
    qt1 = q1[1]
    qt2 = q2[1]
    num1 = q1[0]
    num2 = q2[0]
    if qt1 == 0 and qt2 == 0: 
        ans *= min(num1, num2)


    elif qt1 == 0 and qt2 == 1:
        if num1 >= num2:
            ans *= 1
        else:
            ans *= 0

    elif qt1 == 1 and qt2 == 0:
        if num2 >= num1:
            ans *= 1
        else:
            ans *= 0
    else:
        if num1 != num2:
            ans *= 0
    ans %= MOD
print(ans)

Solution (Python):
N = int(input())
T = list(map(int,input().split()))
A = list(map(int,input().split()))
T_min = [1]*N
T_min[0] = T[0]
for i in range(1,N):
  if T[i-1]<T[i]:
    T_min[i] = T[i]
A_min = [1]*N
A_min[N-1] = A[N-1]
for i in range(N-2,-1,-1):
  if A[i+1]<A[i]:
    A_min[i] = A[i]
M = [0]*N
M_min = [0]*N
for i in range(N):
  M[i] = min(T[i],A[i])
  M_min[i] = max(T_min[i],A_min[i])
ans = 1
mod = 10**9+7
for i in range(N):
  if M[i]<M_min[i]:
    ans = 0
  else:
    ans*=M[i]+1-M_min[i]
    ans%=mod
print(ans)

Solution (Python):
def init(lst):
    l = [lst[0]]
    r = [lst[0]]
    for i in range(1, len(lst)):
        if lst[i - 1] < lst[i]:
            l.append(lst[i])
            r.append(lst[i])
        else:
            l.append(1)
            r.append(lst[i])
    return l, r

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

l1, r1 = init(t)
a.reverse()
l2, r2 = init(a)
l2.reverse()
r2.reverse()

l3 = []
r3 = []
for i in range(n):
    if l2[i] <= r1[i] and l1[i] <= r2[i]:
        l = max(l1[i], l2[i])
        r = min(r1[i], r2[i])
        l3.append(l)
        r3.append(r)
    else:
        print(0)
        exit(0)

ans = 1
for i in range(n):
    ans *= r3[i] - l3[i] + 1
    ans %= 10 ** 9 + 7
print(ans)

Solution (Python):
def end():
    print(0)
    exit()

mod = 10**9+7
n = int(input())
t = list(map(int, input().split()))
a = list(map(int, input().split()))
t1 = [0]*n
a1 = [0]*n
t1[0] = a1[-1] = 1

for i in range(1, n):
    if t[i-1] != t[i]:
        t1[i] = 1
    if a[n-1-i] != a[n-i]:
        a1[n-1-i] = 1

ans = 1
for i in range(n):
    if t1[i] and a1[i]:
        if t[i] != a[i]:
            end()
    elif t1[i]:
        if t[i] > a[i]:
            end()
    elif a1[i]:
        if t[i] < a[i]:
            end()
    else:
        ans *= min(t[i], a[i])
        ans %= mod
print(ans)

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

H = [[1,10 ** 9] for _ in range(N)]

for i in range(N):
  if i == 0:
    hmin = T[i]
    hmax = T[i]
  elif T[i-1] < T[i]:
    hmin = T[i]
    hmax = T[i]
  else:
    hmin = 1
  H[i][0] = hmin
  H[i][1] = hmax
  
for i in range(N-1,-1,-1):
  if i == N-1:
    hmin = A[i]
    hmax = A[i]
  elif A[i] > A[i+1]:
    hmax = A[i]
    hmin = A[i]
  else:
    hmin = 1
  H[i][0] = max(hmin,H[i][0])
  H[i][1] = min(hmax,H[i][1])
  


ans = 1
for i in range(N):
  if H[i][1] < H[i][0]:
    print(0)
    exit()
  ans *= (H[i][1] - H[i][0] + 1)
  ans %= 10 ** 9 + 7
print(ans)

Solution (Python):
N = int(input())
T = [int(i) for i in input().split()]
A = [int(i) for i in input().split()] + [0]

MOD = 10**9 + 7

fixed = [False] * N
fixed[0] = True

for i in range(1, N):
    if T[i] > T[i-1]:
        fixed[i] = True

ans = 1
for i in range(N-1, -1, -1):
    if A[i] > A[i+1] and A[i] > T[i]:
        print(0)
        exit()
    if fixed[i] is True and A[i] < T[i]:
        print(0)
        exit()
    if fixed[i] is False and A[i] == A[i+1]:
        ans *= min(A[i], T[i])
        ans %= MOD
print(ans)

Solution (Python):
N = int(input())
Ts = list(map(int, input().split()))
As = list(map(int, input().split()))
c = 0
Hmax = [0]*N
Hmin = [1]*N
for i in range(N):
  T = Ts[i]
  Hmax[i] = T
  if T > c:
    Hmin[i] = T
    c = T
c = 0
for i in reversed(range(N)):
  A = As[i]
  Hmax[i] = min(Hmax[i], A)
  if A > c:
    Hmin[i] = max(Hmin[i], A)
    c = A


r = 1
for i in range(N):
  d = Hmax[i]-Hmin[i]+1
  if d < 0: d = 0
  r = r*d%1000000007
print(r)

Solution (Python):
def main():

    N = int(input())
    T = list(map(int, input().split()))
    A = list(map(int, input().split()))
    mod = pow(10, 9) + 7

    heightT = []
    for i in range(N):
        if i == 0:
            heightT.append([T[i], "exact"])
        else:
            if T[i] > T[i-1]:
                heightT.append([T[i], "exact"])
            elif T[i] == T[i-1]:
                heightT.append([T[i], "bound"])
            else:
                return 0
    heightA = []
    for i in range(N-1, -1, -1):
        if i == N-1:
            heightA.append([A[i], "exact"])
        else:
            if A[i+1] < A[i]:
                heightA.append([A[i], "exact"])
            elif A[i+1] == A[i]:
                heightA.append([A[i], "bound"])
            else:
                return 0
    heightA = heightA[::-1]

    ans = 1
    for i in range(N):
        if heightT[i][1] == "exact" and heightA[i][1] == "exact":
            if heightT[i][0] != heightA[i][0]:
                return 0
        elif heightT[i][1] == "exact" and heightA[i][1] == "bound":
            if heightT[i][0] > heightA[i][0]:
                return 0
        elif heightT[i][1] == "bound" and heightA[i][1] == "exact":
            if heightT[i][0] < heightA[i][0]:
                return 0
        else:
            ans *= min(heightT[i][0], heightA[i][0])
            ans %= mod
    return ans


if __name__ == '__main__':
    print(main())

Solution (Python):
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)

from math import factorial
import heapq, bisect
import math
import itertools


import queue
from collections import deque




def main():
    num = int(input())
    data1 = list(map(int, input().split()))
    data2 = list(map(int, input().split()))

    dp1 = [0 for i in range(num)]
    dp2 = [0 for i in range(num)]

    dp1[0] = 1
    for i in range(1, num):
        if data1[i] > data1[i - 1]:
            dp1[i] = 1
        
        

    data2 = data2[::-1]
    dp2[0] = 1
    for i in range(1, num):
        if data2[i] > data2[i - 1]:
            dp2[i] = 1
        
        
    dp2 = dp2[::-1]
    data2 = data2[::-1]

    ans = 1
    mod = 10 ** 9 + 7
    for i in range(num):
        if dp1[i] and dp2[i]:
            if data1[i] == data2[i]:
                pass
            else:
                ans *= 0
        elif dp1[i]:
            if data1[i] <= data2[i]:
                pass
            else:
                ans *= 0
        elif dp2[i]:
            if data2[i] <= data1[i]:
                pass
            else:
                ans *= 0
        else:
            ans *= min(data1[i], data2[i])
        ans %= mod

    print(ans)






if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())

t_list = [ int(v) for v in input().split() ]
a_list = [ int(v) for v in input().split() ]
a_list_reverse = list(reversed(a_list))

t_confirm_list = [t_list[0]] + [None for i in range(n-1)]
a_confirm_list = [a_list_reverse[0]] + [None for i in range(n-1)]


for i in range(n-1):
    if t_list[i]  < t_list[i+1]:
        t_confirm_list[i+1] = t_list[i+1]
    if a_list_reverse[i]  < a_list_reverse[i+1]:
        a_confirm_list[i+1] = a_list_reverse[i+1]

a_confirm_list.reverse()


mod = 10**9+7
ans = 1
for i in range(n):
    if a_confirm_list[i] != None and t_confirm_list[i] != None:
        if a_confirm_list[i] != t_confirm_list[i]:
            ans = 0
    elif a_confirm_list[i] == None and t_confirm_list[i] == None:
        ans *= min(a_list[i],t_list[i])
        ans %= mod
    elif a_confirm_list[i] is not None and a_confirm_list[i] > t_list[i]:
            ans = 0
    elif t_confirm_list[i] is not None and t_confirm_list[i] > a_list[i]:
            ans = 0

print(ans)

Solution (Python):



import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
t = LI()
a = LI()
x = [0]*n
y = [0]*n
if a[0] != t[-1]:
    print(0)
    quit()
for i in range(1,n):
    if t[i] > t[i-1]:
        x[i] = 1
for i in range(1,n)[::-1]:
    if a[i] < a[i-1]:
        y[i-1] = 1
ans = 1
for i in range(1,n-1):
    if x[i] == 1 and y[i] == 1:
        if t[i] != a[i]:
            print(0)
            quit()
    elif x[i] == 1:
        if a[i] < t[i]:
            print(0)
            quit()
    elif y[i] == 1:
        if a[i] > t[i]:
            print(0)
            quit()
    else:
        ans *= min(t[i],a[i])
        ans %= mod
print(ans)

Solution (Python):



import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
t = LI()
a = LI()
x = [0]*n
y = [0]*n
if a[0] != t[-1]:
    print(0)
    quit()
for i in range(1,n):
    if t[i] > t[i-1]:
        x[i] = 1
for i in range(1,n)[::-1]:
    if a[i] < a[i-1]:
        y[i-1] = 1
ans = 1
for i in range(1,n-1):
    if x[i] == 1 and y[i] == 1:
        if t[i] != a[i]:
            print(0)
            quit()
        continue
    elif x[i] == 1:
        if a[i] >= t[i]:
            continue
        else:
            print(0)
            quit()
    elif y[i] == 1:
        if a[i] <= t[i]:
            continue
        else:
            print(0)
            quit()
    ans *= min(t[i],a[i])
    ans %= mod
print(ans)

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

exact = [False]*N

height = [0]*N
height[0] = T[0]
exact[0] = True

for i in range(1,N):
    height[i] = T[i]
    exact[i] = T[i]>T[i-1]

if (exact[-1] and A[-1]!=height[-1]) or A[-1]>height[-1]:
    print(0)
    exit()

height[-1] = A[-1]
exact[-1] = True

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

ans=1
mod=10**9+7
for e,h in zip(exact, height):
    if not e:
        ans=(ans*h)%mod

print(ans)

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

if T[-1] != A[0]:
    print(0)
    exit()

max_h = [1] * N




h = [None] * N

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

        ht = ha = None
        if T[i] > T[i-1]:
            ht = T[i]
        if A[i] > A[i+1]:
            ha = A[i]

        if ht != None and ha != None:
            if ht == ha:
                h[i] = ht
            else:
                print(0) 
                exit()
        
        elif ht != None:
            if A[i] < ht:
                print(0) 
                exit()
            
        elif ha != None:
            if T[i] < ha:
                print(0) 
                exit()
            
        

        elif ht == None and ha == None:
            max_h[i] = min(T[i], A[i])


MOD = 10**9 + 7
def mul(a, b):
    return ((a % MOD) * (b % MOD)) % MOD
ans = 1
for i in range(N):
    ans = mul(ans, max_h[i])
    

print(ans)

Solution (Python):
N=int(input());*T,=map(int,input().split());*A,=map(int,input().split());a=int(A[0]==T[-1])
for p,q,r,s in zip(T,T[1:],A[1:],A[2:]):
 if(p==q)&(r==s):a=a*min(q,r)%(10**9+7)
 elif(p!=q)&(q>r)|(r!=s)&(q<r):a*=0
print(a)

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

def f(s1, s2):
  a = s1[0]
  b = s1[1]
  c = s2[0]
  d = s2[1]
  if a>c:
    a,b,c,d = c,d,a,b
  if b<c:
    return [-1, -1]
  return [c, min(b,d)]

Mlist_T = [[1, 1] for i in range(N)]
Mlist_A = [[1, 1] for i in range(N)]
Mlist_T[0][0]=T[0]
Mlist_T[0][1]=T[0]
Mlist_A[N-1][0]=A[N-1]
Mlist_A[N-1][1]=A[N-1]
for i in range(1,N):
  if T[i-1] != T[i]:
    Mlist_T[i][0] = T[i]
  Mlist_T[i][1] = T[i]
for i in range(1,N):
  if A[N-1-(i-1)] != A[N-1-i]:
    Mlist_A[N-1-i][0] = A[N-1-i]
  Mlist_A[N-1-i][1] = A[N-1-i]
fl = True
M = []
for i in range(N):
  m = f(Mlist_T[i],Mlist_A[i])
  if m == [-1, -1]:
    fl = False
    print(0)
    break
  M.append(m)

if fl:

  r = 1000000007
  ss = 1

  for m in M:
    ss = (ss * (m[1]-m[0]+1))%r
  print(ss)

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

ct,ca=0,0
for i in range(N):
    if T[i] > ct:
        ct=T[i]
        Tr[i]=[ct,ct]
    else:
        Tr[i]=[1,ct]

    if A[N-1-i] > ca:
        ca=A[N-1-i]
        Ar[N-1-i]=[ca,ca]
    else:
        Ar[N-1-i]=[1,ca]

MOD=1000000007
ans=1
for tr,ar in zip(Tr,Ar):
    l = max(tr[0],ar[0])
    r = min(tr[1],ar[1])
    if r<l:
        print(0)
        exit()
    ans = (ans*(r-l+1))%MOD
print(ans)

Solution (Python):
mod = 10 ** 9 + 7

N = int(input())
T = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]

HT = [False] * N
for i in range(1, N):
    if T[i - 1] < T[i]:
        HT[i] = True
HT[0] = True

HA = [False] * N
for i in range(N - 1):
    if A[i] > A[i + 1]:
        HA[i] = True
HA[-1] = True

ans = 1
for ht, ha, t, a in zip(HT, HA, T, A):
    if ht and ha:
        ans *= 1 if t == a else 0
    elif ht:
        ans *= 1 if t <= a else 0
    elif ha:
        ans *= 1 if t >= a else 0
    else:
        ans = (ans * min(t, a)) % mod
print(ans)

Solution (Python):
mod = 10 ** 9 + 7

N = int(input())
T = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]

HT = [False] * N
for i in range(1, N):
    if T[i - 1] < T[i]:
        HT[i] = True
HT[0] = True

HA = [False] * N
for i in range(N - 1):
    if A[i] > A[i + 1]:
        HA[i] = True
HA[-1] = True

ans = 1
for i in range(N):
    if HT[i] and HA[i]:
        ans *= 1 if T[i] == A[i] else 0
    elif HT[i]:
        ans *= 1 if T[i] <= A[i] else 0
    elif HA[i]:
        ans *= 1 if T[i] >= A[i] else 0
    else:
        ans = (ans * min(T[i], A[i])) % mod
    if ans == 0:
        break
print(ans)

Solution (Python):
mod = 10 ** 9 + 7

N = int(input())
T = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]

HT = [False] * N
for i in range(1, N):
    if T[i - 1] < T[i]:
        HT[i] = True
HT[0] = True

HA = [False] * N
for i in range(N - 1):
    if A[i] > A[i + 1]:
        HA[i] = True
HA[-1] = True

ans = 1
for i in range(N):
    if HT[i] and HA[i]:
        ans *= 1 if T[i] == A[i] else 0
    elif HT[i]:
        ans *= 1 if T[i] <= A[i] else 0
    elif HA[i]:
        ans *= 1 if T[i] >= A[i] else 0
    else:
        ans = (ans * min(T[i], A[i])) % mod
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

import numpy as np

MOD = 10**9 + 7

N = int(input())
T = np.array(input().split(),dtype=np.int64)
A = np.array(input().split(),dtype=np.int64)

Tlower = T.copy()
Tlower[1:][T[1:]<=T[:-1]] = 1

A = A[::-1]
Alower = A.copy()
Alower[1:][A[1:]<=A[:-1]] = 1
A = A[::-1]; Alower = Alower[::-1]

lower = np.maximum(Alower,Tlower)
upper = np.minimum(A,T)
x = np.maximum(0,upper - lower + 1)

L = len(x); Lsq = int(L**.5+1)
x = np.resize(x,Lsq**2); x[L:] = 1; x = x.reshape((Lsq,Lsq))
for n in range(1,Lsq):
    x[:,n] *= x[:,n-1]; x[:,n] %= MOD
for n in range(1,Lsq):
    x[n] *= x[n-1,-1]; x[n] %= MOD
answer = x[-1,-1]
print(answer)

Solution (Python):
MOD=10**9+7
n=int(input())
la=list(map(int,input().split()))
lb=list(map(int,input().split()))
l=[[1,10**9] for i in range(n)]
tmp=0
for num,i in enumerate(la):
    if i>tmp:
        l[num][0]=i
        l[num][1]=i
    else :
        l[num][1]=i
    tmp=max(tmp,i)
tmp=0
ans=1
for i in range(n-1,-1,-1):
    if lb[i]<l[i][0]:
        print(0);exit()
    elif lb[i]>tmp:
        if lb[i]<=l[i][1]:ans*=1 
        else:print(0);exit()
    else:
        ans=ans*(min(lb[i],l[i][1])-l[i][0]+1)%MOD
    tmp=max(tmp,lb[i])
print(ans)

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

Solution (Python):

def main():
    """
      /----\     <- x = y
     /      \--\
    /           \
      xi  yi
    0 ~ xi / xi ~ yi / yi+1 ~ N-1
    """
    N = int(input())
    T = tuple(map(int, input().split()))
    A = tuple(map(int, input().split()))
    x, y = T[-1], A[0]  
    xi, yi = T.index(x), N - A[::-1].index(y) - 1  
    if x != y or xi > yi:  
        print(0)
        return
    MOD = 10 ** 9 + 7
    ans, cur = 1, 0
    for i in T[: xi + 1]:
        if i > cur:  
            cur = i
        else:
            ans = (ans * cur) % MOD  
    for _ in range(xi + 1, yi):
        ans = (ans * cur) % MOD
    for i in A[yi + 1 :]:
        if i < cur:
            cur = i
        else:
            ans = (ans * cur) % MOD
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

from bisect import bisect_left as bs


def main():
    """
      /----\     <- xi (= yi)
     /      \--\
    /           \
      xi  yi
    0 ~ xi / xi ~ yi / yi+1 ~ N-1
    """
    N = int(input())
    T = tuple(map(int, input().split()))
    A = tuple(map(int, input().split()))
    x, y = T[-1], A[0]  
    xi, yi = T.index(x), N - A[::-1].index(y) - 1  
    if x != y or xi > yi:
        print(0)
        return
    MOD = 10 ** 9 + 7
    ans, cur = 1, 0
    for i in T[: xi + 1]:
        if i > cur:  
            cur = i
        else:
            ans = (ans * cur) % MOD  
    for _ in range(xi + 1, yi):
        ans = (ans * cur) % MOD
    for i in A[yi + 1 :]:
        if i < cur:
            cur = i
        else:
            ans = (ans * cur) % MOD
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

from bisect import bisect_left as bs


def main():
    """
      /----\     <- xi (= yi)
     /      \--\
    /           \
      xi  yi
    0 ~ xi / xi ~ yi / yi+1 ~ N-1
    """
    N = int(input())
    T = tuple(map(int, input().split()))
    A = tuple(map(int, input().split()))
    x, y, xi, yi = 0, 0, 0, 0  
    for i, t in enumerate(T):
        if t > x:
            x, xi = t, i
    for i, a in enumerate(A):
        if a >= y:
            y, yi = a, i
    if x != y or xi > yi:
        print(0)
        return
    MOD = 10 ** 9 + 7
    ans, cur = 1, 0
    for i in T[: xi + 1]:
        if i > cur:  
            cur = i
        else:
            ans = (ans * cur) % MOD  
    for _ in range(xi + 1, yi):
        ans = (ans * cur) % MOD
    for i in A[yi + 1 :]:
        if i < cur:
            cur = i
        else:
            ans = (ans * cur) % MOD
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
T = [0]+list(map(int,input().split()))+[float('inf')]
A = [float('inf')]+list(map(int,input().split()))+[0]
ans = 1
MOD = 10**9+7
for i in range(1,N+1):
    if T[i-1] < T[i]:
        ans *= int(T[i] <= A[i])
    if A[i] > A[i+1]:
        ans *= int(T[i] >= A[i])
    if T[i-1] == T[i] and A[i+1] == A[i]:
        ans *= min(T[i],A[i])
        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

def restor_accumulation_max(accumulation_max: list, include_0: bool) -> list:
    '''累積Maxを復元する
    Maxには逆元が存在しないので、一意に復元することはできない
    復元値の候補を[begin, end)として定義される
 
    例: accumulation_max = [3, 4, 4, 8, 8]
        include_0 = Trueのとき
        -> [(3, 4), (4, 5), (0, 5), (8, 9), (0, 9)]
        include_0 = Falseのとき
        -> [(3, 4), (4, 5), (0, 5), (8, 9), (1, 9)]
    '''
    n = len(accumulation_max)
    res = []
    init = 0 if include_0 else 1
    max_ = init
    
    for i in range(n):
        if accumulation_max[i] > max_:
            res.append((accumulation_max[i], accumulation_max[i] + 1))
            max_ = accumulation_max[i]
        else:
            res.append((init, max_ + 1))
    
    return res

li_t = restor_accumulation_max(t, False)
li_a = restor_accumulation_max(a[::-1], False)[::-1]

ans = 1
for i in range(n):
    tmp = min(li_t[i][1], li_a[i][1]) - max(li_t[i][0], li_a[i][0])
    if tmp <= 0:
        ans *= 0
    ans *= tmp
    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

li_t = []
max_t = 0
for i in range(n):
    if t[i] > max_t:
        li_t.append((t[i], t[i] + 1))
        max_t = t[i]
    else:
        li_t.append((1, max_t + 1))

li_a = []
max_a = 0
for i in range(n)[::-1]:
    if a[i] > max_a:
        li_a.append((a[i], a[i] + 1))
        max_a = a[i]
    else:
        li_a.append((1, max_a + 1))
li_a = li_a[::-1]

ans = 1
for i in range(n):
    tmp = min(li_t[i][1], li_a[i][1]) - max(li_t[i][0], li_a[i][0])
    if tmp <= 0:
        ans *= 0
    ans *= tmp
    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 N == 1:
    if T[0] == A[0]:
        print(1)
    else:
        print(0)
    exit()

ans = 1
for i in range(1, N - 1):
    if T[i] == T[i - 1] and A[i] == A[i + 1]:
        ans *= min(T[i], A[i])
        ans %= MOD
    elif T[i] > T[i - 1] and A[i] == A[i + 1] and A[i] < T[i]:
        print(0)
        exit()
    elif T[i] == T[i - 1] and A[i] > A[i + 1] and A[i] > T[i]:
        print(0)
        exit()
    elif T[i] > T[i - 1] and A[i] > A[i + 1] and A[i] != T[i]:
        print(0)
        exit()
print(ans)

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

Solution (Python):
import sys
import heapq
from operator import itemgetter
from collections import deque, defaultdict
from bisect import bisect_left, bisect_right
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
MOD = 10**9 + 7

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

    if T[-1] != A[0]:
        print(0)
        return

    minHeight = [float('inf')] * N
    minHeight[0] = -1
    minHeight[N - 1] = -1

    for i in range(1, N - 1):
        if T[i] > T[i - 1]:
            if T[i] > A[i]:
                print(0)
                return
            minHeight[i] = -1
        if A[i] > A[i + 1]:
            if A[i] > T[i]:
                print(0)
                return
            minHeight[i] = -1
        minHeight[i] = min(minHeight[i], A[i], T[i])

    ans = 1
    for h in minHeight:
        if h == -1:
            continue
        ans = (ans * h) % MOD

    print(ans)

sol()

Solution (Python):
n=int(input())
a=[0]+list(map(int,input().split()))
b=list(map(int,input().split()))+[0]

m=[a[i] for i in range(1,n+1)]
l=[1 for _ in range(n)]

for i in range(n):
	if a[i]<a[i+1]:
		l[i]=a[i+1]
		
ans=1
for i in range(n-1,-1,-1):
	if b[i+1]<b[i]:
		if l[i]>b[i]:
			ans=0
		else:
			l[i]=b[i]
	m[i]=min(m[i],b[i])
	
for i in range(n):
	ans=(ans*(m[i]-l[i]+1))%(10**9+7)
	if m[i]<l[i]:
		ans=0
print(ans)

Solution (Python):
N=int(input())
T,A=[list(map(int,input().split())) for i in range(2)]
tp,ap=0,A[0]
f=0
r=1
mod=10**9+7
x=0
if T[-1]!=A[0]:
    f=-1
for t,a in zip(T,A):
    if f==-1:
        break
    if f==0:
        if t==tp and a==ap:
            r=(r*t)%mod
        elif t==a==ap:
            f=1
        elif not (t>tp and a==ap):
            f=-1
    elif f==1:
        if t==a==ap:
            x+=1
        elif t==tp and a<ap:
            for j in range(x-1):
                r=(r*t)%mod
            f=2
        else:
            f=-1
    elif f==2:
        if t==tp and a==ap:
            r=(r*a)%mod
        elif not (t==tp and a<ap):
            f=-1
    tp,ap=t,a
if f==1:
    for j in range(x-1):
        r=(r*t)%mod
print(0 if f<1 else r)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n=I()

  a=LI()
  b=LI()

  if n==1 and a[0]!=b[0]:
    return 0

  l1=[]
  l2=[]

  amx=a[0]
  l1.append(1)
  for i in range(1,len(a)):
    x=a[i]
    if amx<x:
      
      if x>b[i]:
        print(0)
        exit()

      l1.append(1)
      amx=x
    elif amx==x:
      l1.append(amx)
    else:
      print(0)
      exit()

  b.reverse()
  l1.reverse()

  bmx=b[0]
  l2.append(1)
  for i in range(1,len(b)):
    x=b[i]
    if bmx<x:
      
      if x>a[n-i-1]:
        print(0)
        exit()

      l2.append(1)
      bmx=x
    elif bmx==x:
      l2.append(min(bmx,l1[i]))
    else:
      print(0)
      exit()

  ans=1
  for x in l2:
    ans*=x
    ans%=mod

  return ans

print(main())

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline())[:-1]
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)]

sys.setrecursionlimit(1000000)
mod = 1000000007


def A():
    n = I()

    return


def B():
    n = I()

    return


def C():
    n = I()
    t = LI()
    a = LI()
    f = [1]*n
    h = [float("inf")]*n
    ma = 0
    for i in range(n):
        if t[i] > ma:
            ma = t[i]
            f[i] = 0
        h[i] = ma
    ma = 0
    for i in range(n)[::-1]:
        if a[i] > ma:
            ma = a[i]
            if not f[i] and h[i] != ma:
                print(0)
                return
            f[i] = 0
            if h[i] < ma:
                print(0)
                return
        if ma < h[i]:
            h[i] = ma
    ans = 1
    for i in range(n):
        if f[i]:
            ans *= h[i]
            ans %= mod
    print(ans)
    return


def D():
    n = I()

    return


def E():
    n = I()

    return


def F():
    n = I()

    return


def G():
    n = I()

    return


def H():
    n = I()

    return


def I_():
    n = I()

    return


def J():
    n = I()

    return


if __name__ == "__main__":
    C()

Solution (Python):
N=int(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,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)

Solution (Python):
N=int(input())
*T, = map(int,input().split())
*A, = 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)