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):
mod = 10**9+7
import sys

N = int(input())

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

able = [0] * N
dif = [False] * N

for i in range(N):

    able[i] = min(T[i],A[i])

for i in range(N):

    if i == 0 or T[i] != T[i-1]:
        dif[i] = True

        if A[i] < T[i]:
            print (0)
            sys.exit()

for i in range(N):
    if i == N-1 or A[i] != A[i+1]:

        if dif[i]:
            if T[i] != A[i]:
                print (0)
                sys.exit()

        else:
            if A[i] > T[i]:
                print (0)
                sys.exit()
        
        dif[i] = True

ans = 1
for i in range(N):

    if dif[i]:
        continue

    ans *= able[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
ans = 1
if n == 1 and t != a:
    print(0)
    exit()
for i in range(1,n-1):
    if t[i-1] == t[i] and a[i] == a[i+1]:
        ans = (ans*min(t[i],a[i]))%mod
    elif t[i-1] < t[i] and t[i] > a[i]:
        print(0)
        exit()
    elif a[i] > a[i+1] and t[i] < a[i]:
        print(0)
        exit()
print(ans)

Solution (Python):

import sys

N=input()
T=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())

if max(T)!=max(A):
    print 0
    quit()

L=[ None for _ in range(N) ]



for i,t in enumerate(T):
    if i==0:
        L[i]=t
    else:
        if prev_t<t:
            L[i]=t
        elif prev_t>t:
            print 0
            quit()
    prev_t=t



for i,a in enumerate(A[::-1]):
    i=N-i-1 
    if i==N-1:
        L[i]=a
    else:
        if prev_a<a:
            if L[i] is not None:    
                if L[i]!=a: 
                    print 0
                    quit()
            L[i]=a  
        elif prev_a>a:
            print 0
            quit()
    prev_a=a


max_x=0
for x,t in zip(L,T):
    
    if x is not None:
        max_x=max(max_x,x)
        if max_x!=t:
            print 0
            quit()


max_x=0
for x,a in zip(L[::-1],A[::-1]):
    if x is not None:
        max_x=max(max_x,x)
        if max_x!=a:
            print 0
            quit()


ans=1
mod=10**9+7
for x,a,t in zip(L,T,A):
    if x is None:   
        ans*=min(a,t)
        ans%=mod
print ans

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

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

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

A_d = [1] * N
A_d[-1] = A[-1]
for i in range(N - 2, -1, -1):
    if A[i] > A[i + 1]:
        A_d[i] = A[i]







ans = 1
for i in range(N):
    tmp = max(0, min(A[i], T[i]) - max(A_d[i], T_d[i]) + 1)
    ans *= tmp
    ans %= MOD

print (ans)

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

MA = [0] * N
MI = [1] * N
P = 10**9+7
for i in range(N):
    if A[i] > A[i-1]:
        MA[i] = MI[i] = A[i]
    else:
        MA[i] = A[i]

for i in range(N)[::-1]:
    if B[i] > B[i+1]:
        if MI[i] <= B[i] <= MA[i]:
            MA[i] = MI[i] = B[i]
        else:
            print(0)
            exit()
    else:
        if B[i] >= MI[i]:
            MA[i] = min(MA[i], B[i])
        else:
            print(0)
            exit()
ans = 1
for i in range(N):
    ans = ans * (MA[i] - MI[i] + 1) % P
print(ans)

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10


def solve():
    n = II()
    t = LI()
    a = LI()[::-1]
    dt = [None] * n
    da = [None] * n
    ba = 0
    bt = 0
    for i in range(n):
        if bt < t[i]:
            dt[i] = (1, t[i])
            bt = t[i]
        else:
            dt[i] = (bt, bt)
    for i in range(n):
        if ba < a[i]:
            da[i] = (1, a[i])
            ba = a[i]
        else:
            da[i] = (ba, ba)
    da = da[::-1]
    ans = 1
    for i in range(n):
        f = True
        if da[i][0] == 1:
            if da[i][1] > dt[i][1]:
                print(0)
                return
            f &= 0
        if dt[i][0] == 1:
            if da[i][1] < dt[i][1]:
                print(0)
                return
            f &= 0
        if f:
            ans *= min(da[i][1], dt[i][1])
            ans %= mod
    print(ans)
    return



if __name__ == '__main__':
    solve()

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    A=list(map(int,input().split()))
    B=list(map(int,input().split()))

    
    C=[None]*n
    C[0]=A[0]
    for i in range(1,n):
        if(A[i]!=A[i-1]):
            C[i]=A[i]

    for i in range(n):
        if(C[i] and B[i]<C[i]):
            print(0)
            return

    D=[None]*n
    D[-1]=B[-1]
    for i in range(n-2,-1,-1):
        if(B[i]!=B[i+1]):
            D[i]=B[i]

    for i in range(n):
        if(D[i] and A[i]<D[i]):
            print(0)
            return

    ans=1
    for i in range(n):
        if(C[i] or D[i]): continue
        ans*=min(A[i],B[i])
        ans%=MOD

    print(ans)
resolve()

Solution (Python):
n = int(input())
t = list(map(int, input().split()))
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
renew = [0] * n
b = 0
for i, h in enumerate(t):
	if b < h:
		if h > a[i]:
			print(0)
			exit()
		renew[i] = 1
		b = h
b = 0
for i, h in enumerate(a[::-1]):
	if b < h:
		if h > t[n - i - 1]:
			print(0)
			exit()
		renew[n - i - 1] = 1
		b = h
ans = 1
for f, th, ah in zip(renew, t, a):
	if not f:
		ans *= min(th, ah)
		ans %= mod
print(ans)

Solution (Python):
p = 10**9+7
N = int(input())
T = list(map(int,input().split()))
A = list(map(int,input().split()))
H = [0 for _ in range(N)]
cnt = 1
H[0] = T[0]
for i in range(1,N):
    if T[i]>T[i-1]:
        H[i] = T[i]
if H[-1]>0 and H[-1]!=A[-1]:
    cnt = 0
else:
    H[-1] = A[-1]
    for i in range(-2,-N-1,-1):
        if H[i]==0 and A[i]>A[i+1]:
            if A[i]<=T[i]:
                H[i] = A[i]
            else:
                cnt = 0
                break
        elif H[i]>0 and A[i]>A[i+1]:
            if H[i]==A[i]:continue
            else:
                cnt = 0
                break
    if cnt==1:
        for i in range(1,N-1):
            if H[i]==0:
                cnt = (cnt*min(T[i],A[i]))%p
print(cnt)

Solution (Python):
MOD = 1000000007

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

H0 = [None for _ in range(N)]
H1 = [None for _ in range(N)]

incorrect = False

maxh = 0
for i in range(N):
    if maxh < T[i]:
        H0[i] = T[i]
        maxh = T[i]

maxh = 0
for i in reversed(range(N)):
    if H0[i] != None and H0[i] > A[i]:
        incorrect = True
    if maxh < A[i]:
        H1[i] = A[i]
        maxh = A[i]

ans = 1
for i in range(N):
    if H0[i]==None and H1[i]==None:
        ans *= min(A[i],T[i])
        ans %= MOD

print(ans if not incorrect else 0)

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

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

H = [0] * N
for i in range(N):
    if i == 0 or T[i - 1] < T[i]:
        H[i] = T[i]

for i in range(N - 1, -1, -1):
    if i == N - 1 or A[i] > A[i + 1]:
        H[i] = A[i]
    
ans = 1
for i in range(N):
    if H[i] == 0:
        H[i] = min(T[i], A[i])
        ans *= min(T[i], A[i])
        ans %= mod

flag = True
M = 0
for i in range(N):
    M = max(M, H[i])
    flag &= (M == T[i])
M = 0
for i in range(N - 1, -1, -1):
    M = max(M, H[i])
    flag &= (M == A[i])

if flag is True:
    print(ans)
else:
    print(0)

Solution (Python):
def examA():
    S = SI()
    ans = "No"
    flag = False
    for s in S:
        if flag:
            if s=="F":
                ans = "Yes"
                break
        else:
            if s=="C":
                flag = True
    print(ans)
    return

def examB():
    K, T = LI()
    A = LI(); A.sort()
    ans = max(0,A[-1]*2-sum(A)-1)
    print(ans)
    return

def examC(mod):
    N = I()
    T = LI(); A = LI()
    H = [0]*N
    ans = 1
    if T[-1] != A[0]:
        ans = 0
    H[0] = T[0]; H[N-1] = A[N-1]
    for i in range(N-1):
        if T[i+1]>T[i]:
            if H[i+1]==0:
                H[i+1] = T[i+1]
            elif H[i+1]!=T[i+1]:
                ans = 0
        else:
            if H[i+1]==0:
                H[i+1] = -T[i+1]
    for i in range(N-1,0,-1):
        if A[i-1]>A[i]:
            if H[i-1]==0:
                H[i-1] = A[i-1]
            elif H[i-1]<0:
                if -H[i-1]<A[i-1]:
                    ans = 0
                else:
                    H[i-1] = A[i-1]
            elif H[i-1]!=A[i-1]:
                ans = 0

    flag = False; cur = 0
    for i in H:
        if flag:
            if i<0:
                cur +=1
            else:
                ans *=(min(now,i)**cur)%mod
                ans %=mod
                flag = False
                cur = 0; now = i
        else:
            if i<0:
                cur = 1
                flag = True
            else:
                now = i
    print(ans%mod)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examC(mod)

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()
x=[1]*n
x[0]=x[-1]=0
for i in range(1,n):
  if t[i-1]!=t[i]:
    x[i]=0
    if t[i]>a[i]:print(0);exit()
for i in range(n-2,-1,-1):
  if a[i+1]!=a[i]:
    x[i]=0
    if t[i]<a[i]:print(0);exit()
ans=1
for i in range(n):
  if x[i]:
    ans*=min(t[i],a[i])
    ans%=10**9+7
print(ans)

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

ua = [0 for i in range(n)]
fa = [False for i in range(n)]
ub = [0 for i in range(n)]
fb = [False for i in range(n)]


bm = 0
for i in range(n):
    if a[i] > bm:
        ua[i] = a[i]
        fa[i] = True
        bm = a[i]
    else:
        ua[i] = bm

bm = 0
for i in range(n):
    if b[i] > bm:
        ub[i] = b[i]
        fb[i] = True
        bm = b[i]
    else:
        ub[i] = bm
ub = ub[::-1]
fb = fb[::-1]
ans = 1
m = 10**9+7

for i in range(n):
    if fa[i] and fb[i]:
        if not ua[i] == ub[i]:
            ans = 0
            break
    elif fa[i] or fb[i]:
        if ua[i] > ub[i]:
            if not fb[i]:
                ans = 0
                break
        elif ua[i] == ub[i]:
            ans = ans
        else:
            if not fa[i]:
                ans = 0
                break
    else:
        ans *= min(ua[i],ub[i])
        ans %= m
    

print(ans)

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

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

max_=T[-1]
MAX_T=[max_ for _ in range(N)]
MIN_T=[1 for _ in range(N)]
MAX_A=[max_ for _ in range(N)]
MIN_A=[1 for _ in range(N)]

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



        
MAX_A[-1] = A[-1]
MIN_A[-1] = A[-1]
for i in range(0,N-1)[::-1]:
    MAX_A[i] = min(MAX_A[i],A[i])
    if A[i] > A[i+1]:
        MIN_A[i] = min(MAX_A[i],A[i])
    else:
        MIN_A[i] = max(1,MIN_A[i])
        



sum_= 1
lim = 10**9 + 7
for i in range(N):
    if (MAX_T[i] < MIN_A[i]) or (MAX_A[i] < MIN_T[i]):
        print(0)
        sys.exit()
    else:
        sum_ = (sum_ * (min(MAX_T[i],MAX_A[i]) - max(MIN_T[i],MIN_A[i]) +1 )) % lim
print(sum_)

Solution (Python):
N = int(input())
T = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
mod = 10 ** 9 + 7

def solve() :
    if T[-1] != A[0] :
        return 0
        
    for i in range(N) :
        if T[i] == T[-1] :
            break
    for j in range(N - 1, -1, -1) :
        if A[j] == A[0] :
            break
            
    if i > j :
        return 0
        
    ok = [False] * N
    ok[0] = ok[-1] = True
    for i in range(1, N) :
        if T[i] != T[i-1] :
            ok[i] = True
        if A[N-1-i] != A[N-i] :
            ok[N-1-i] = True

    ret = 1
    for i in range(N) :
        if ok[i] : continue
        ret = ret * min(T[i], A[i]) % mod   
    
    return ret
    
print(solve())

Solution (Python):
n=int(input())
tM=list(map(int,input().split()))
aM=list(map(int,input().split()))
tm=[1]*n
am=[1]*n

tm[0]=tM[0]
for i in range(1,n):
    if tM[i]>tM[i-1]:
        tm[i]=tM[i]

aM.reverse()
am[0]=aM[0]
for i in range(1,n):
    if aM[i]>aM[i-1]:
        am[i]=aM[i]
aM.reverse()
am.reverse()

m=[]
M=[]
for i in range(n):
    m.append(max(tm[i],am[i]))
    M.append(min(tM[i],aM[i]))

ans=1
mod=10**9+7
for i in range(n):
    ans*=max(0,M[i]-m[i]+1)
    ans%=mod

print(ans)

Solution (Python):
n=int(input())
tM=list(map(int,input().split()))
aM=list(map(int,input().split()))
tm=[1]*n
am=[1]*n

tm[0]=tM[0]
for i in range(1,n):
    if tM[i]>tM[i-1]:
        tm[i]=tM[i]

aM.reverse()
am[0]=aM[0]
for i in range(1,n):
    if aM[i]>aM[i-1]:
        am[i]=aM[i]
aM.reverse()
am.reverse()

m=[]
M=[]
for i in range(n):
    m.append(max(tm[i],am[i]))
    M.append(min(tM[i],aM[i]))

ans=1
mod=10**9+7
for i in range(n):
    ans*=max(0,M[i]-m[i]+1)
    ans%=mod

print(ans)

Solution (Python):

n= int(input())
t = [0]+list(map(int, input().split( )))
tmx = max(t)
a = [tmx]+list(map(int, input().split( )))+[0]

if a[0]!= a[1]:
    print(0)
    exit()
ans = 1

for i in range(1,n+1):
    if t[i]>t[i-1]:
        if t[i]>a[i]:
            print(0)
            exit()
        continue
    if a[i]>a[i+1]:
        if a[i]>t[i]:
            print(0)
            exit()
        continue
    if t[i]>t[i-1] and a[i]>a[i+1]:
        if t[i]!=a[i]:
            print(0)
            exit()
        continue

    else:
        ans *= min(t[i],a[i])
        ans %=10**9+7
print(ans)

Solution (Python):
MOD = 10**9 + 7
N = int(input())
*T, = map(int, input().split())
*A, = map(int, input().split())
ans = 1
for i in range(N):
    temp1 = -1
    temp2 = -1
    if i == 0 or T[i-1] < T[i]:
        temp1 = T[i]
    if i == N-1 or A[i] > A[i+1]:
        temp2 = A[i]

    if temp1 == -1 and temp2 == -1:
        ans = (ans * min(T[i], A[i])) % MOD
    elif temp1 == -1 and temp2 != -1:
        if temp2 > T[i]: ans = 0; break
    elif temp1 != -1 and temp2 == -1:
        if temp2 > A[i]: ans = 0; break
    else:
        if temp1 != temp2: ans = 0; break
print(ans)

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

    ans = [0] * N
    H = [0] * N
    tmp = 0
    for i in range(N):
        if tmp < T[i]:
            tmp = T[i]
            ans[i] = 1
            H[i] = T[i]
        else:
            ans[i] = T[i]
            H[i] = T[i]

    tmp = 0
    for i in range(N-1,-1,-1):
        if tmp < A[i]:
            tmp = A[i]
            ans[i] = min(ans[i],1)
            H[i] = min(H[i],A[i])
        else:
            ans[i] = min(A[i],ans[i])
            H[i] = min(H[i],A[i])

    tmp = 0
    for i in range(N):
        tmp = max(tmp, H[i])
        if tmp != T[i]:
            print(0)
            exit()

    tmp = 0
    for i in range(N-1,-1,-1):
        tmp = max(tmp, H[i])
        if tmp != A[i]:
            print(0)
            exit()
    

    cnt = 1
    for i in ans:
        cnt *= i
        cnt %= MOD
    
    print(cnt)

if __name__ == "__main__":
    main()

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


class Mod:
    def __init__(self, m):
        self.m = m

    def add(self, a, b):
        return (a + b) % self.m

    def sub(self, a, b):
        return (a - b) % self.m

    def mul(self, a, b):
        return ((a % self.m) * (b % self.m)) % self.m

    def div(self, a, b):
        return self.mul(a, pow(b, self.m-2, self.m))

    def pow(self, a, b):
        return pow(a, b, self.m)

@mt
def slv(N, T, A):

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

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

    fa = [False] * N
    fa[-1] = True
    for i in range(N-2, 0, -1):
        if A[i] > A[i+1]:
            fa[i] = True
    
    H = []
    for i in range(N):
        if ft[i] and fa[i]:
            if T[i] != A[i]:
                return 0
        elif ft[i]:
            if T[i] > A[i]:
                return 0
        elif fa[i]:
            if T[i] < A[i]:
                return 0
        else:
            H.append(min(T[i], A[i]))

    M = Mod(10**9+7)
    ans = 1
    for h in H:
        ans = M.mul(ans, h)
    return ans


def main():
    N = read_int()
    T = read_int_n()
    A = read_int_n()
    print(slv(N, T, A))


if __name__ == '__main__':
    main()

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

Min = [1] * N
Max = [float("inf")] * N
tp = 0
for i, t in enumerate(T):
    if t != tp:
        Min[i] = max(Min[i], t)
    Max[i] = min(Max[i], t)
    tp = t
ap = 0
for i, a in zip(range(N-1, -1, -1), A[::-1]):
    if a != ap:
        Min[i] = max(Min[i], a)
    Max[i] = min(Max[i], a)
    ap = a
ans = 1
for mi, ma in zip(Min, Max):
    ans = ans * max(ma-mi+1, 0) % mod
print(ans)

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

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

ans = 1
for i in range(1, n - 1):
    x = t[i] if t[i] > t[i - 1] else 1
    y = a[i] if a[i] > a[i + 1] else 1
    ans = ans * max(0, min(t[i], a[i]) - max(x, y) + 1) % mod

print(ans if a[0] == t[-1] else 0)

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

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(0)
    else:
        print(1)
    exit()

max_list = [10**9+1] * n
min_list = [0] * n

max_list[0] = t[0]
min_list[0] = t[0]
for i in range(1, n):
    if t[i] > t[i-1]:
        max_list[i] = t[i]
        min_list[i] = t[i]
    else:
        max_list[i] = t[i]
        min_list[i] = 1

max_list[n-1] = a[n-1]
min_list[n-1] = a[n-1]
for i in range(n-2, -1, -1):
    if a[i] > a[i+1]:
        if min_list[i] <= a[i] <= max_list[i]:
            max_list[i] = a[i]
            min_list[i] = a[i]
        else:
            print(0)
            exit()
    else:
        if max_list[i] == min_list[i]:
            if max_list[i] > a[i]:
                print(0)
                exit()
        else:
            max_list[i] = min(max_list[i], a[i])

ans = 1
for i in range(n):
    ans = (ans * (max_list[i] - min_list[i] + 1)) % mod
print(ans)

Solution (Python):
import sys
input = sys.stdin.buffer.readline
N = int(input())
t = list(map(int, input().split()))
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
free = [0] * N
f = 0
if N == 1:
  if t[0] == a[0]:
    print(1)
  else:
    print(0)
  exit(0)
if max(a) != max(t):
  print(0)
  exit(0)
for i in range(1, N - 1):
  if t[i - 1] == t[i]:
    free[i] = t[i]
  if t[i - 1] != t[i]:
    if t[i] > a[i]:
      f = 1
      break
  if a[i] != a[i + 1]:
    if a[i] > t[i]:
      f = 1
      break
for i in range(N - 2, 0, -1):
  if a[i + 1] == a[i] and t[i - 1] == t[i]:
    free[i] = min(t[i], a[i])
  else:
    free[i] = 0
res = 1
if f:
  print(0)
  exit(0)
for i in range(N):
  res *= max(1, free[i])
  res %= mod
print(res)

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

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

decided = [False for _ in range(N)]
H = [0 for _ in range(N)]

pre = -1
for i, t in enumerate(T):
    if t != pre:
        decided[i] = True
    H[i] = t
    pre = t

ok = True
pre = -1
for i in reversed(range(N)):
    a = A[i]
    if a != pre:
        if decided[i] and H[i] != a:
            ok = False
            break
        decided[i] = True
    elif decided[i]:
        if H[i] > a:
            ok = False
            break
    if not decided[i]:
        H[i] = min(H[i], a)
    pre = a

if ok:
    ans = 1
    for i in range(N):
        if not decided[i]:
            ans = ans * H[i] % mod
    print(ans)
else:
    print(0)

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

def main():
    N = int(input())
    T = list(map(int, input().split()))
    A = list(map(int, input().split()))
    if T[0] <= A[0] and A[N-1] <= T[N-1]:
        ans = 1
    else:
        return 0
    MOD = int(1e9) + 7
    for i in range(1, N-1):
        t = (T[i] == T[i-1])
        a = (A[i] == A[i+1])
        if not(t) and not(a):
           if T[i] != A[i]:
               return 0
        elif not(t) and a:
            if A[i] < T[i]:
                return 0
        elif t and not(a):
            if T[i] < A[i]:
                return 0
        else:
            ans *= min(T[i], A[i])
            ans %= MOD
    
    return ans

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

Solution (Python):

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


    maxh = t[:]
    minflag = [0 for _ in range(n)]

    if t[-1] != a[0]:
        return 0
    else:
        maxflag = t[-1]
    maxh[0] = t[0]
    minflag[0] = 1

    for i in range(1,n):
        if t[i] > t[i-1]:
            minflag[i] = 1

    maxh[-1] = a[-1]
    minflag[-1] = 1
    if a[-1] != t[-1]:
        for i in range(n-2,-1,-1):
            if a[i] > t[i]:
                return 0
            elif a[i] == t[i]:
                if a[i] != maxflag:
                    return 0
                else:
                    minflag[i] = 1
                    break
            maxh[i] = a[i]
            if a[i] > a[i+1]:
                minflag[i] = 1

    res = 1
    for i in range(n):
        if not minflag[i]:
            res *= maxh[i]
            res %= (10**9+7)
    return res % (10**9+7)

print(main())

Solution (Python):
n = int(input())
t = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
result = 1
for i in range(n):
    h_max, h_min = min(t[i], a[i])+1, 1
    if i != 0:
        if t[i-1] < t[i]:
            h_min = t[i]
    else:
        h_min = t[0]

    if i != n-1:
        if a[i] > a[i+1]:
            h_min = max(a[i], h_min)
    else:
        h_min = max(a[i], h_min)
    result = result * max(h_max - h_min, 0) % (10**9+7)
print(result)

Solution (Python):
n = int(input())
t = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
result = 1
for i in range(n):
    h_max, h_min = min(t[i], a[i])+1, 1

    if i != 0:
        if t[i-1] < t[i]:
            h_min = t[i]
    else:
        h_min = t[0]

    if i != n-1:
        if a[i] > a[i+1]:
            h_min = a[i]
    else:
        h_min = max(a[n-1], h_min)


    result = result * max(h_max - h_min, 0) % (10**9+7)
print(result)

Solution (Python):
n = int(input())
t = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
result = 1
for i in range(n):
    h_max, h_min = min(t[i], a[i])+1, 1

    if i != n-1:
        if a[i] > a[i+1]:
            h_min = a[i]
    else:
        h_min = a[n-1] 

    if i != 0:
        if t[i-1] < t[i]:
            h_min = t[i]
    else:
        h_min = t[0]


    result = result * max(h_max - h_min, 0) % (10**9+7)
print(result)

Solution (Python):
n=int(input())
s=list(map(int,input().split()))
a=list(map(int,input().split()))
uselist=[0 for _ in range(n)]
cnt=0
for i in range(n):
    if cnt!=s[i]:
        cnt=s[i]
        uselist[i]=cnt
    else:
        continue


uselist1=[0 for _ in range(n)]
count=0
for i in range(n):
    if count!=a[n-1-i]:
        count=a[n-1-i]
        uselist1[n-1-i]=count
    else:
        continue


flag=True
anslist=[0 for _ in range(n)]
for i in range(n):
    if uselist[i]!=0 and uselist1[i]!=0:
        if uselist[i]!=uselist1[i]:
            flag=False
            break
        else:
            anslist[i]=uselist[i]
    elif uselist[i]!=0 and uselist1[i]==0:
        anslist[i]=uselist[i]
    elif uselist[i]==0 and uselist1[i]!=0:
        anslist[i]=uselist1[i]
    else:
        continue
for i in range(n):
    if anslist[n-1-i]>a[n-1-i]:
        flag=False
        break

        
        
ans=1
checker=0
index=0
i=1
if flag:
    anslist.append(0)
    anslist.insert(0,0)
    while i<=n:
        if index==0 and anslist[i]!=0:
            checker=anslist[i]
            i+=1
            
        elif index==0 and anslist[i]==0:
            index+=1
            i+=1
            
        elif index!=0 and anslist[i]!=0:
            checker=min(checker,anslist[i])
            use=1
            for _ in range(index):
                use=(use*checker)%(10**9+7)
            ans=(ans*(use))%(10**9+7)
            checker=anslist[i]
            index=0
            i+=1
        elif index!=0 and anslist[i]==0:
            index+=1
            i+=1
    print(ans%(10**9+7))
if not flag:
    print(0)

Solution (Python):
n = int(input())
tn = [0]+[int(i) for i in input().split()]+[0]
an = [0]+[int(i) for i in input().split()]+[0]
ans = 1
mod = 10**9+7

for i in range(1,n+1):
  if tn[i-1] < tn[i] and an[i] > an[i+1]:
    ans *= int(tn[i] == an[i])
  elif tn[i-1] < tn[i] and an[i] == an[i+1]:
    ans *= int(tn[i] <= an[i])
  elif tn[i-1] == tn[i] and an[i] > an[i+1]:
    ans *= int(tn[i] >= an[i])
  else:
    ans *= min(an[i],tn[i])
    ans %= mod
print(ans)

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

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

B=[]
C=[]
B.append([0,T[0],T[0]])
C.append([N-1,A[-1],A[-1]])

if N>1:
  for i in range(1,N):
    B.append([i,T[i],T[i] if T[i]>T[i-1] else 1])
  for i in range(N-2,-1,-1):
    C.append([i,A[i],A[i] if A[i]>A[i+1] else 1])
C.sort()



ans=1
for i in range(N):
  d=min(B[i][1],C[i][1])-max(B[i][2],C[i][2])+1
  ans*=d if d>=0 else 0
  ans%=mod
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
n=int(input())
t=list(map(int,input().split()))
a=list(map(int,input().split()))
t=[0]+t+[t[-1]]
a=[a[0]]+a+[0]
ans=1

for i in range(n):
    if t[i]<t[i+1]:
        if a[i+1]<t[i+1]:
            ans=0
            break
    if a[i]>a[i+1]:
        if t[i]<a[i]:
            ans=0
            break
    if t[-2]!=a[1]:
        ans=0
        break
    if t[i]==t[i+1] and a[i+1]==a[i+2]:
        ans=(ans*(min(t[i+1],a[i+1])))%(10**9+7)
print(ans)

Solution (Python):
import sys
n = int(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)

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)

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)


ans = 1
for i in range(n):
    if not fixed[i]:
        ans = ans * h[i] % (10**9+7)
print(ans)

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


x = [1 for _ in range(N)]
for i in range(1, N):
    if i == 0:
        x[i] = 1
    elif T[i - 1] < T[i]:
        x[i] = 1
    else:
        x[i] = T[i]

y = [1 for _ in range(N)]
for i in range(N - 2, -1, -1):
    if i == N - 1:
        y[i] == 1
    if A[i] > A[i + 1]:
        y[i] = 1
    else:
        y[i] = A[i]

ans = 1

for i in range(N):
    ans = ans * min(x[i], y[i]) % int(1e9 + 7)


for x,y,t,a in zip(x,y,T,A):
    if x == 1 and t > a:
        ans = 0
    if y == 1 and t < a:
        ans = 0


print(ans)