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:
def read():
	return [int(i) for i in input().split(" ")]
N = int(input())
T = read()
A = read()
L = [0] * N
unknowL = [0] * N
tempT, tempA = 0, 0
for i in range(N):
	if(T[i] > tempT):
		tempT = L[i] = T[i]
	else:
		unknowL[i] = T[i]
for i in range(N-1, -1, -1):
	if(A[i] > tempA):
		if(L[i] != 0 and L[i] != A[i]):
			print(0)
			exit()
		tempA = L[i] = A[i]
		unknowL[i] = 0
	else:
		unknowL[i] = min(unknowL[i], A[i])
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()))
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 T[i] < h[i]:
				print(0)
				exit()
			elif h[i] == 0:
				h[i] = -T[i]
		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 A[-i-1] < h[-i-1]:
				print(0)
				exit()
			elif h[-i-1] == 0:
				h[-i-1] = -A[-i-1]
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:
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):
			print(mn(result, i))
			count *= mn(result, i)
			
	return count % (10**9+7)

print(f())

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:], Amemo ,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

Submission status: Wrong Answer

Solution code:
def getPossibilities(list_one, list_two):
    if len(list_one) <= 1:
        return 1

    maxValOne = max(list_one[0], list_one[-1])
    maxValTwo = max(list_two[0], list_two[-1])

    possible = 0
    onePoss = 0
    twoPoss = 0

    for i in xrange(1, len(list_one) - 1):
        if list_one[i] > maxValOne:
            maxValOne = list_one[i]

        if list_two[i] > maxValTwo:
            maxValTwo = list_two[i]

        if list_one[i] != list_one[i - 1]:
            onePoss = 0
        else:
            onePoss = list_one[i]

        if list_two[i] != list_two[i + 1]:
            twoPoss = 0
        else:
            twoPoss = list_two[i]

        if possible == 0:
            possible = min(onePoss, twoPoss)
        else:
            if onePoss > 0 and twoPoss > 0:
                possible *= min(onePoss, twoPoss)

    if maxValOne != maxValTwo:
        return 0

    return possible % (10**9 + 7)


if __name__ == "__main__":
    junk = raw_input()
    list_one = list(map(int, raw_input().split()))
    list_two = list(map(int, raw_input().split()))
    print getPossibilities(list_one, list_two)

Submission status: Wrong Answer

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 t[i] < a[i]:
                    ans = 0
                    break
                flag1 = True
        else:
            if a[i+1] < a[i]:
                ans = 0
                break
            flag1 = True
        if i < n-1:
            if a[i+1] < a[i]:
                if a[i] > t[i]:
                    ans = 0
                    break
                flag2 = True
        else:
            if t[i-1] < t[i]:
                ans = 0
                break
            flag2  = True
        #print m
        
        if not(flag1 or flag2):
            ans *= m
            ans %= M
else:
    ans = 0
print ans,

Submission status: Wrong Answer

Solution code:
def read():
	return [int(i) for i in input().split(" ")]
N = int(input())
T = read()
A = read()
L = [0] * N
unknowL = [0] * N
tempT, tempA = 0, 0
for i in range(N):
	if(T[i] > tempT):
		tempT = L[i] = T[i]
	else:
		unknowL[i] = T[i]
for i in range(N-1, -1, -1):
	if(A[i] > tempA):
		if(L[i] != 0 and L[i] != A[i]):
			print(0)
			exit()
		tempA = L[i] = A[i]
		unknowL[i] = 0
	else:
		unknowL[i] = min(unknowL[i], A[i])
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()))

tt = [t[0]]
aa = [a[-1]]
for i in range(1,n):
    if t[i] > t[i-1]:
        tt.append(t[i])
    else:
        tt.append(0)
    if a[n-i-1] > a[n-i]:
        aa.append(a[n-i-1])
    else:
        aa.append(0)
aa.reverse()

def f():
    m = []
    for i in range(n):
        if tt[i] > 0 and aa[i] > 0:
            if tt[i] == aa[i]:
                m.append(tt[i])
            else:
                return 0
        elif tt[i] == 0 and aa[i] == 0:
            m.append(0)
        elif tt[i] == 0 and aa[i] > 0:
            m.append(aa[i])
        elif tt[i] > 0 and aa[i] == 0:
            m.append(tt[i])
    ans = 1
    for i in range(n):
        if m[i] == 0:
            ans = ans * min(t[i], a[i])
    nnnn = divmod(ans, 10**9+7)
    ans = nnnn[1]
    return ans

print (f())

Submission status: Wrong Answer

Solution code:
n = input()
t = map(int, raw_input().split())
a = map(int, raw_input().split())

c = [False] * n
d = [False] * n

c[0], d[-1] = True, True
m = [0] * n
m[0] = t[0]
m[-1] = a[-1]
i = 1
if n > 1:
    if a[0] != a[1] and a[0] != t[0]:
        print 0
        exit()
    elif t[-1] != t[-2] and t[-1] != a[-1]:
        print 0
        exit()
else:
    if a[0] != t[0]:
        print 0
        exit()

while i < n:
    if t[i] < t[i-1] or a[i] > a[i-1]:
        print 0
        exit()
    if t[i] != t[i-1]:
        c[i] = True
        m[i] = t[i]
    if a[i] != a[i-1]:
        d[i-1] = True
        m[i-1] = a[i-1]
    i += 1

for i in xrange(n):
    if c[i] and d[i]:
        if t[i] != a[i]:
            print 0
            exit()
    if not (c[i] or d[i]):
        m[i] = min(a[i], t[i])

# print c
# print m
p = 1
for cc, dd, mm in zip(c,d , m):
    if cc or dd:
        continue
    p *= mm
    p %= (10**9)+7
print p

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()
			else:
				h[i] = T[i]
		else:
			if h[i] < 0:
				h[i] = max(h[i],-T[i])
			elif T[i] < h[i]:
				print(0)
				exit()
			elif h[i] == 0:
				h[i] = -T[i]
		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 A[-i-1] < h[-i-1]:
				print(0)
				exit()
			elif h[-i-1] == 0:
				h[-i-1] = -A[-i-1]
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:
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()
			else:
				h[i] = T[i]
		else:
			if h[i] < 0:
				h[i] = max(h[i],-T[i])
			elif T[i] < h[i]:
				print(0)
				exit()
			elif h[i] == 0:
				h[i] = -T[i]
		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 A[-i-1] < h[-i-1]:
				print(0)
				exit()
			elif h[-i-1] == 0:
				h[-i-1] = -A[-i-1]
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:
mtNum = input()
Tmemo = []
map(Tmemo.append, map(long, raw_input().split(' ')))
Amemo = []
map(Amemo.append, map(long, raw_input().split(' ')))
mulMemo = []
DEV = long(10**9 + 7)

result = long(1)

if max(Tmemo) != max(Amemo):
	result = 0
elif Tmemo == Amemo[::-1]:
	result = 1
else:
	maxHeiT = Tmemo[0]
	for mtHeiT, mtHeiTT, mtHeiA, mtHeiAA in zip(Tmemo, Tmemo[1:], Amemo ,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


result %= DEV

print result

Submission status: Wrong Answer

Solution code:
n_mountain = int(input())
changed = [False for i in range(n_mountain)]

t_report = list(map(int, input().split()))
a_report = list(map(int, input().split()))

if max(t_report) != max(a_report):
	print(0)
else:
	for i in range(1, n_mountain):
		if t_report[i - 1] != t_report[i]:
			changed[i] = True
		if a_report[i - 1] != a_report[i]:
			changed[i - 1] = True
	changed[0] = changed[n_mountain -1 ] = True
	okasii = False
	for i in range(1, n_mountain):
		if (t_report[i - 1] != t_report[i] and a_report[i - 1] != a_report[i]) or(t_report[i - 1] != t_report[i] and a_report[i + 1] != a_report[i]):
			if  t_report[i] != a_report[i]:
				okasii = True
	if okasii:
		print(0)
	else:
		min_mountain = [min(a_report[i], t_report[i]) for i in range(n_mountain)]
		ans = 1
		for i in range(n_mountain):
			if changed[i] == False:
				ans *= min_mountain[i]
				ans %= 1000000007
				
		print(ans)

Submission status: Wrong Answer

Solution code:
import math as mt

import operator as op
def ncr(n, r):
    r = min(r, n-r)
    if r == 0: return 1
    numer = reduce(op.mul, xrange(n, n-r, -1))
    denom = reduce(op.mul, xrange(1, r+1))
    return numer//denom

N = input()
T = map(lambda x:int(x), raw_input().split())
A = map(lambda x:int(x), raw_input().split())

check = [False for x in range(N)]
check[0] = True
check[N - 1] = True

ans = N - 2
n = max(T)

for i in range(1, len(T) - 1):
    if T[i - 1] < T[i]:
        global LastT
        LastT = T[i]
        check[i] = True
    if A[i + 1] < A[i]:
        global LastA
        LastA = A[i]
        check[i] = True

if max(T) != max(A):
    print 0
else:
    if (((N % (10 ** 9 + 7) - sum(check) % (10 ** 9 + 7)) % (10 ** 9 + 7)) ** 2) % (10 ** 9 + 7) == 0:
        print 1
    else:
        print (((N % (10 ** 9 + 7) - sum(check) % (10 ** 9 + 7)) % (10 ** 9 + 7)) ** 2) % (10 ** 9 + 7)
    #print ncr((n % (10 ** 9 + 7) - (N % (10 ** 9 + 7) - sum(check) % (10 ** 9 + 7))) % (10 ** 9 + 7), ((N - sum(check)) % (10 ** 9 + 7)))

Submission status: Wrong Answer

Solution code:
n_mountain = int(input())
changed = [False for i in range(n_mountain)]

t_report = list(map(int, input().split()))
a_report = list(map(int, input().split()))

if max(t_report) != max(a_report):
	print(0)
else:
	for i in range(1, n_mountain):
		if t_report[i - 1] != t_report[i]:
			changed[i] = True
		if a_report[i - 1] != a_report[i]:
			changed[i - 1] = True
	changed[0] = changed[n_mountain -1 ] = True
	okasii = False
	for i in range(1, n_mountain):
		if t_report[i - 1] != t_report[i] and a_report[i + 1] != a_report[i]:
			if  t_report[i] != a_report[i]:
				okasii = True
	if okasii:
		print(0)
	else:
		min_mountain = [min(a_report[i], t_report[i]) for i in range(n_mountain)]
		ans = 1
		for i in range(n_mountain):
			if changed[i] == False:
				ans *= min_mountain[i]
				ans %= 1000000007
				
		print(ans)

Submission status: Wrong Answer

Solution code:
import sys
n = input()
t = map(int, raw_input().split())
a = map(int, raw_input().split())
ans = 0
if max(t) != max(a):
    print ans
    sys.exit()
supermax = max(t)
flagt = 0
flaga = 0
for i in range(n):
    if flagt == 0 and t[i] == supermax:
        flagt = i
    if flaga == 0 and a[n-1-i] == supermax:
        flaga = n-1-i
if flagt > flaga:
    print ans
    sys.exit()


ans = 1
tmx = t[0]
dummy_a = a[0]
amx = a[n-1]
dummy_t = t[n-1]
if (n%2)==0:
    loop = n/2-1
else:
    loop = n/2
for i in range(loop):
    if t[i+1] <= a[i+1]:
        if t[i+1] == tmx:
            ans *= t[i+1]
    elif a[i+1] < dummy_a and i< t.index(dummy_a) :
        print 0
        sys.exit()
    tmx = t[i+1]
    dummy_a = a[i+1]
    ans %= 1000000007
    if a[(n-1)-(i+1)] <= t[(n-1)-(i+1)]:
        if a[(n-1)-(i+1)] == amx:
            ans*= a[(n-1)-(i+1)]
    elif t[(n-1)-(i+1)] < dummy_t and n-1-i > a.index(dummy_t):
        print 0
        sys.exit()
    amx = a[(n-1)-(i+1)]
    dummy_t = t[(n-1)-(i+1)]
    ans %= 1000000007
print ans

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

    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:
            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:
mtNum = input()
Tmemo = []
map(Tmemo.append, map(long, raw_input().split(' ')))
Amemo = []
map(Amemo.append, map(long, raw_input().split(' ')))
mulMemo = []
DEV = long(10**9 + 7)

result = long(1)

if max(Tmemo) != max(Amemo):
	result = 0
elif Tmemo == Amemo[::-1]:
	result = 1
else:
	maxHeiT = Tmemo[0]
	for mtHeiT, mtHeiTT, mtHeiA, mtHeiAA in zip(Tmemo, Tmemo[1:], Amemo ,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

print result

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 H[i] != None and H[i] != A[i]:
                    print 0
                    exit()
            if p != A[i]:
                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]
                if a != 0:
                    count = (count*(min(M,m))**a)%(10**9+7)
                a = 0
                m = M
        print count

Submission status: Wrong Answer

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()]
    if xs[-1] != ys[0]:
        print(0)
        return
    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(h.ub - h.lb + 1 for h in hs)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n_mountain = int(input())
changed = [False for i in range(n_mountain)]

t_report = list(map(int, input().split()))
a_report = list(map(int, input().split()))

if max(t_report) != max(a_report):
	print(0)
else:
	for i in range(1, n_mountain):
		if t_report[i - 1] != t_report[i]:
			changed[i] = True
		if a_report[i - 1] != a_report[i]:
			changed[i - 1] = True
	changed[0] = changed[n_mountain -1 ] = True
	okasii = False
	for i in range(1, n_mountain):
		if t_report[i - 1] != t_report[i] and a_report[i - 1] != a_report[i]:
			if  t_report[i] != a_report[i]:
				okasii = True
	if okasii:
		print(0)
	else:
		min_mountain = [min(a_report[i], t_report[i]) for i in range(n_mountain)]
		ans = 1
		for i in range(n_mountain):
			if changed[i] == False:
				ans *= min_mountain[i]
				ans %= 1000000007
				
		print(ans)

Submission status: Wrong Answer

Solution code:
N = int(raw_input())
T = raw_input().split(' ')
A = raw_input().split(' ')
check = False
yama = [0] * N
high_think = [0] * N
max_numT = 0
max_numA = 0
sum_num = 1
i = -1
for i in range(0, N/2):
  temp = int(T[i])
  if max_numT < temp:
    max_numT = temp
    yama[i] = temp
  high_think[i] = max_numT
  temp = int(A[-1 - i])
  if max_numA < temp:
    max_numA = temp
    yama[-1 - i] = temp
  high_think[-1-i] = max_numA

if (N%2 == 1):
  i = i + 1
  if max_numT < int(T[i]):
    yama[i] = int(T[i])
  elif max_numA < int(A[-1-i]):
    yama[i] = int(A[-1-i])
  else:
    high_think[i] = min(max_numT, max_numA)

# print yama
# print high_think
max_numT = 0
max_numA = 0

for i in range(0, N):
  temp = int(T[i])
  if max_numT < temp:
    max_numT = temp
    if yama[i] > max_numT:
      check = True
  temp = int(A[-1-i])
  if max_numA < temp:
    max_numA = temp
    if yama[-1-i] > max_numA:
      check = True
  if yama[i] == 0:
    sum_num *= high_think[i]


if (T[-1] == A[0]) and not check:
  print sum_num % (10**9 + 7)
else:
  print "0"

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 T[i] < h[i]:
				print(0)
				exit()
			else:
				h[i] = T[i]
		else:
			if h[i] < 0:
				h[i] = max(h[i],-T[i])
			elif T[i] < h[i]:
				print(0)
				exit()
			elif h[i] == 0:
				h[i] = -T[i]
		if A[-i-1] > A[-i]:
			if 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 A[-i-1] < h[-i-1]:
				print(0)
				exit()
			elif h[-i-1] == 0:
				h[-i-1] = -A[-i-1]
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:
N=int(input())
T=input().split()
A=input().split()
for i in range(N):
	T[i]=int(T[i])
	A[i]=int(A[i])
if max(T)!=max(A):
	print(0)
	exit(0);
V=[min(T[i], A[i]) for i in range(N)]
V[0]=1
for i in range(N-1):
	if T[i]<T[i+1]:
		V[i+1]=1
V[N-1]=1
for i in range(N-1):
	if A[i]>A[i+1]:
		V[i]=1
ans=1
for i in range(N):
	ans*=V[i]
print(ans%1000000007)

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


print p

Submission status: Wrong Answer

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

n = stdin.readline()
n = n.strip().split(" ")
N = int(n[0])
n = stdin.readline()
n = n.strip().split(" ")
T = []
for t in n:
	T.append(int(t))
T = np.array(T)
n = stdin.readline()
n = n.strip().split(" ")
A = []
for a in n:
	A.append(int(a))
A = np.array(A)

arr = np.ones((len(A),))
res = 1

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]
	for a in range(A.shape[0])[::-1]:
		if A[a] > T[a]:
			res = 0
			break
		if 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]
			# elif A[a] == arr[a]:
			# 	pass
			# elif A[a] < arr[a]:
			# 	arr[a] = A[a]
	for j in arr:
		res *= j

res = res % ((10**9) + 7)
print int(res)

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
elif Tmemo == Amemo[::-1]:
	result = 1
else:
	maxHeiT = Tmemo[0]
	for mtHeiT, mtHeiTT, mtHeiA, mtHeiAA in zip(Tmemo, Tmemo[1:], Amemo ,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

print result

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()))
    SH = [-1] * len(T)
    if T[-1] != A[0] : return 0
    h = T[0]
    c = 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])
        c += 1
    return ans % d
    
if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
def chk_cake():
    for i in range(0, cake_kind):
        if cakes[i] > 0:
            return True
    return False


def get_max(last_cake):
    max_num = 0
    max_inx = last_cake
    for i in range(0, cake_kind):
        if max_num < cakes[i] and i != last_cake:
            max_num = cakes[i]
            max_inx = i
    return max_inx

counter = 0
n = int(raw_input())
choku = map(int, raw_input().split())
aoki = map(int, raw_input().split())

if n == 1:
    if choku[0] == aoki[0]:
        print choku[0]
    else:
        print 0

elif choku[-1] != aoki[0]:
    print 0

else:
    print 1

Submission status: Wrong Answer

Solution code:
n = input()
t = map(int, raw_input().split())
a = map(int, raw_input().split())

c = [False] * n
c[0], c[-1] = True, True
m = [0] * n
m[0] = t[0]
m[-1] = a[-1]
i = 1
if n > 1:
    if a[0] != a[1] and a[0] != t[0]:
        print 0
        exit()
    elif t[-1] != t[-2] and t[-1] != a[-1]:
        print 0
        exit()
else:
    if a[0] != t[0]:
        print 0
        exit()

while i < n:
    if t[i] < t[i-1] or a[i] > a[i-1]:
        print 0
        exit()
    if t[i] != t[i-1]:
        c[i] = True
        m[i] = t[i]
    if a[i] != a[i-1]:
        if c[i-1] == True and m[i-1] != a[i-1]:
            print 0
            exit()
        m[i-1] = a[i-1]
        c[i-1] = True
    if c[i-1] == False:
        m[i-1] = min(a[i-1], t[i-1])
    i += 1
# print c
# print m
p = 1
for cc, mm in zip(c,m):
    if cc:
        continue
    p *= mm
    p %= (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())
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]:
                flag1 = True
        else:
            if a[i+1] < a[i]:
                ans = 0
                break
            flag1 = True
        if i < n-1:
            if a[i+1] < a[i]:
                flag2 = True
        else:
            if t[i-1] < t[i]:
                ans = 0
                break
            flag2  = True
        #print m
        
        if not(flag1 or flag2):
            ans *= m
            ans %= M
else:
    ans = 0
print ans,

Submission status: Wrong Answer

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

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

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

    previous_value = -1
    for i, value in enumerate(takahashi):
        if previous_value < value:
            result[i] = value
            previous_value = value

    previous_value = -1
    for i, value in enumerate(reversed(aoki)):
        index = N-i-1
        if previous_value < value:
            result[index] = value
            previous_value = 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:
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]
                if a != 0:
                    count = (count*(min(M,m))**a)%(10**9+7)
                a = 0
                m = M
        print count