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: Wrong Answer

Solution code:
# coding: utf-8
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 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*abs(i))%1000000007

print(x)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
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 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]<1:
            yama[len(yama)-i-1]=max(-check,yama[len(yama)-i-1])

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

print(x)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
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 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]<1:
            yama[len(yama)-i-1]=max(-check,yama[len(yama)-i-1])

x=1
for i in yama:
    if i<0:
        x*=abs(i)

print(x%1000000007)

Submission status: Wrong Answer

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):
    fixed = -1
    if i == 0 or T[i-1] < T[i]:
        fixed = T[i]
    if i == N-1 or A[i+1] < A[i]:
        if fixed > 0 and fixed != A[i]:
            print(0)
            exit()
        fixed = A[i]
    if fixed < 0:
        ans = (ans * min(T[i],A[i])) % MOD
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

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


Th = [[T[0]]]
th_prev = T[0]
for i in range(1, N):
    if T[i] == th_prev:
        Th.append([1, th_prev])
    else:
        Th.append([T[i]])
        th_prev = T[i]

A = A[::-1]
Ah = [[A[0]]]
ah_prev = A[0]
for i in range(1, N):
    if A[i] == ah_prev:
        Ah.append([1, ah_prev])
    else:
        Ah.append([A[i]])
        ah_prev = A[i]
        
Ah = Ah[::-1]
ans = 1
for i in range(N):
    if len(Ah[i]) == 1 and len(Th[i]) == 1:
        if Ah[i][0] != Th[i][0]:
            ans = 0   
    elif len(Ah[i]) == 1 and len(Th[i]) == 2:
        if Ah[i][0] > Th[i][1]:
            ans = 0
    elif len(Ah[i]) == 2 and len(Th[i]) == 1:
        if Ah[i][1] < Th[i][0]:
            ans = 0       
    elif len(Ah[i]) == 2 and len(Th[i]) == 2:
         ans *= (abs(Ah[i][1] - Th[i][1]) + 1)%(10**9+7)
            
print(ans%(10**9+7))

Submission status: Wrong Answer

Solution code:
from itertools import groupby
print(sum(1 for _ in groupby(input())) - 1)

Submission status: Wrong Answer

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()))
    for i in range(1, len(T)-1):
        t1, t2, a1, a2 = T[i], T[i-1], A[i], A[i+1]
        if t1 == t2 and a1 == a2:
            ans = (ans * min(t1, a1)) % D
        else:
            if t1 != t2 and a1 != a2 and t1 != a1 : ans = 0
            if t1 != t2 and a1 < t1 : ans = 0
            if a1 != a2 and t1 < a1 : ans = 0
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def main():
    d = 10 ** 9 + 7
    ans = 1
    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-1] > T[i] or A[i] < A[i+1] : ans = 0
            if T[i] != T[i-1] : ST[i] = T[i]
            if A[i] != A[i+1] : SA[i] = A[i]
    mt = ma = 0
    for i in range(L):
        ma = max(ma, SA[i])
        mt = max(mt, ST[L-i-1])
        if ST[i] and SA[i] and ST[i] != SA[i]:
            ans = 0
        if ST[i] and ST[i] < ma:
            ans = 0
        if SA[-i-1] and SA[-i-1] < mt:
            ans = 0
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def main():
    d = 10 ** 9 + 7
    ans = 1
    N = int(input())
    T = list(map(int, input().split()))
    A = list(map(int, input().split()))
    L = len(T)
    ST = [T[0]] + [0] * (L-2) + [T[-1] if T[-1] != T[-2] else 0]
    SA = [A[0] if A[0] != A[1] else 0] + [0] * (L-2) + [A[-1]]
    for i in range(0, 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]
    mt = ma = 0
    for i in range(L):
        ma = max(ma, SA[i])
        mt = max(mt, ST[L-i-1])
        if ST[i] and SA[i] and ST[i] != SA[i]:
            ans = 0
        if (ST[i] and ST[i] < ma) or (SA[-i-1] and SA[-i-1] < mt):
            ans = 0
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def main():
    d = 10 ** 9 + 7
    ans = 1
    N = int(input())
    T = list(map(int, input().split()))
    A = list(map(int, input().split()))
    L = len(T)
    ST = [T[0]] + [0] * (L-1)
    SA = [0] * (L-1) + [A[L-1]]
    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
        elif T[i] < T[i-1] or A[i] < A[i+1]:
            ans = 0
        else:
            if T[i] != T[i-1] : ST[i] = T[i]
            if A[i] != A[i+1] : SA[i] = A[i]
    if T[-1] != A[0] : ans = 0
    for i in range(L):
        if ST[i] and SA[i] and ST[i] != SA[i] : ans = 0
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def main():
    d = 10 ** 9 + 7
    ans = 1
    N = int(input())
    T = list(map(int, input().split()))
    A = list(map(int, input().split()))
    L = len(T)
    ST = [T[0]] + [0] * (L-1)
    SA = [0] * (L-1) + [A[L-1]]
    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
        if T[i] != T[i-1] : ST[i] = T[i]
        if A[i] != A[i+1] : SA[i] = A[i]
    if T[-1] != A[0] : ans = 0
    for i in range(L):
        if ST[i] and SA[i] and ST[i] != SA[i] : ans = 0
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=input()
p=lambda:map(int,raw_input().split())
l=p()
r=p()
def s(c,q):
 for i in range(n):b[i]+=c!=q[i];c=q[i]
b=[0]*n
s(0,r[::-1])
b=b[::-1]
s(0,l)
a=1
for x,s,t in zip(b,l,r):a*=1-~-(x>0)*~-min(s,t);a*=x<2or s==t;a%=10**9+7
print a

Submission status: Wrong Answer

Solution code:
n=input()
p=lambda:map(int,raw_input().split())
l=p()
r=p()
g=range(n)
b=[0]*n
def s(c,q):
 for i in g:b[i]|=c!=q[i];c=q[i]
s(0,r[::-1])
b=b[::-1]
s(0,l)
a=1
for i in g:
 a*=1-~-b[i]*~-min(l[i],r[i])
print a%(10**9+7)

Submission status: Wrong Answer

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 *= min(p[1], q[1]) % (10 ** 9 + 7)
print(P)

Submission status: Wrong Answer

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] and p[1] < q[1]):
		print(0)
		exit()
	elif(not p[0] and not q[0]):	
		P *= min(p[1], q[1]) % (10 ** 9 + 7)
print(P)

Submission status: Wrong Answer

Solution code:
import sys
n=int(input())
t=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]
a_max=max(a)
t_max=max(t)
if a_max != t_max:
    print(0)
    sys.exit()
if n==1:
    print(t_max)
    sys.exit()
i=0
sum=1
while t[i]!=t_max:
    if t[i]==t[i+1]:
        sum*=t[i]
    i+=1
if a[i]!=a_max:
    print(0)
    sys.exit()
j=len(a)-1
while a[j]!=a_max:
    if a[j]==a[j-1]:
        sum*=a[j]
    j-=1
if t[j]!=t_max:
    print(0)
    sys.exit()
n=0 if j==i else j-i-1
sum=(sum*(a_max**n))%((1e+9)+7)
print(int(sum))

Submission status: Wrong Answer

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(not p[0] and not q[0]):	
		P *= min(p[1], q[1]) % (10 ** 9 + 7)
print(P)

Submission status: Wrong Answer

Solution code:
def read():
	return [int(i) for i in input().split(" ")]
N = int(input())
T = read()
A = read()
L, unknowL = [0] * N, [0] * N
MaxT, MaxA = 0, 0
P = 1
for i in range(N):
	height = T[i]
	if(height > MaxT):
		MaxT = height 
		L[i] = height
	else:
		unknowL[i] = height
for i in range(N - 1, -1, -1):
	height = A[i]
	tempL = L[i]
	if(height > MaxA):
		if(tempL != 0 and tempL != height):
			print(0)
			exit()
		MaxA = height
		L[i] = height
		unknowL[i] = 0
	elif(unknowL[i] != 0):
		P *= min(unknowL[i], height) % (10 ** 9 +7)
print(P)

Submission status: Wrong Answer

Solution code:
N = int(raw_input())

T = map(int, raw_input().split())
A = map(int, raw_input().split())

ans = 1
mod = 10 ** 9 + 7

if T[N - 1] != A[0]:
    ans = 0

else:
    for i in xrange(1, N - 1):
        if T[i] > A[i]:
            ans = (ans * A[i]) % mod
        elif T[i] < A[i]:
            ans = (ans * T[i]) % mod

print ans % mod

Submission status: Wrong Answer

Solution code:
def read():
	return [int(i) for i in input().split(" ")]
N = int(input())
T = read()
A = read()
L, unknowL = [0] * N, [0] * N
MaxT, MaxA = 0, 0
P = 1
for i in range(N):
	height = T[i]
	if(height > MaxT):
		MaxT = height 
		L[i] = height
	else:
		unknowL[i] = height
for i in range(N-1, -1, -1):
	height = A[i]
	tempL = L[i]
	if(height > MaxA):
		if(tempL != 0 and tempL != height):
			print(0)
			exit()
		MaxA = height
		L[i] = height
		unknowL[i] = 0
	elif(unknowL[i] != 0):
		P *= min(unknowL[i], height)
print(P % (10**9 +7))

Submission status: Wrong Answer

Solution code:
import sys
n=int(input())
t=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]
a_max=max(a)
t_max=max(t)
if a_max != t_max:
    print(0)
    sys.exit()
if n==1:
    print(t_max)
    sys.exit()
i=0
sum=1
while t[i]!=t_max:
    if t[i]==t[i+1]:
        sum*=t[i]
    i+=1
j=len(a)-1
while a[j]!=a_max:
    if a[j]==a[j-1]:
        sum*=a[j]
    j-=1
n=0 if j==i else j-i-1
sum=(sum*(a_max**n))%((1e+9)+7)
print(int(sum))

Submission status: Wrong Answer

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
	else:
		res*=min(l[i],r[i])
		res%=1000000007
print(res)

Submission status: Wrong Answer

Solution code:
def read():
	return [int(i) for i in input().split(" ")]
N = int(input())
T = read()
A = read()
L, unknowL = [0] * N, [0] * N
MaxT, MaxA = 0, 0
P = 1
for i in range(N):
	height = T[i]
	if(height > MaxT):
		MaxT = height 
		L[i] = height
	else:
		unknowL[i] = height
for i in range(N-1, -1, -1):
	height = A[i]
	tempL = L[i]
	if(height > MaxA):
		if(tempL != 0 and tempL != height):
			print(tempL, height)
			print(0)
			exit()
		MaxA = height
		L[i] = height
		unknowL[i] = 0
	elif(unknowL[i] != 0):
		P *= min(unknowL[i], height)
print(P % (10**9 +7))

Submission status: Wrong Answer

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
	else:
		res*=min(l[i],r[i])
		res%=1000000007
print(res)

Submission status: Wrong Answer

Solution code:
def read():
	return [int(i) for i in input().split(" ")]
N = int(input())
T = read()
A = read()
L, unknowL = [0] * N, [0] * N
MaxT, MaxA = 0, 0
P = 1
for i in range(N):
	height = T[i]
	if(height > MaxT):
		MaxT = height 
		L[i] = height
	else:
		unknowL[i] = height
for i in range(N-1, -1, -1):
	height = A[i]
	tempL = L[i]
	if(height > MaxA):
		if(tempL != 0 and tempL != height):
			print(tempL, height)
			print(0)
			exit()
		MaxA = height
		L[i] = height
		unknowL[i] = 0
	else:
		unknowL[i] = min(unknowL[i], height)
P = 1 
for i in unknowL:
	if(i != 0):
		P *= i	
print(P % (10**9 +7))

Submission status: Wrong Answer

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[-2]):
	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
	else:
		res*=min(l[i],r[i])
		res%=1000000007
print(res)

Submission status: Wrong Answer

Solution code:
def read():
	return [int(i) for i in input().split(" ")]
N = int(input())
T = read()
A = read()
L, unknowL = [0] * N, [0] * N
MaxT, MaxA = 0, 0
P = 1
for i in range(N):
	height = T[i]
	if(height > MaxT):
		MaxT = height 
		L[i] = height
	else:
		unknowL[i] = height
print(L)
print(unknowL)
for i in range(N-1, -1, -1):
	height = A[i]
	tempL = L[i]
	if(height > MaxA):
		if(tempL != 0 and tempL != height):
			print(tempL, height)
			print(0)
			exit()
		MaxA = height
		L[i] = height
		unknowL[i] = 0
	else:
		unknowL[i] = min(unknowL[i], height)
P = 1 
for i in unknowL:
	if(i != 0):
		P *= i	
print(P % (10**9 +7))

Submission status: Wrong Answer

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(r[1]!=l[-2]):
	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
	else:
		res*=min(l[i],r[i])
		res%=1000000007
print(res)

Submission status: Wrong Answer

Solution code:
import sys
n=int(input())
t=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]
a_max=max(a)
t_max=max(t)
if a_max != t_max:
    print(0)
    sys.exit()
i=0
sum=1
while t[i]!=t_max:
    if t[i]==t[i+1]:
        sum*=t[i]
    i+=1
j=len(a)-1
while a[j]!=a_max:
    if a[j]==a[j-1]:
        sum*=a[j]
    j-=1
sum=(sum*(a_max**(j-i-1)))%((1e+9)+7)
print(int(sum))

Submission status: Wrong Answer

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[-1] != T[0]:
    print 0
    exit(0)
print p

Submission status: Wrong Answer

Solution code:
def main():
    d = 10 ** 9 + 7
    ans = 1
    N = int(input())
    T = list(map(int, input().split()))
    A = list(map(int, input().split()))
    for i in range(0, len(T)//2):
        if T[-(i+1)] != A[i] : return 0
    for i in range(1, len(T) - 1):
        if T[i] < T[i-1] or A[i] < A[i+1] : return 0
        if not(T[i] > T[i-1] or A[i] > A[i+1]):
            ans *= min(T[i], A[i])
    return ans % d
    
if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

N = int(raw_input())
T = map(int, raw_input().split())
A = map(int, raw_input().split())
T = np.array(T)
A = np.array(A)

arr = np.ones((len(A),))
res = 1
rest = np.zeros((len(A),))
flag = 0
if max(A) != max(T):
    res = 0
else:
    for t in range(T.shape[0]):
        if t > 0 and T[t] == T[t-1] and t != T.shape[0]-1:
            arr[t] = T[t]
        elif t != T.shape[0]-1:
            rest[t] = T[t]
    for a in range(A.shape[0])[::-1]:
        if rest[a]>1 and A[a]<rest[a]:
            flag = 1
            break
        elif a < A.shape[0]-1 and a != 0 and arr[a] != 1:
            if A[a] > A[a+1]:
                arr[a] = 1
            elif A[a] < arr[a]:
                arr[a] = A[a]
    if flag != 1:
        for j in arr:
            res *= j

if flag != 1:
    res = res % ((10**9) + 7)

print int(res)

Submission status: Wrong Answer

Solution code:
import sys
n=int(input())
t=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]
a_max=max(a)
t_max=max(t)
if a_max != t_max:
    print(0)
    sys.exit()
i=0
sum=1
while t[i]!=t_max:
    if t[i]==t[i+1]:
        sum*=t[i]
j=len(a)-1
while a[j]!=a_max:
    if a[j]==a[j-1]:
        sum*=a[j]
sum=sum*(a_max**(n-(i+j+1)))%((1e+9)+7)
print(int(sum))

Submission status: Wrong Answer

Solution code:
def main():
    d = 10 ** 9 + 7
    ans = 1
    N = int(input())
    T = list(map(int, input().split()))
    A = list(map(int, input().split()))
    for i in range(0, len(T)//2+1):
        if T[-(i+1)] != A[i] : return 0
    for i in range(1, len(T) - 1):
        if not(T[i] > T[i-1] or A[i] > A[i+1]):
            ans *= min(T[i], A[i])
    return ans % d
    
if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
def isAsc(a):
    f=True
    for i in range(len(a)-1):
        if a[i+1]<a[i]:
            f=False
            break
    return f
    
def isDes(a):
    f=True
    for i in range(len(a)-1):
        if a[i+1]>a[i]:
            f=False
            break
    return f

n=int(input())
t=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]
ans=1
if isAsc(a) or isDes(t) or max(t)==max(a):
    for i in range(n-1):
        if t[i]==t[i+1] and a[i]==a[i+1]:
            ans*=min(t[i],a[i])
        if t[i+1]>t[i] and t[i+1]>a[i+1] or a[i]>a[i+1] and a[i]>t[i]:
            ans=0
print(ans%(10**9+7))

Submission status: Wrong Answer

Solution code:
def read():
	return [int(i) for i in input().split(" ")]
N = int(input())
T = read()
A = read()
L, unknowL = [], []
tempT, tempA = 0, 0
for i in range(N):
	if(T[i] > tempT):
		tempT = T[i]
		L.append(tempT)
		unknowL.append(0)
	else:
		unknowL.append(T[i])
		L.append(0)
for i in range(N-1, -1, -1):
	val = A.pop()
	if(val > tempA):
		if(L[i] != 0 and L[i] != val):
			print(0)
			exit()
		tempA = L[i] = val 
		unknowL[i] = 0
	else:
		unknowL[i] = min(unknowL[i], val)
P = 1 
for i in unknowL:
	if(i != 0):
		P *= i	
print(P % (10**9 +7))

Submission status: Wrong Answer

Solution code:
n = int(input())
T = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]

result = [0] * n
result[0] = T[0]
result[-1] = A[-1]

def mn(nums, x):
	left = right = 0
	for i in range(x-1, -1, -1):
		if nums[i] != 0:
			left = nums[i]
			break
	for i in range(x+1, len(nums)):
		if nums[i] != 0:
			right = nums[i]
			break
	return min(left, right)
	
def f():
	for i in range(1, len(T)):
		if T[i] > T[i-1]:
			result[i] = T[i]
		elif T[i] < T[i-1]:
			return 0
			
	for i in range(len(A)-2, -1, -1):
		if A[i] > A[i+1]:
			result[i] = A[i]
		elif A[i] < A[i+1]:
			return 0
		if result[i] != 0 and (result[i]>A[i+1] and result[i]!=A[i] or result[i]<=A[i+1] and A[i]!=A[i+1]):
			return 0

	count = 1
	tmp = 0
	for i in range(1, n):
		if (result[i] == 0):
			if result[i-1] == 0:
				count *= tmp
			else:
				tmp = mn(result, i)
				count *= tmp
			
	return count % (10**9+7)

print(f())

Submission status: Wrong Answer

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

if max(T) != max(A):
    print 0
else:
    if N == 1:
        print 1
    else:
        H = [None]*N
        p = -1
        for i in range(N):
            if p != T[i]:
                H[i] = T[i]
                p = T[i]
        p = -1
        for i in range(N)[::-1]:
            if p != A[i]:
                if H[i] != None and H[i] != A[i]:
                    print 0
                    exit()
                H[i] = A[i]
                p = A[i]
        a = 0
        m = -1
        count = 1
        for i in range(N):
            if m == -1 and H[i] != None:
                m = H[i]
            elif m != -1 and H[i] == None:
                a+=1
            elif m != -1 and H[i] != None:
                M = H[i]
                for j in range(a):
                    count = (count*min(M,m))%(10**9+7)
                a = 0
                m = M
        print count

Submission status: Wrong Answer

Solution code:
N = int(input())
T = list(map(int, input().split()))
A = list(map(int, input().split()))
h = [0 for i in range(N)]
if T[-1] != A[0]:
	print(0)
	exit()
else:
	h[0] = T[0]
	h[-1] = A[-1]
	for i in range(1,N):
		if T[i] > T[i-1]:
			if h[i] > 0 and T[i] != h[i]:
				print(0)
				exit()
				h[i] = T[i]
		else:
			if h[i] < 0:
				h[i] = max(h[i],-T[i])
			elif h[i] == 0:
				h[i] = -T[i]
			elif h[i] > T[i]:
				print(0)
				exit()
		if A[-i-1] > A[-i]:
			if h[-i-1] > 0 and A[-i-1] != h[-i-1]:
				print(0)
				exit()
			else:
				h[-i-1] = A[-i-1]
		else:
			if h[-i-1] < 0:
				h[-i-1] = max(h[-i-1],-A[-i-1])
			elif h[-i-1] == 0:
				h[-i-1] = -A[-i-1]
			elif h[i] > A[-i-1]:
				print(0)
				exit()
a = 1
for n in h:
	if a > 10**9+7:
		a %= 10**9+7
	if n < -1:
		a *= -n
print(a%(10**9+7))

Submission status: Wrong Answer

Solution code:
def read():
	return [int(i) for i in input().split(" ")]
N = int(input())
T = read()
A = read()
L, unknowL = [], []
tempT, tempA = 0, 0
for i in range(N):
	if(T[i] > tempT):
		tempT = T[i]
		L.append(tempT)
		unknowL.append(0)
	else:
		unknowL.append(T[i])
		L.append(0)
for i in range(N):
	val = A.pop()
	if(val > tempA):
		if(L[i] != 0 and L[i] != val):
			print(0)
			exit()
		tempA = L[i] = val 
		unknowL[i] = 0
	else:
		unknowL[i] = min(unknowL[i], val)
P = 1 
for i in unknowL:
	if(i != 0):
		P *= i	
print(P % (10**9 +7))

Submission status: Wrong Answer

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

Z = []
Zmax = 0
for i in range(N):
        Z.append(min(T[i],A[i]))
        if Z[i] < Z[i-1] and Z[i-1] > Zmax:
                Zmax = Z[i-1]

sum = 1
offset = []
if Zmax < max(T[-1],A[0]):
        print(0)
else:
        for i in range(1,N-1):
                if not (Z[i] > Z[i-1] or Z[i] > Z[i+1]):
                        offset.append(i)
        for j in offset:
                sum *= Z[j]
        print(sum%(1000000000 + 7))

Submission status: Wrong Answer

Solution code:
n = int(input())
T = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]

result = [0] * n
result[0] = T[0]
result[-1] = A[-1]

def mn(nums, x):
	left = right = 0
	for i in range(x-1, -1, -1):
		if nums[i] != 0:
			left = nums[i]
			break
	for i in range(x+1, len(nums)):
		if nums[i] != 0:
			right = nums[i]
			break
	return min(left, right)
	
def f():
	for i in range(1, len(T)):
		if T[i] > T[i-1]:
			result[i] = T[i]
		elif T[i] < T[i-1]:
			return 0
			
	for i in range(len(A)-2, -1, -1):
		if A[i] > A[i+1]:
			result[i] = A[i]
		elif A[i] < A[i+1]:
			return 0
		if result[i] != 0 and (result[i]>A[i+1] and result[i]!=A[i] or result[i]<=A[i+1] and A[i]!=A[i+1]):
			return 0

	count = 1
	for i in range(n):
		if (result[i] == 0):
			count *= mn(result, i)
			
	return count % (10**9+7)

print(f())

Submission status: Wrong Answer

Solution code:
N = int(input())
T = list(map(int, input().split()))
A = list(map(int, input().split()))
h = [0 for i in range(N)]
if T[-1] != A[0]:
	print(0)
	exit()
else:
	h[0] = T[0]
	h[-1] = A[-1]
	for i in range(1,N):
		if T[i] > T[i-1]:
			if h[i] > 0 and T[i] != h[i]:
				print(0)
				exit()
				h[i] = T[i]
		else:
			if h[i] < 0:
				h[i] = max(h[i],-T[i])
			elif h[i] == 0:
				h[i] = -T[i]
			else:
				print(0)
				exit()
		if A[-i-1] > A[-i]:
			if h[-i-1] > 0 and A[-i-1] != h[-i-1]:
				print(0)
				exit()
			else:
				h[-i-1] = A[-i-1]
		else:
			if h[-i-1] < 0:
				h[-i-1] = max(h[-i-1],-A[-i-1])
			elif h[-i-1] == 0:
				h[-i-1] = -A[-i-1]
			else:
				print(0)
				exit()
a = 1
for n in h:
	if a > 10**9+7:
		a %= 10**9+7
	if n < -1:
		a *= -n
print(a%(10**9+7))

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    takahashi = list(map(int, input().split()))
    aoki = list(map(int, input().split()))

    result = [-1 for i in range(N)]

    if max(takahashi) != max(aoki):
        print(0)
        return

    takahasi_previous_value = -1
    aoki_previous_value = -1
    for i, (takahashi_value, aoki_value) in enumerate(zip(takahashi, reversed(aoki))):
        if takahasi_previous_value < takahashi_value:
            if result[i] != -1 and result[i] != takahashi_value:
                print(0)
                return
            result[i] = takahashi_value
            takahasi_previous_value = takahashi_value
        index = N-i-1
        if aoki_previous_value < aoki_value:
            if result[index] != -1 and result[index] != aoki_value:
                print(0)
                return
            result[index] = aoki_value
            aoki_previous_value = aoki_value

    min_value = -1
    counter = 0
    r_value = 1
    for i in range(N):
        if result[i] == -1:
            counter += 1
        elif counter>0:
            mlt = min(min_value, result[i])
            r_value *= mlt**counter
            min_value = result[i]
            counter = 0
        else:
            min_value = result[i]
    print(r_value%(10**9+7))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    d = 10 ** 9 + 7
    ans = 1
    N = int(input())
    T = list(map(int, input().split()))
    A = list(map(int, input().split()))
    for i in range(0, len(T)//2):
        if T[-(i+1)] != A[i] : return 0
    for i in range(1, len(T) - 1):
        if T[i] > T[i-1] or A[i] > A[i+1]:
            pass
        else:
            ans *= min(T[i], A[i])
    return ans % d
    
if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
mtNum = input()
Tmemo = []
map(Tmemo.append, map(long, raw_input().split(' ')))
Amemo = []
map(Amemo.append, map(long, raw_input().split(' ')))
mulMemo = []
DEV = 10**9 + 7

result = 1

if max(Tmemo) != max(Amemo):
	result = 0
else:
	maxHeiT = Tmemo[0]
	for mtHeiT, mtHeiTT, mtHeiA, mtHeiAA in zip(Tmemo, [Tmemo[1:], Tmemo[-1]], Amemo ,[Amemo[1:], Amemo[-1]]):
		if maxHeiT != mtHeiT:
			maxHeiT = mtHeiT
			if mtHeiT > mtHeiA:
				result = 0
				break
		elif mtHeiA != mtHeiAA:
			if mtHeiT < mtHeiA:
				result = 0
				break
		elif mtHeiT < mtHeiA:
			result *= mtHeiT
		else:
			result *= mtHeiA
		if result > DEV:
			result %= DEV

if result > 0 and Tmemo == Amemo[::-1]:
	result = 1

print result