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.



Sample input:
5
1 3 3 3 3
3 3 2 2 2


Sample output:
4

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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 # 山の高さが1通りのときTrue
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)

Submission status: Accepted

Solution code:
#coding=UTF-8

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]

#concat
ans=1
hou=1000000007
for idx in range(0,N,1):
    # prod set
    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)

Submission status: Accepted

Solution code:
#! /usr/bin/env python3

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()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# get a integer
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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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
        #print m
        
        if not(flag1 or flag2):
            ans *= m
            ans %= M
else:
    ans = 0
print ans,

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

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()

Submission status: Accepted

Solution code:
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])
#print(tr)
#print(ar)
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# def makelist(n, m):
# 	return [[0 for i in range(m)] for j in range(n)]

# n = int(input())
# a, b = map(int, input().split())
# s = input()

MOD = int(1e9) + 7
n = int(input())
T = list(map(int, input().split()))
A = list(map(int, input().split()))

Tsa = [0]*n
Asa = [0]*n

if n == 1:
	if T[0] != A[0]:
		print(0)
	else:
		print(1)
elif T[-1] != A[0]:
	print(0)
else:
	##
	dai = T[0]
	shou = T[0]
	Tsa[0] = (shou, dai)

	for i in range(1, n):
		if T[i] == dai:
			Tsa[i] = (1, dai)
		else:
			dai = T[i]
			Tsa[i] = (dai, dai)

	##
	dai = A[-1]
	shou = A[-1]
	Asa[-1] = (shou, dai)

	for i in reversed(range(-n, -1)):
		if A[i] == dai:
			Asa[i] = (1, dai)
		else:
			dai = A[i]
			Asa[i] = (dai, dai)

	##
	ans = 1
	for i in range(n):
		d = max(Tsa[i][0], Asa[i][0])
		u = min(Tsa[i][1], Asa[i][1])

		if d > u:
			ans = 0
			break
		else:
			ans *= u - d + 1
			ans %= MOD
	print(ans)

Submission status: Accepted

Solution code:

def read():
    return int(input())

def reads(sep=None):
    return list(map(int, input().split(sep)))

def main():
    m = 10**9 + 7
    n = read()
    t = reads()
    a = reads()
    if (t != sorted(t)) or (a != sorted(a, reverse=True)) or (max(t) != max(a)):
        print(0)
        return
    if len([1 for ti,ai in zip(t, a) if ti==ai]) == 0:
        print(0)
        return
    
    b = [0] * n
    o = -1
    for i in range(0, n):
        if o != t[i]:
            b[i] = 1
            o = t[i]
            if a[i] < t[i]:
                print(0)
                return
    o = -1
    for i in range(n-1, -1, -1):
        if o != a[i]:
            b[i] = 1
            o = a[i]
            if t[i] < a[i]:
                print(0)
                return
    r = 1
    for i in range(0, n):
        if b[i] == 0:
            r = (r * min(t[i], a[i])) % m
    print(r)

main()

Submission status: Accepted

Solution code:
n = int(input())
ts = list(map(int, input().split()))
xs = list(map(int, input().split()))

ts = [0] + ts + [ts[-1]]
xs = [xs[0]] + xs + [0]

if n == 1:
    if xs[1] == ts[1]:
        print(1)
    else:
        print(0)
else:
    m = 10**9 + 7
    a = 1
    for i in range(1, n+1):
        if ts[i-1]==ts[i] and xs[i]==xs[i+1]:
            a = a*min(ts[i], xs[i]) % m
        else:
            if ts[i-1]<ts[i] and xs[i]>xs[i+1]:
                if ts[i]!=xs[i]:
                    a = 0
            elif ts[i-1]<ts[i]:
                if xs[i]<ts[i]:
                    a = 0
            else:
                if ts[i]<xs[i]:
                    a = 0
    print(a)

Submission status: Accepted

Solution code:
#coding: utf-8

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

    m = [0] * N

    b = 0
    for i,h in enumerate(t):
        if b != h:
            m[i] = h#確定
            if a[N-i-1] < h:
                print(0)
                return
        b = h
    b = 0
    for j,h in enumerate(a):
        p = N-j-1
        if b != h:
            if m[p] != 0 and m[p] != h:
                print(0)
                return
            else:
                m[p] = h#確定
                if t[p] < h:
                    print(0)
                    return
        b = h
    res = 1
    for k,h in enumerate(m):
        if h == 0:
            res = res * min(a[N-k-1], t[k]) % (pow(10, 9) + 7)
    print(res)
    return

main()

Submission status: Accepted

Solution code:
N = input()
T = map(int,raw_input().split(" "))
A = map(int,raw_input().split(" "))




note = [1 for i in range(N)]
fx = [0 for i in range(N)]

def solve():
    global note
    if T[-1] != A[0]:
        return 0
    
    prev = 0
    for i in range(N):
        if prev > T[i]:
            return 0
        elif prev == T[i]:
            note[i] = prev
        else:
            note[i] = 1
            prev = T[i]
            fx[i] = T[i]
    prev = 0
    for i in range(N-1,-1,-1):
        if prev > A[i]:
            return 0
        elif prev == A[i]:
            if not fx[i]:
                note[i] = min(note[i],prev)
            else:
                if fx[i] > A[i]:
                    return 0

        else:
            if fx[i] and fx[i] != A[i]:
                return 0
            note[i] = 1
            prev = A[i]

    ans = 1
    for i in range(N):
        if not fx[i]:
            ans = (ans * note[i])%(10**9+7)
    return ans



    





print solve()

Submission status: Accepted

Solution code:
def work(n, t, a):
    if t[-1] != a[0]:
        return 0

    middle = 0
    for i in xrange(n):
        if t[i] == a[i]:
            if t[i] != t[-1] or t[i] != a[0]:
                return 0
            middle = i
            break
    for i in xrange(middle, n):
        if t[i] < a[i]:
            return 0

    record = [None] * n
    left, right = 0, n - 1

    while left <= right and t[left] < a[left]:
        if not left or t[left] > t[left-1]:
            record[left] = 1
        else:
            record[left] = t[left]
        left += 1
    
    record[left] = 1
    while left < right and a[right] <= t[right]:
        if right == n - 1 or a[right] > a[right+1]:
            record[right] = 1
        else:
            record[right] = a[right]
        right -= 1

    ans = 1
    for i in xrange(n):
        ans = (ans*record[i]) % (10**9+7)
    return ans


n = int(raw_input())
t = map(int, raw_input().split())
a = map(int, raw_input().split())

print work(n, t, a)

Submission status: Accepted

Solution code:
n = int(input())
t = list(map(int, input().split()))
a = list(map(int, input().split()))
if n==1 and t[0] != a[0]:
    print(0)
    exit()
p = [False for i in range(n)]
p[0] = True
p[n - 1] = True

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

for i in range(n - 2, -1, -1):
    if a[i] > a[i + 1]:
        p[i] = True
        if t[i] < a[i]:
            print(0)
            exit()
h=[min(t[i],a[i]) for i in range(n)]
mod = 10**9 + 7
total = 1
for i in range(n):
    if not p[i]:
        total = (total * h[i]) % mod
print(total)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding:utf-8 -*-

from __future__ import division, print_function, absolute_import, unicode_literals

import sys

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

h = [None] * N

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

odd = False
h[0] = T[0]
for i, (a, b) in enumerate(zip(T[1:], T[:-1]), 1):
    if a != b:
        h[i] = a
    if a < b:
        odd = True
        break

if odd is True:
    print(0)
    sys.exit()

if h[-1] is None:
    h[-1] = A[-1]
elif h[-1] != A[-1]:
    print(0)
    sys.exit()

for i, (a, b) in enumerate(zip(A[::-1][1:], A[::-1][:-1]), 1):
    if a != b:
        if h[N-i-1] is None:
            h[N-i-1] = a
        elif h[N-i-1] != a:
            odd = True
            break
    if a < b:
        odd = True
        break

if odd is True:
    print(0)
    sys.exit()

i = 1
a, b = 0, 0
cnt = 0
ans = 1
flag = False
while i < N-1:
    if h[i] is None:
        flag = True
        a = h[i-1]
        cnt = 0
        while h[i] is None:
            i += 1
            cnt += 1
        b = h[i]
        # print(a, b, cnt)
        ans = (ans * (min(a, b) ** cnt)) % (10 ** 9 + 7)
        # print(ans)
    else:
        i += 1

# check
# print(h)
odd = False
for i in range(N):
    if h[i] != None:
        cm = h[i]
    if T[i] < cm:
        # print(i, cm, T[i])
        odd = True
        break
cm = None
for i in range(N):
    if h[N-i-1] != None:
        cm = h[N-i-1]
    if A[N-i-1] < cm:
        # print('hoge')
        # print(N-i-1, cm, A[N-i-1])
        odd = True
        break
if odd is True:
    print(0)
    sys.exit()

print(ans)

Submission status: Accepted

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

t_left = []
t_right = []
a_left = []
a_right = []
for i in range(N):
    if i == 0:
        t_left.append(T[i])
        t_right.append(T[i])
    else:
        if T[i] > T[i-1]:
            t_left.append(T[i])
            t_right.append(T[i])
        else:
            t_left.append(1)
            t_right.append(T[i])

for i in range(N - 1, -1, -1):
    if i == N - 1:
        a_left.append(A[i])
        a_right.append(A[i])
    else:
        if A[i] > A[i+1]:
            a_left.append(A[i])
            a_right.append(A[i])
        else:
            a_left.append(1)
            a_right.append(A[i])
a_left = a_left[::-1]
a_right = a_right[::-1]
# print(t_left)
# print(t_right)
# print(a_left)
# print(a_right)
ans = 1
MOD = int(1e9) + 7
for i in range(N):
    l = max(t_left[i], a_left[i])
    r = min(t_right[i], a_right[i])
    if l > r:
        print(0)
        exit()
    else:
        ans *= (r - l + 1)
        ans %= MOD
print(ans % MOD)

Submission status: Accepted

Solution code:
n = int(raw_input())
t = map(int, raw_input().split())
a = map(int, raw_input().split())
mi = [1 for i in range(n)]
ma = [10**9 for i in range(n)]
for i, e in enumerate(t):
    if i == 0:
        mi[i] = e
        ma[i] = e
    else:
        if e > t[i-1]:
            mi[i] = e
            ma[i] = e
        elif e == t[i-1]:
            ma[i] = e
        else:
            print 0
            exit()

a = a[::-1]
mi = mi[::-1]
ma = ma[::-1]

for i, e in enumerate(a):
    if i == 0:
        if mi[i] <= e <= ma[i]:
            mi[i] = e
            ma[i] = e
        else:
            print 0
            exit()
    else:
        if e > a[i-1]:
            if mi[i] <= e <= ma[i]:
                mi[i] = e
                ma[i] = e
            else:
                print 0
                exit()
        elif e == a[i-1]:
            if mi[i] <= e <= ma[i]:
                ma[i] = e
        else:
            print 0
            exit()
res = 1
for i in range(n):
    res *= ma[i] - mi[i] + 1
    res = res % (10**9+7)
print res % (10**9+7)

Submission status: Accepted

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

fixed_peek_t = {}
unfixed_peek_t = {}
fixed_peek_a = {}
unfixed_peek_a = {}

lp = 0
for idx, height in enumerate(T):
    if lp < height:
        fixed_peek_t[idx] = height
    elif idx not in fixed_peek_t:
        unfixed_peek_t[idx] = height
    lp = height

lp = 0
for _idx, height in enumerate(reversed(A)):
    idx = N-1 - _idx
    if lp < height:
        fixed_peek_a[idx] = height
    elif idx not in fixed_peek_a:
        unfixed_peek_a[idx] = height
    lp = height

# print(unfixed_peek_t)
# print(fixed_peek_t)
# print(unfixed_peek_a)
# print(fixed_peek_a)

if max(A) != max(T):
    print(0)
else:
    failflag = 0
    for k,v in fixed_peek_t.items():
        if k not in fixed_peek_a:
            continue
        if v != fixed_peek_a[k]:
            failflag = 1
            break
        if k in unfixed_peek_a and v > unfixed_peek_a[k]:
            failflag = 1
            break
    for k,v in unfixed_peek_t.items():
        if k not in fixed_peek_a:
            continue
        if v < fixed_peek_a[k]:
            failflag = 1
            break
    if failflag == 0:
        cnt = 1
        for k,v in unfixed_peek_t.items():
            if k not in unfixed_peek_a:
                continue
            cnt *= min(v, unfixed_peek_a[k])
            cnt %= 10**9 + 7

        print(cnt)
    else:
        print(0)

Submission status: Accepted

Solution code:
import bisect
import sys
import math
import itertools
sys.setrecursionlimit(10000)

INF = float('inf')

# input macro
def i():
    return int(raw_input())
def ii():
    return map(int,raw_input().split(" "))
def s():
    return raw_input()
def ss():
    return raw_input().split(" ")
def slist():
    return list(raw_input())
def join(s):
    return ''.join(s)

#memoize macro
def memoize(f):
    cache = {}
    def helper(*args):
        if args not in cache:
            cache[(args)] = f(*args)
        return cache[args]
    return helper

nh=zip([1,0,-1,0],[0,1,0,-1])
mod=1000000007

###########
n=i()
t=ii()
a=ii()
just=[0]*n
ub=[INF]*n
lb=[0]*n

def justa(x,y):
    if just[x] in [0,y]:
        just[x]=y
    else:
        print '0'
        sys.exit()

justa(0,t[0])
justa(n-1,a[n-1])

for j in range(1,n):
    if t[j]!=t[j-1]:
        justa(j,t[j])
    else:
        ub[j]=t[j]
for j in reversed(range(0,n-1)):
    if a[j]!=a[j+1]:
        justa(j,a[j])
    else:
        ub[j]=min(ub[j],a[j])

ans=1
for j in range(1,n):
    if just[j]==0:
        ans=ans*ub[j]%mod
    elif just[j]<=ub[j]:
        ans=ans
    else:
        print '0'
        sys.exit()

print ans