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())
T=[int(i) for i in input().split()]
A=[int(i) for i in input().split()]
H=[0 for i in range(N)]
P=10**9+7

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

Solution (Python):
n=int(input())
s=list(map(int, input().split()))
t=list(map(int, input().split()))
ans=1
if s[s.index(max(s))]!=t[s.index(max(s))]:ans=0
t=t[::-1]
a=[1]+[0]*(n-2)+[1]
for i in range(1,n-1):
    if s[i]<a[i-1]:
        ans=0
        break
    elif s[i]>s[i-1]:a[i]=1
    else:a[i]=s[i-1]
for i in range(1,n-1):
    if t[i]<t[i-1]:
        ans=0
        break
    elif t[i]>t[i-1]:a[n-i-1]=1
    else:a[n-i-1]=min(t[i-1],a[-1*(i+1)])
for i in a:
    ans=(ans*i)%(10**9+7)
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()

if n == 1:
    print(1)
    exit()

ans = 1
for i in range(1, n - 1):
    if t[i] != t[i - 1]:
        if a[i] < t[i]:
            print(0)
            exit()
    elif a[i] != a[i + 1]:
        if t[i] < a[i]:
            print(0)
            exit()
    else:
        ans *= min(a[i], t[i])
        ans %= MOD

print(ans)

Solution (Python):
read=lambda :list(map(int,input().split()))
n=int(input())
t,a=read(),read()
mod=10**9+7
t=[(i!=j,i) for i,j in zip(t,[0]+t)]
a=[(i!=j,i) for i,j in zip(a,(a+[0])[1:])]

ans=1
for p,q in zip(t,a):
  if(p[0]):
    if(q[0]):
      if(p[1]!=q[1]):
        print(0)
        exit()  
    elif(q[1]<p[1]):
      print(0)
      exit()
  elif(q[0]):
    if(p[1]<q[1]):
      print(0)
      exit()
  else:
	  ans=ans*min(p[1],q[1])%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] or T.index(T[-1]) > N - 1 - A[::-1].index(A[0]):
    print(0)
    exit()
ans = 1
MOD = 10 ** 9 + 7
for i in range(1, N - 1):
    t_comb = 1
    if T[i - 1] == T[i]:
        t_comb *= T[i]

    a_comb = 1
    if A[i] == A[i + 1]:
        a_comb *= A[i]
    ans = (ans * min(t_comb, a_comb)) % MOD
print(ans)

Solution (Python):
N = int(input())
T = list(map(int, input().split()))
A = list(map(int, input().split()))
h = []
for i in range(N):
    if i == 0:
        h.append(1)
    elif i == N - 1:
        h.append(1)
    else:
        if T[i] > T[i - 1]:
            h.append(1)
            if T[i] > A[i]:
                h[i] = 0
                break
        elif A[i] > A[i + 1]:
            h.append(1)
            if A[i] > T[i]:
                h[i] = 0
                break
        else:
            h.append(min([A[i], T[i]]))

if max(T) != max(A):
    h.append(0)
    
cnt = 1
for i in range(len(h)):
    cnt *= h[i]
    cnt %= 1000000007
                     
print(cnt % 1000000007)

Solution (Python):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())

N = int(input())
tt = inpl()
aa = inpl()
hh = [[0,0] for i in range(N)]

bt = 0
for i in range(N):
	t = tt[i]
	if t != bt:
		hh[i] = [t,t]
	else:
		hh[i] = [1,t]
	bt = t

ba = 0
for i in reversed(range(N)):
	a = aa[i]
	MIN,MAX = hh[i]
	if a != ba:
		if MIN <= a <= MAX:
			hh[i] = [a,a]
		else:
			print(0)
			sys.exit()
	else:
		hh[i] = [max(1,MIN),min(a,MAX)]

	ba = a

ans = 1
for azu,nyan in hh:
	ans *= (nyan-azu+1)
	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()))
ans = 1

if t[0] > a[0] or t[-1] < a[-1] or t[-1] != a[0]:
	ans *= 0

for i in range(1, n - 1):
	if t[i - 1] != t[i]:
		if t[i] > a[i]:
			ans *= 0
			break
		else:
			continue
	if a[i + 1] != a[i]:
		if t[i] < a[i]:
			ans *= 0
			break
		else:
			continue
	ans = (ans * min(t[i], a[i])) % MOD

print(ans)

Solution (Python):

import sys
stdin = sys.stdin

def li(): return [int(x) for x in stdin.readline().split()]
def li_(): return [int(x)-1 for x in stdin.readline().split()]
def lf(): return [float(x) for x in stdin.readline().split()]
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(ns())
def nf(): return float(ns())

n = ni()
t = li()
a = li()

MOD = 10**9 + 7


t_modified = [0]*n
a_modified = [0]*n
t_modified[0] = 1
a_modified[-1] = 1
consistent = True

if max(a) != max(t):
    consistent = False

for i in range(n-1):
    if t[i+1] != t[i]:
        t_modified[i+1] = 1
        
        if t[i+1] > a[i+1]:
            consistent = False
            
    if a[i] != a[i+1]:
        a_modified[i] = 1
        
        if a[i] > t[i]:
            consistent = False


ans = 1
for i in range(n):
    if t_modified[i] == 0 and a_modified[i] == 0:
        ans = (ans * min(t[i], a[i])) % MOD

if consistent:
    print(ans)
else:
    print(0)

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

a = map(int,raw_input().split())
b = map(int,raw_input().split())

combs = [0 for i in range(n)]
unique = [0 for i in range(n)]
t = 0
mod = 10**9 + 7
for i in range(n):
	if a[i] > t or a[i] == 1:
		combs[i] = 1
		t = a[i]
		unique[i] = 1

	else :
		combs[i] = t
t = 0
for i in range(n-1,-1,-1):
	if unique[i] == 1 and a[i] > b[i] :
		print 0
		quit()

	if b[i] > t:
		combs[i] = 1
		t = b[i]

	else :
		combs[i] = min(combs[i], t)

ans = 1
for i in combs:
	ans = (ans * i)%mod

print ans

Solution (Python):
import sys

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [long(input_raw[0])]
    input_rawT = read_func().strip().split()
    input_rawA = read_func().strip().split()
    T = []
    A = []
    for i in range(N):
        T.append(long(input_rawT[i]))
        A.append(long(input_rawA[i]))
    count = 1
    h_T = [(0, 0) for i in range(N)]
    h_A = [(0, 0) for i in range(N)]
    h_T[0] = (T[0], T[0])
    h_A[N - 1] = (A[N - 1], A[N - 1])

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

        if A[j] == A[j + 1]:
            h_A[j] = (1, A[j])
        else:
            h_A[j] = (A[j], A[j])

    count = 1L
    for i in range(0, N):
        h1 = max(h_A[i][0], h_T[i][0])
        h2 = min(h_A[i][1], h_T[i][1])
        if h1 > h2:
            print 0
            exit()
        count = ((count * long(h2 - h1 + 1)) % long(1e9 + 7))

    print count




if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
l = list(map(int,input().split()))
r = list(map(int,input().split()))
l1 = max(l)
r1 = max(r)
left = 1
if l[0] == l1:left = 0

right = n-2
if r[-1] == l1:right = n-1

res = 1
if l1 != r1:print(0)
else:
    while l[left] != l1:
        if l[left] == l[left-1]:
            res = (res*l[left])%1000000007
        left += 1
    while r[right] != r1:
        if r[right] == r[right+1]:
            res = (res*r[right])%1000000007
        right -=1

    if  left > right:print(0)
    else:
        if  left < right:
            for i in range(right-left-1):
                res = (res*l1)%1000000007

        print(res%1000000007)

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

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

    if is_certain[-1] and h[-1] != a[-1]:
        print(0)
        exit()
    else:
        h[-1] = a[-1]
        is_certain[-1] = 1

    for i in range(n-2, -1, -1):
        if a[i] > a[i+1]:
            if is_certain[i] and h[i] != a[i]:
                print(0)
                exit()
            else:
                h[i] = a[i]
                is_certain[i] = 1
        elif is_certain[i] and a[i] < h[i]:
            print(0)
            exit()
        else:
            h[i] = min(h[i], a[i])

    ans = 1
    for i in range(n):
        if not is_certain[i]:
            ans = ans * h[i] % 1000000007

    print(ans)


if __name__ == '__main__':
    main()

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

B = [10**9] * n

h = 0

for i, t in enumerate(T):
    if h < t:
        B[i] = 1
        h = t
    else:
        B[i] = h

h = 0

for i, a in zip(range(n-1, -1, -1), A[::-1]):
    if h < a:
        if a > T[i] or (B[i] == 1 and a != T[i]):
            print(0)
            exit(0)
        B[i] = 1
        h = a
    elif B[i] == 1 and T[i] > a:
        print(0)
        exit(0)
    elif B[i] > h:
        B[i] = h

ans = 1

for b in B:
    ans *= b
    ans %= p

print(ans)

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

B = [10**9] * n

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

h = 0

for i, t in enumerate(T):
    if h < t:
        B[i] = 1
        h = t
    else:
        B[i] = h

h = 0

for i, a in zip(range(n-1, -1, -1), A[::-1]):
    if h < a:
        B[i] = 1
        h = a
    elif B[i] == 1 and T[i] > A[i]:
        print(0)
        exit(0)
    elif B[i] > h:
        B[i] = h

ans = 1

for b in B:
    ans *= b
    ans %= p

print(ans)

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

T = [T[0]] + T + [T[-1]]
A = [A[0]] + A + [A[-1]]

ans = 1
mod = 10**9 + 7
for i in range(1, N+1):
    if T[i-1] < T[i] and A[i + 1] < A[i]:
        if T[i] != A[i]:
            ans *= 0
        continue

    if T[i-1] < T[i]:
        if A[i] < T[i]:
            ans *= 0
        continue

    if A[i] > A[i+1]:
        if T[i] < A[i]:
            ans *= 0
        continue

    if i == 1 or i == N:
        if T[N] != A[1]:
            ans *= 0
        else:
            ans *= 1
        continue

    ans = (ans * min(T[i], A[i])) % mod

print(ans % mod)

Solution (Python):

import sys
try: from typing import Any, Union, List, Tuple, Dict
except ImportError: pass
def debug(*args): print(*args, file=sys.stderr)
def exit(): sys.exit(0)


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


T_ = []
A_ = []

T_.append(("=", T[0]))
for i in range(1, N):
    if T[i-1] == T[i]:
        T_.append(("<=", T[i]))
    else:
        T_.append(("=", T[i]))

for i in range(N-1):
    if A[i] == A[i+1]:
        A_.append(("<=", A[i]))
    else:
        A_.append(("=", A[i]))
A_.append(("=", A[N-1]))


count = 1
for i in range(N):
    t0, t1 = T_[i]
    a0, a1 = A_[i]
    if t0 == "=" and a0 == "=":
        if t1 == a1:
            continue
        else:
            print(0)
            exit()
    elif t0 == "=":
        if t1 > a1:
            print(0)
            exit()
        else:
            continue
    elif a0 == "=":
        if a1 > t1:
            print(0)
            exit()
        else:
            continue
    else:
        count = (count * min(t1, a1)) % (10**9+7)

print(count)

Solution (Python):
n = int(input())
t = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
x,flag,ans,mod = [[t[0],t[0]]]+[[1,1] for _ in range(n-1)],True,1,10**9+7
if max(t)!=max(a): flag = False
for i in range(1,n):
	if t[i-1]<t[i]: x[i] = [t[i],t[i]]
	else: x[i][0] = t[i]
if x[-1][0]<a[-1]: flag = False
else:
	x[-1] = [a[-1],a[-1]]
	for i in range(n-2,0,-1):
		if a[i]>a[i+1]:
			if x[i][0]>=a[i]: x[i]=[a[i],a[i]]
			else: flag = False
		else:
			if x[i][0]>=a[i]: x[i][0]=a[i]
			else: pass
if flag:
	for i,j in x: ans = (ans*(i-j+1))%mod
else: ans = 0
print(ans)

Solution (Python):

yama=[0 for i in range(int(input()))]
t=list(map(int,input().split()))
a=list(map(int,input().split()))

check=0
for i in range(len(yama)):
    if check!=t[i]:
        check=t[i]
        yama[i]=check
    else:
        yama[i]=-check
check=0
for i in range(len(yama)):
    if yama[len(yama)-i-1]>0 and yama[len(yama)-i-1]>a[len(yama)-i-1]:
        print(0)
        exit()
    if check!=a[len(yama)-i-1]:
        check=a[len(yama)-i-1]
        if yama[len(yama)-i-1]>0 and yama[len(yama)-i-1]>check:
            print(0)
            exit()
        yama[len(yama)-i-1]=check
    else:
        if yama[len(yama)-i-1]<0:
            yama[len(yama)-i-1]=max(-check,yama[len(yama)-i-1])

x=1
for i in yama:
    if i<0:
        x=(x*-i)%1000000007

print(x)

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

ans = 1
for i in range(N):
    min_t = min_a = 1
    if i == 0 or T[i-1] < T[i]:
        min_t = T[i]
    if i == N-1 or A[i+1] < A[i]:
        min_a = A[i]
    if min_t > A[i] or min_a > T[i]:
            print(0)
            exit()
    n = min(T[i],A[i]) - max(min_t,min_a) + 1
    ans = (ans * n) % MOD
print(ans)

Solution (Python):
mo = 1000000007
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)
    exit()

mt = [False] * n 
mt[0] = True
mt[-1] = True

for i in range(1, n):
    if t[i - 1] < t[i]:
        mt[i] = True
        if a[i] < t[i]:
            print(0)
            exit()

for i in range(n - 2, -1, -1):
    if a[i] > a[i + 1]:
        mt[i] = True
        if t[i] < a[i]:
            print(0)
            exit()

ans = 1
for i in range(n):
    if not mt[i]:
        ans *= min(t[i], a[i])
        ans %= mo

print(ans)

Solution (Python):


N=int(input())
mozir=input()
hyo=mozir.split(' ')

Rec_LTR=[int(mono) for mono in hyo]

mozir=input()
hyo=mozir.split(' ')

Rec_RTL=[int(mono) for mono in hyo]

LTR_hani=[]
highest=0
for idx in range(0,N,1):
    if Rec_LTR[idx]>highest:
        highest=Rec_LTR[idx]
        LTR_hani.append([highest,highest])
    else:
        LTR_hani.append([highest,1])

RTL_hani=[None]*N
highest=0
for idx in range(N-1,-1,-1):
    if Rec_RTL[idx]>highest:
        highest=Rec_RTL[idx]
        RTL_hani[idx]=[highest,highest]
    else:
        RTL_hani[idx]=[highest,1]


ans=1
hou=1000000007
for idx in range(0,N,1):
    
    ue=min(RTL_hani[idx][0],LTR_hani[idx][0])
    shita=max(RTL_hani[idx][1],LTR_hani[idx][1])
    ans=(ans*max((ue-shita+1),0))%hou

print(ans)

Solution (Python):


def main():
    ans = 1
    D = 10 ** 9 + 7
    N = int(input())
    T = list(map(int, input().split()))
    A = list(map(int, input().split()))
    L = len(T)
    ST, SA = [0] * L, [0] * L
    ST[0], SA[-1] = T[0], A[-1]
    if L > 1:
        if T[-1] != T[-2] : ST[-1] = T[-1]
        if A[0] != A[1] : SA[0] = A[0]
    for i in range(1, L-1):
        if T[i] == T[i-1] and A[i] == A[i+1]:
            ans = (ans * min(T[i], A[i])) % D
        else:
            if T[i] != T[i-1] : ST[i] = T[i]
            if A[i] != A[i+1] : SA[i] = A[i]
    for i in range(L):
        if ST[i] and SA[i] and ST[i] != SA[i]:
            ans = 0; break
        if A[i] < ST[i] or T[i] < SA[i]:
            ans = 0; break
    print(ans)
    
if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
T = list(map(int,input().split()))
A = list(map(int,input().split()))
A.append(1)
count = 0
Tmax = 1
Amax = 1
T2 = [0] * N
A2 = [0] * N
ans = 1

for i in range(N):
    if T[i] > Tmax:
        Tmax = T[i]
        if A[i] > A[i+1]:
            Amax = A[i+1]
            if T[i] == A[i]:
                count = 1
            else:
                ans = 0
                break
        else:
            if T[i] > A[i]:
                ans = 0
                break
            else:
                count = 1
    else:
        if A[i] > A[i+1]:
            Amax = A[i+1]
            if A[i] > T[i]:
                ans = 0
                break
            else:
                count = 1
        else:
            count = min(A[i],T[i])
    ans *= count
    ans = ans % (10**9+7)
print(ans)

Solution (Python):
N = int(input())
T = list(map(int,input().split()))
A = list(map(int,input().split()))
A.append(1)
count = 0
Tmax = 1
Amax = 1
T2 = [0] * N
A2 = [0] * N
ans = 1

for i in range(N):
    if T[i] > Tmax and A[i] > A[i+1] and T[i] != A[i]:
        ans = 0
        break
    elif T[i] > Tmax and A[i] > A[i+1] and T[i] == A[i]:
        count = 1
        Tmax = T[i]
        Amax = A[i+1]
    elif T[i] > Tmax and A[i] == A[i+1] and T[i] > A[i]:
        ans = 0
        break
    elif T[i] > Tmax and A[i] == A[i+1] and T[i] <= A[i]:
        count = 1
        Tmax = T[i]
    elif T[i] == Tmax and A[i] > A[i+1] and A[i] > T[i]:
        ans = 0
        break
    elif T[i] == Tmax and A[i] > A[i+1] and A[i] <= T[i]:
        count = 1
        Amax = A[i+1]
    else:
        count = min(A[i],T[i])
    ans *= count
    ans = ans % (10**9+7)
print(ans)

Solution (Python):
N = int(input())
T = list(map(int,input().split()))
A = list(map(int,input().split()))
A.append(1)
count = 0
Tmax = 1
Amax = 1
T2 = [0] * N
A2 = [0] * N
ans = 1

for i in range(N):
    if T[i] > Tmax and A[i] > A[i+1] and T[i] != A[i]:
        ans = 0
        break
    elif T[i] > Tmax and A[i] == A[i+1] and T[i] > A[i]:
        ans = 0
        break
    elif A[i] > A[i+1] and T[i] == Tmax and A[i] > T[i]:
        ans = 0
        break
    elif T[i] > Tmax and A[i] > A[i+1] and T[i] == A[i]:
        count = 1
        Tmax = T[i]
        Amax = A[i+1]
    elif T[i] > Tmax and A[i] == A[i+1] and T[i] <= A[i]:
        count = 1
        Tmax = T[i]
    elif A[i] > A[i+1] and T[i] == Tmax and A[i] <= Tmax:
        count = 1
        Amax = A[i+1]
    else:
        count = min(A[i],T[i])
    ans *= count
    ans = ans % (10**9+7)
print(ans)

Solution (Python):
m = 10**9 + 7
n = int(input())
t = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
h = [t[i] if i == 0 or t[i-1] < t[i] else a[i] if i == n - 1 or a[i] > a[i+1] else -1
     for i in range(n)]
ans = 1
top = 0
for i in range(n):
    top = max(top, h[i])
    ans *= 1 if top == t[i] else 0
top = 0
for i in reversed(range(n)):
    top = max(top, h[i])
    ans *= 1 if top == a[i] else 0
prev = 0
for i in range(1, n):
    if h[i] > 0:
        ans = ans * pow(min(h[prev], h[i]), i - prev - 1, m) % m
        prev = i
print(ans)

Solution (Python):
def read():
	return [int(i) for i in input().split(" ")]
N = int(input())
T = read()
A = read()
T = [(i != j, i) for i, j in zip(T, [None] + T)]
A = [(i != j, i) for i, j in zip(A, (A + [None])[1:])]
P = 1
for p, q in zip(T, A):
	if(p[0]):
		if(q[0]):
			if(p[1] != q[1]):
				print(0)
				exit()
		else:
			if(q[1] < p[1]):
				print(0)
				exit()
	elif(q[0]):
		if(p[1] < q[1]):
			print(0)
			exit()
	else:
		P = P * min(p[1], q[1]) % (10 ** 9 + 7)
print(P)

Solution (Python):
def read():
	return [int(i) for i in input().split(" ")]
N = int(input())
T = read()
A = read()
t = [(i != j, i) for i, j in zip(T, [None] + T)]
a = [(i != j, i) for i, j in zip(A, (A + [None])[1:])]
P = 1
for p, q in zip(t, a):
	if(p[0]):
		if(q[0]):
			if(p[1] != q[1]):
				print(0)
				exit()
		elif(q[1] < p[1]):
			print(0)
			exit()
	elif(q[0]):
		if(p[1] < q[1]):
			print(0)
			exit()
	else:
		P = P * min(p[1], q[1]) % (10 ** 9 + 7)
print(P)

Solution (Python):
def read():
	return [int(i) for i in input().split(" ")]
N = int(input())
T = read()
A = read()
t = [(i != j, i) for i, j in zip(T, [None] + T)]
a = [(i != j, i) for i, j in zip(A, (A + [None])[1:])]
P = 1
for p, q in zip(t, a):
	if(p[0]):
		if(q[0]):
			if(p[1] != q[1]):
				print(0)
				exit()
		else:
			if(q[1] < p[1]):
				print(0)
				exit()
	elif(q[0]):
		if(p[1] < q[1]):
			print(0)
			exit()
	else:
		P = P * min(p[1], q[1]) % (10 ** 9 + 7)
print(P)

Solution (Python):
n=int(input())
l= list(map(int, input().split()))
r= list(map(int, input().split()))
l=[0]+l
r.append(0)
 
res=1
if(l[-1]!=r[0]):
	res=0
for i in range(n):
	if( (l[i]<l[i+1]) or (r[i]>r[i+1]) ):
		if( ( (l[i]<l[i+1]) and (r[i]>r[i+1]) ) and (l[i+1]!=r[i]) ):
			res=0
		if( ( (l[i]<l[i+1]) and (r[i]==r[i+1]) ) and (l[i+1]>r[i]) ):
			res=0
		if( ( (l[i]==l[i+1]) and (r[i]>r[i+1]) ) and (l[i+1]<r[i]) ):
			res=0
	else:
		res*=min(l[i],r[i])
		res%=1000000007
print(res)

Solution (Python):


n = int(raw_input())

ls_t = map(int, raw_input().split())
ls_a = map(int, raw_input().split())

max_m1 = max(ls_t)
max_m2 = max(ls_a)

if max_m1 != max_m2 or ls_t.index(max_m1)  + list(reversed(ls_a)).index(max_m2) >= n or sorted(ls_t) != ls_t or sorted(ls_a) != list(reversed(ls_a)):
    print '0'
else:
    idx = ls_t.index(max_m1)
    def get_same_ls(ls):
        if len(ls) == 0:
            return []
        prev = ls[0]
        out = []
        for i in ls[1:]:
            if i == prev:
                out.append(i)
            else:
                prev = i
        return out
    ls_mult = get_same_ls(ls_t[:idx]) + get_same_ls(list(reversed(ls_a))[:n-idx-1])
    mult = 1
    m = pow(10, 9) + 7
    for i in ls_mult:
        mult = mult * i % m
    print mult

Solution (Python):
import sys                                                                                                                                                                                                                                                                                      
def range_union(al,ah,bl,bh):
    cl = max(al,bl)
    ch = min(ah,bh)
    if cl > ch:
        return 0
    else:
        return ch-cl +1
 
N = int(sys.stdin.readline())
T = map(int,sys.stdin.readline().strip().split())
A = map(int,sys.stdin.readline().strip().split())
 
p = 1
if N >= 2:
    Tl = T[0]
    Th = T[0]
    if A[0] > A[1]:
        Al = A[0]
        Ah = A[0]
    elif A[1] == A[0]:
        Al = 1
        Ah = A[0]
    else:
        p = 0
        print p
        exit(0)
 
    r = range_union(Tl,Th,Al,Ah)
    if not r:
        p = 0
        print p
        exit(0)
    else:
        p = (p*r) % 1000000007
        
for i in xrange(1,N-1):
    if T[i-1] < T[i]:
        Tl = T[i]
        Th = T[i]
    elif T[i-1] == T[i]:
        Tl = 1
        Th = T[i]
    else:
        p = 0
        break
        
    if A[i] > A[i+1]:
        Al = A[i]
        Ah = A[i]
    elif A[i+1] == A[i]:
        Al = 1
        Ah = A[i]
    else:
        p = 0
        break
 
    r = range_union(Tl,Th,Al,Ah)
    if not r:
        p = 0
        break
    else:
        p = (p*r) % 1000000007
 
if N >= 2:
    Al = A[-1]
    Ah = A[-1]
    if T[-1] > T[-2]:
        Tl = T[-1]
        Th = T[-1]
    elif T[-1] == T[-2]:
        Tl = 1
        Th = T[-1]
    else:
        p = 0
        print p
        exit(0)
 
    r = range_union(Tl,Th,Al,Ah)
    if not r:
        p = 0
        print p
        exit(0)
    else:
        p = (p*r) % 1000000007
 
if A[0] != T[-1]:
    print 0
    exit(0)
print p

Solution (Python):
N = int(input())
T = list(map(int,input().split()))
A = list(map(int,input().split()))
mami = []
ans = 1
mod = 10**9 + 7
for i in range(N):
    mi, ma = 1 , min(T[i], A[i])+1
    if i != N-1:
        if A[i] > A[i+1]:
            mi = A[i]
    else :
        mi = A[i]
    if i != 0 :
        if T[i] > T[i-1]:
            mi = T[i]
    else :
        mi = T[i]            
    ans = (max(ma - mi, 0) * ans ) % mod
print (ans)

Solution (Python):
n = int(raw_input())
t = map(int, raw_input().split())
a = map(int, raw_input().split())
M = 10**9 + 7
ans = 1
m = [0] * n
if t[n-1] == a[0]:
    for i in xrange(n):
        if n == 1:
            break
        m = min([t[i], a[i]])
        flag1 = False
        flag2 = False
        if i > 0:
            if t[i-1] < t[i]:
                if a[i] < t[i]:
                    ans = 0
                    break
                flag1 = True
        else:
            flag1 = True
        if i < n-1:
            if a[i+1] < a[i]:
                if a[i] > t[i]:
                    ans  = 0
                    break
                flag2 = True
        else:
            flag2  = True
        
        
        if not(flag1 or flag2):
            ans *= m
            ans %= M
else:
    ans = 0
print ans,

Solution (Python):
import sys

MOD = 1000000007

def union(x,y):
    if len(x)==1:
        if len(y)==1:
            return (1 if x[0]==y[0] else 0)
        else:
            return (1 if y[-1]>=x[0] else 0)
    if len(x)>1:
        if len(y)==1:
            return (1 if x[-1]>=y[0] else 0)
        else:
            return min(len(x),len(y))

n = int(input())
a = list(map(lambda x: int(x) % MOD, input().split()))
b = list(map(lambda x: int(x) % MOD, input().split()))
x = [0 for _ in range(n)]
y = [0 for _ in range(n)]
x[0] = 1
y[-1] = 1
z = 1

for i in range(1,n):
    if a[i-1]<a[i]:
        x[i] = 1
    else:
        x[i] = 0

for j in range(n-1, 0, -1):
    if b[j-1]>b[j]:
        y[j-1] += 1
    else:
        y[j-1] += 0

for i in range(n):
    if x[i] == 1 and y[i] == 0:
        if a[i] > b[i]:
            print(0)
            sys.exit()
    elif x[i] == 0 and y[i] == 1:
        if a[i] < b[i]:
            print(0)
            sys.exit()
    elif x[i] == 1 and y[i] == 1:
        if a[i] != b[i]:
            print(0)
            sys.exit()
    else:
        z *= min(a[i],b[i])
        z = z % MOD

print(z)

Solution (Python):
import sys

n = int(input())
t = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
if t[n-1]!=a[0]:
    print(0)
    sys.exit()

m =[]
for i in range(n):
    mx = min(t[i],a[i])
    mn = 1
    if i == 0:
        mn = t[i]
    elif i == n-1:
        mn = a[i]
    else :
        if t[i]!=t[i-1] :
            if t[i-1] > a[i-1]:
                print(0)
                exit()
            if mx == t[i]:
                mn=t[i]
        if a[i]!=a[i+1] :
            if a[i] > t[i]:
                print(0)
                exit()
            if mx == a[i]:
                mn=a[i]
    m.append([mx,min(mx,mn)])
ans = 1
for i in range(n):
    ans = (ans * (m[i][0]-m[i][1]+1))%(10**9+7)
print(ans)

Solution (Python):


import collections
import functools


MOD = 10 ** 9 + 7
INF = MOD


def product_mod(seq, mod=MOD):
    return functools.reduce(lambda x, y: x * y % mod, seq, 1) % mod


Mountain = collections.namedtuple("Mountain", "ub lb")


def main():
    n = int(input())
    xs = [int(x) for x in input().split()]
    ys = [int(y) for y in input().split()]
    ms = [Mountain(INF, 1) for _ in range(n)]
    ns = [Mountain(INF, 1) for _ in range(n)]
    ms[0] = Mountain(xs[0], xs[0])
    ns[-1] = Mountain(ys[-1], ys[-1])
    for idx in range(1, n):
        if xs[idx - 1] < xs[idx]:
            ms[idx] = Mountain(xs[idx], xs[idx])
        else:
            ms[idx] = Mountain(xs[idx], ms[idx].lb)
    for idx in range(n - 1)[::-1]:
        if ys[idx + 1] < ys[idx]:
            ns[idx] = Mountain(ys[idx], ys[idx])
        else:
            ns[idx] = Mountain(ys[idx], ns[idx].lb)
    hs = (Mountain(min(m.ub, n.ub), max(m.lb, n.lb)) for m, n in zip(ms, ns))
    ans = product_mod(max(h.ub - h.lb + 1, 0) for h in hs)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
ts = list(map(int,input().split()))
As = list(map(int,input().split()))
tr = [0]*n
ar = [0]*n
ct = 0
ca = 0
for i in range(n):
    if ct!=ts[i]:
        tr[i]=(ts[i],ts[i])
        ct=ts[i]
    else:
        tr[i]=(1,ts[i])
    if ca!=As[n-i-1]:
        ar[n-i-1]=(As[n-i-1],As[n-i-1])
        ca=As[n-i-1]
    else:
        ar[n-i-1]=(1,As[n-i-1])


ans=1
for i in range(n):
    if tr[i][0]==1 and ar[i][0]==1:
        ans=(ans*min(tr[i][1],ar[i][1]))%1000000007
    elif tr[i][0]==1 and ar[i][0]>1:
        if tr[i][1]<ar[i][1]:
            print(0)
            break
    elif tr[i][0]>1 and ar[i][0]==1:
        if tr[i][1]>ar[i][1]:
            print(0)
            break
    else:
        if tr[i][0]!=ar[i][0]:
            print(0)
            break
else:
    print(ans%1000000007)

Solution (Python):
N = int(input())
T = [int(t) for t in input().split()]
A = [int(a) for a in input().split()]

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

P = 1

for i in range(1,N-1):
    t = 1 if T[i-1]<T[i] else T[i]
    a = 1 if A[i]>A[i+1] else A[i]
    if(t==1):
        if(T[i]>A[i]):
            print(0)
            exit()
    if(a==1):
        if(A[i]>T[i]):
            print(0)
            exit()
    P*=min(t,a)
    P%=1000000007

print(P)

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

if t[n-1]!=a[0]:
    print(0)
    exit()

ans=1
for i in xrange(n):
    if i==0 or i==n-1:
        continue
    elif t[i]>t[i-1]:
        if a[i]<t[i] and a[i]==a[i+1]:
            print(0)
            exit()
        elif a[i]!=t[i] and a[i]!=a[i+1]:
            print(0)
            exit()
    else:
        if t[i]==a[i]:
            if a[i]==a[i+1]:
                ans=(ans*t[i])%mod
        elif t[i]>a[i]:
            if a[i]==a[i+1]:
                ans=(ans*a[i])%mod
        elif a[i]>t[i]:
            if a[i]==a[i+1]:
                ans=(ans*t[i])%mod
            else:
                print(0)
                exit()
print(ans%mod)