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:
import math
import sys

N = input()
T = [int(i) for i in raw_input().split()]
A = [int(i) for i in raw_input().split()]

ans = 1

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

for i in range(N):
#    if(i==0 or i==N-1):
#        continue
    
    if(T[i-1]!=T[i] and T[i]>A[i]):
        print '0'
        sys.exit()
    
    if(T[i-1]>T[i]):
        continue

    if(i==N-1):
        continue

    if(A[i+1]!=A[i] and A[i]>T[i]):
        print '0'
        sys.exit()
    
    if(A[i+1]>A[i]):
        continue

    if(i==0):
        continue

    if((T[i-1]!=T[i])or(A[i+1]!=A[i])):
        continue

    else:
        ans *= min(T[i],A[i])
        ans %= 1000000007

    

print ans

Submission status: Accepted

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

M = max(t)
t = [t[0]-1] + t + [t[-1]+1]
a = [a[0]-1] + a + [a[-1]+1]

flag = True
count = 1
for i in range(n):
    if t[i+1] == M and a[i+1] == M:
        flag = False
        if t[i] == M and a[i] == M and t[i+2] == M and a[i+2] == M:
            count = (count * t[i+1]) % (10**9+7)
        continue
    if flag:
        if t[i+1] == t[i]:
            count = (count * t[i+1]) % (10**9+7)
    else:
        if a[i+1] == a[i+2]:
            count = (count * a[i+1]) % (10**9+7)

if flag:
    print(0)
else:
    print(count)

Submission status: Accepted

Solution code:
from random import random
import math
import re
import fractions

MOD = 10**9+7

N = input()
T = map(int, raw_input().split(" "))
A = map(int, raw_input().split(" "))
# N = 10**5
# A = T = [999999999] * 10**5

result = 1

for i in xrange(N):
    et = bt = ea = ba = 0
    if i == 0 or T[i] != T[i-1]:
        et = T[i]
    else:
        bt = T[i]
    if i == N-1 or A[i] != A[i+1]:
        ea = A[i]
    else:
        ba = A[i]
    if ea + et > 0:
        if ea * et > 0 and et != ea:
            result = 0
            break
        if (ea > bt and bt != 0) or (et > ba and ba != 0):
            result = 0
            break
    else:
        result *= min(ba, bt)
        result %= MOD
print(result % MOD)

Submission status: Accepted

Solution code:
N = int(input())
T = list(map(int, input().split()))
A = list(map(int, input().split()))
R = []
M = 1000000007


def main():
    global R
    global A
    p = -1
    for t in T:
        if t > p: # new
            R.append(t)
            p = t
        else:
            R.append(-t)

    # reverse direction
    R = list(reversed(R))
    A = list(reversed(A))

    p = -1
    for i, t in enumerate(A):
        r = R[i]
        if r < 0:
            max_h = -r
            if t > p: # new
                p = t
                if t <= max_h:
                    R[i] = t
                else:
                    return 0
            else:
                R[i] = -min(max_h, t)
        else:
            if t > p: # new
                p = t
                if t != r:
                    return 0
            else:
                if r > t:
                    return 0

    answer = 1
    for r in R:
        if r > 0:
            answer *= 1
        else:
            max_h = -r
            answer *= max_h
            answer %= M
    return answer
print(main())

Submission status: Accepted

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

I,J=N-1,0
ans=1
for i in range(N):
    H=min(T[i],A[i])
    if i>0 and T[i-1]<T[i]:
        H=1
        if i+1 <N  and A[i+1]<A[i]:
            if T[i]!=A[i]:
                ans=0
                break
        if i==N-1 and T[i]!=A[i]:
            ans=0
            break
        if T[i] > A[i]:
            ans=0
            break
    if i+1 <N and A[i+1] <A[i]:
        H=1
        if i==0 and T[i]!=A[i]:
            ans=0
            break
        if A[i] > T[i]:
            ans=0
            break
    if i==0:
        H=1
        if T[i] > A[i]:
            ans =0
            break
    if i==N-1:
        H=1
        if A[i] > T[i]:
            ans = 0
            break
    ans *= H
    ans %= 1000000007
print ans

Submission status: Accepted

Solution code:
from functools import reduce
MOD = 10**9 + 7
class MyRange(object):
    def __init__(self, l, r): 
        self.l = l
        self.r = r

    def __and__(self, other):
        return MyRange(max(self.l, other.l), min(self.r, other.r))

    def __len__(self):
        return self.r - self.l

    def __str__(self):
        return "[{}, {})".format(self.l, self.r)

def hoge(arr):
    res = [MyRange(arr[0], arr[0]+1)]
    for pre, cur in zip(arr, arr[1:]):
        if pre < cur:
            res.append(MyRange(cur, cur+1))
        else:
            res.append(MyRange(1, cur+1))
    return res

if __name__ == "__main__":
    N = int(input())
    T = [int(i) for i in input().split()]
    A = [int(i) for i in input().split()]
    B = [t & a for t, a in zip(hoge(T), hoge(A[::-1])[::-1])]
    # print(*B)
    try:
        C = list(map(len, B))
        print(reduce(lambda a, b: (a * b) % MOD, C))
    except ValueError:
        print(0)

Submission status: Accepted

Solution code:
N = int(input())

T = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]

kakutei = [-1]* N
for i,a in enumerate(A):
    if i==N-1:
        kakutei[i]= a
        continue
    if a > A[i+1]:
        kakutei[i]=a

for i,t in enumerate(T):
    if i==0:
        kakutei[i]=t
        continue
    if t > T[i-1]:
        if kakutei[i]==-1 or kakutei[i]==t:
            kakutei[i] = t
        else:
            print(0)
            exit(0)

ans = 1
for i in range(N):
    if not(kakutei[i] <= A[i] and kakutei[i] <= T[i]):
        print(0)
        exit(0)
    if kakutei[i]==-1:
        ans*= min(A[i],T[i])
        ans= ans%(10**9 +7)

print(ans)

Submission status: Accepted

Solution code:
N = int(raw_input())
T_list = map(int, raw_input().split())
A_list = map(int, raw_input().split())

low_list = [1]*N
high_list = [1]*N

t_now = 0
for i in range(N):
    if T_list[i] > t_now:
        t_now = T_list[i]
        low_list[i] = T_list[i]
        high_list[i] = T_list[i]
    else:
        high_list[i] = T_list[i]
a_now = 0
for j in range(N):
    i = N - 1 - j
    if A_list[i] > a_now:
        a_now = A_list[i]
        low_list[i] = max(A_list[i], low_list[i])
        high_list[i] = min(A_list[i], high_list[i])
    else:
        high_list[i] = min(A_list[i], high_list[i])
    
res = 1
for i in range(N):
    if high_list[i] < low_list[i]:
        res = 0
        break
    else:
        res = res*(high_list[i] - low_list[i] + 1) % 1000000007

print res

Submission status: Accepted

Solution code:
DIV = 10**9 + 7

N = int(input())
T = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]


def get_range(a, b):
    if b > a:
        return b, b
    if b == a:
        return (1, b)


left = [(T[0], T[0])]
right = [(A[-1], A[-1])]
for i in range(N - 1):
    a, b = T[i], T[i + 1]
    left.append(get_range(a, b))
    a, b = A[N - i - 1], A[N - i - 2]
    right.append(get_range(a, b))
right = right[::-1]


ans = 1
for i in range(N):
    a1, b1 = left[i]
    a2, b2 = right[i]

    a = max(a1, a2)
    b = min(b1, b2)

    ans *= max((b - a + 1), 0)
    ans %= DIV


print(ans)

Submission status: Accepted

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

prev = -1
TP = [None for _ in xrange(N)]
for i, t in enumerate(T):
    if t > prev:
        TP[i] = (t, t)
    else:
        TP[i] = (1, t)
    prev = t

prev = -1
AP = [None for _ in xrange(N)]
for i in xrange(N-1, -1, -1):
    a = A[i]
    if a > prev:
        AP[i] = (a, a)
    else:
        AP[i] = (1, a)
    prev = a

ans = 1
for i in xrange(N):
    lt, rt = TP[i]
    la, ra = AP[i]

    if rt < la or ra < lt:
        print 0
        exit()

    s = sorted([lt, rt, la, ra])
    ans = ans * (s[2]-s[1]+1) % MOD
print ans

Submission status: Accepted

Solution code:
from sys import stdin
from itertools import repeat
def main():
    n = int(stdin.readline())
    a = map(int, stdin.readline().split(), repeat(10, n))
    b = map(int, stdin.readline().split(), repeat(10, n))
    la = [0] * n
    lb = [0] * n
    la[0] = a[0]
    for i in xrange(1, n):
        if a[i-1] < a[i]:
            la[i] = a[i]
        else:
            la[i] = 1
    lb[-1] = b[-1]
    for i in xrange(-2, -n-1, -1):
        if b[i+1] < b[i]:
            lb[i] = b[i]
        else:
            lb[i] = 1
    ans = 1
    for i in xrange(n):
        if a[i] < lb[i] or b[i] < la[i]:
            print 0
            return
        l = la[i] if la[i] > lb[i] else lb[i]
        r = b[i] if b[i] < a[i] else a[i]
        ans = ans * (r - l + 1) % 1000000007
    print ans
main()

Submission status: Accepted

Solution code:


N = input()
heights = [0] * N
line = raw_input()
T = map(int, line.split())
line = raw_input()
A = map(int, line.split())

def cal():
    preMax = 0
    for i in xrange(N):
        if T[i] > preMax:
            heights[i] = T[i]
            preMax = T[i]
        else:
            heights[i] = -preMax

    preMax = 0
    for i in xrange(N-1, -1, -1):
        if A[i] > preMax:
            # check
            if heights[i] > 0 and heights[i] != A[i]:
                return 0
            if heights[i] < 0 and A[i] > -heights[i]:
                return 0
            heights[i] = A[i]
            preMax = A[i]
        else:
            # heights[i] should <= preMax
            if heights[i] > 0 and heights[i] > preMax:
                return 0
            if heights[i] < 0:
                heights[i] = max(heights[i], -preMax)

    total = 1
    for i in xrange(N):
        if heights[i] < 0:
            total *= -heights[i]
            total %= (10 ** 9 + 7)

    return total

print cal()

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf = 10**10
mod = 10**9 + 7


def f():
    n = int(input())
    t = list(map(int, input().split()))
    a = list(map(int, input().split()))
    r = 0

    if a[-1] > t[-1] or a[0] < t[0]:
        return 0

    for i in range(1,n):
        if (t[i-1] < t[i] and t[i] > a[i]) or t[i] < t[i-1]:
            return 0

    for i in range(n-2,-1,-1):
        if (a[i+1] < a[i] and a[i] > t[i]) or a[i] < a[i+1]:
            return 0

    r = 1
    for i in range(1,n-1):
        if t[i] != t[i-1] or a[i] != a[i+1]:
            continue
        if t[i] < a[i]:
            r = r * t[i] % mod
        else:
            r = r * a[i] % mod

    return r


print(f())

Submission status: Accepted

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

INF = 10**9
record_A = []
record_B = []

record_A.append((A[0], A[0]))

for i in range(1, N):
    if A[i] == A[i-1]:
        record_A.append((1, record_A[-1][1]))
    else:
        record_A.append((A[i], A[i]))

record_B.append((B[-1], B[-1]))

for i in reversed(range(N-1)):
    if B[i] == B[i+1]:
        record_B.append((1, record_B[-1][1]))
    else:
        record_B.append((B[i], B[i]))

ans = 1
mod = 10**9 + 7
for (lb_a, ub_a), (lb_b, ub_b) in zip(record_A, reversed(record_B)):
    lb, ub = max(lb_a, lb_b), min(ub_a, ub_b)
    if ub - lb < 0:
        print(0)
        break
    ans *= ub - lb + 1
    ans %= mod
else:
    print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys,copy,math,heapq,itertools as it,fractions,re,bisect,collections as coll

mod = 10**9 + 7

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

hmin = [1]*N
hmax = [1]*N
hmin[0] = hmax[0] = T[0]
hmin[N - 1] = hmax[N - 1] = A[N - 1]

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

for i in xrange(1, N - 1):
    if T[i] > T[i - 1]:
        hmin[i] = hmax[i] = T[i]
    else:
        hmax[i] = T[i]
for i in xrange(N - 2, 0, -1):
    if A[i] > A[i + 1]:
        if A[i] > hmax[i]:
            print 0
            exit()
        hmax[i] = hmin[i] = A[i]
    else:
        hmax[i] = min(hmax[i], A[i])

ans = 1
for i in xrange(N):
    ans = ans*(hmax[i] - hmin[i] + 1)%mod

print ans

Submission status: Accepted

Solution code:
N = int(raw_input())
a = map(int, raw_input().split())
b = map(int, raw_input().split())
low = [1] * N
high = [float('inf')] * N
for i in xrange(N):
    high[i] = min(high[i], a[i])
    if i == 0 or a[i] > a[i-1]:
        low[i] = max(low[i], a[i])
for i in xrange(N):
    high[i] = min(high[i], b[i])
    if i == N-1 or b[i] > b[i+1]:
        low[i] = max(low[i], b[i])
M = 10**9+7
ans = 1
for lo, hi in zip(low, high):
    ans = (ans * max(0, hi - lo + 1)) % M
print ans % M

Submission status: Accepted

Solution code:
n = int(input())
t = [int(x) for x in input().split()]
a = list(reversed([int(x) for x in input().split()]))

t2 = [(p != q, p) for p, q in zip(t, [None] + t)]
a2 = reversed([(p != q, p) for p, q in zip(a, [None] + a)])

ans = 1
for (tf, tv), (af, av) in zip(t2, a2):
    if tf:
        if af:
            if tv != av:
                ans = 0
                break
        else:
            if tv > av:
                ans = 0
                break
    elif af:
        if tv < av:
            ans = 0
            break
    else:
        ans = (ans * min(tv, av)) % 1000000007

print(ans)

Submission status: Accepted

Solution code:
import sys

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

ma = [T[0]]
mi = [T[0]]

for i in range(1, N):
    if T[i] != T[i - 1]:
        ma.append(T[i])
        mi.append(T[i])
    else:
        ma.append(T[i])
        mi.append(1)

if mi[N - 1] > A[N - 1] or ma[N - 1] < A[N - 1]:
    print 0
    sys.exit()

ma[N - 1] = min(ma[N - 1], A[N - 1])
mi[N - 1] = max(mi[N - 1], A[N - 1])

for i in range(N - 2, -1, -1):
    if A[i] != A[i + 1]:
        if mi[i] > A[i] or ma[i] < A[i]:
            print 0
            sys.exit()
        ma[i] = min(ma[i], A[i])
        mi[i] = max(mi[i], A[i])
    else:
        ma[i] = min(ma[i], A[i])
        mi[i] = max(mi[i], 1)

cnt = 1
for i in range(N):
    cnt *= (ma[i] - mi[i]) + 1
    cnt %= 10 ** 9 + 7
print cnt

Submission status: Runtime Error

Solution code:
while True:
  print(" ".join([str(i) for i in range(10000)]))

Submission status: Runtime Error

Solution code:
p = 10**9+7
N = int(input())
T = list(map(int,input().split()))
A = list(map(int,input().split()))
H = [0 for _ in range(N)]
flag = 0
if T[N-1]!=A[0]:
    flag = 1
if flag==0:
    indmaxT=0
    H[0] = T[0]
    for i in range(1,N):
        if T[i]>T[i-1]:
            H[i] = T[i]
            if H[i]==hmax:
                indmaxT = i
                break
    indmaxA = N-1
    H[N-1] = A[N-1]
    for i in range(N-2,-1,-1):
        if A[i]>A[i+1]:
            H[i]=A[i]
            if H[i]==hmax:
                indmaxA = i
                break
    if indmaxA<indmaxT:
        flag = 1
if flag==1:
    print(0)
else:
    hmax = max(H)
    cnt = 1
    cur = 0
    indH=0
    for i in range(1,indmaxT):
        if H[i]==0:
            cnt = (cnt*H[cur])%p
        elif H[i]>0:
            cur = i
    cur = N-1
    for i in range(N-2,indmaxA,-1):
        if H[i]==0:
            cnt = (cnt*H[cur])%p
        elif H[i]>0:
            cur = i
    for i in range(indmaxT+1,indmaxA):
        cnt = (cnt*hmax)%p
    print(cnt)

Submission status: Runtime Error

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

Submission status: Runtime Error

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(1, n):
    if t[i] == t[i - 1]:
        ans = ans * t[i] % mod
    if t[i] == a[i]:
        l = i
        break
for i in range(n - 1)[::-1]:
    if a[i] == a[i + 1]:
        ans = ans * a[i] % mod
    if t[i] == a[i]:
        r = i
        break

if n == 1:
    print(int(t == a))
elif t[l] != max(t) or a[r] != max(a):
    print(0)
else:
    print(ans * pow(t[l], max(r - l - 1, 0), mod) % mod)

Submission status: Runtime Error

Solution code:
MOD = 10**9+7
n = int(input())
T = list(map(int, input().split()))
A = list(map(int, input().split()))
n_cases = [0]*n
n_cases[0] = n_cases[-1] = 1
height = [0]*n
if T[-1] != A[0]:
    print(0)
    exit()
height[0] = T[0]
height[-1] = A[-1]
for i, (t1, t2) in enumerate(zip(T, T[1:]), 1):
    if t1 != t2:
        n_cases[i] = 1
        height[i] = t2


for i, (a1, a2) in enumerate(zip(A[::-1], A[::-1][1:]), 2):
    if a1 != a2:
        n_cases[-i] = 1
        assert height[-i] == a2 or height[-i] == 0
        height[-i] = a2


for i, (t, a) in enumerate(zip(T, A)):
    if n_cases[i]:
        continue
    n_cases[i] = min(t, a)


ans = 1
for x in n_cases:
    ans *= x
    ans %= MOD
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
t = list(map(int, input().split()))
a = list(map(int, input().split()))
MOD = 10**9 + 7
t_max = max(t)
for i in range(n):
  if a[i] == t_max:
    max_ind = i
    break
if t[max_ind] != a[max_ind] or t[-1] != a[0]:
  print(0)
else:
  idx = []
  before = 0
  for i in range(n):
    if before == t[i]:
      idx .append(i)
    before = t[i]
  if len(idx) == 0:
    print(1)
  else:
    new_idx = []
    for j in idx:
      if j < n-1 and a[j] == a[j+1]:
        new_idx.append(j)
    if len(new_idx) == 0:
      print(0)
    else:
      ans = 1
      for k in new_idx:
        ans *= min(t[k], a[k])
        ans %= MOD
      print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
t = list(map(int, input().split()))
a = list(map(int, input().split()))
MOD = 10**9 + 7
if t[0] != a[-1]:
  print(0)
else:
  idx = []
  before = 0
  for i in range(n):
    if before == t[i]:
      idx.append(i)
    before = t[i]
  new_index = []
  for j in idx:
    if j < n-1:
      if a[j] == a[j+1]:
        new_index.append(j)
  if len(new_index) > 0:
    print(1)
  else:
    ans = 1
    for k in new_index:
      ans *= min(t[k], a[k])
      ans %= MOD
  print(ans)

Submission status: Runtime Error

Solution code:

input = sys.stdin.readline




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


def II():
    return int(input())



mod = 10**9+7


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact



def serch(x, count):
    #print("top", x, count)
            

    for d in directions:
        nx = d+x
        #print(nx)
        if np.all(0 <= nx) and np.all(nx < (H, W)):
            if field[nx[0]][nx[1]] == "E":
                count += 1 
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)  
                continue
            if field[nx[0]][nx[1]] == "#":
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)    
                 
    return count

N=II()

t=acinput()
a=acinput()

st = [[] for i in range(N)]
st[0]=[t[0],t[0]]
for i in range(1,N):
    if t[i-1]<t[i]:
        st[i]=[t[i],t[i]]
    else:
        st[i]=[1,st[i-1][1]]
        

sa = [[] for i in range(N)]
sa[N-1] = [a[N-1], a[N-1]]
for i in reversed(range(N-1)):
    if a[i] > a[i+1]:
        sa[i] = [a[i], a[i]]
    else:
        sa[i] = [1,sa[i+1][1]]

def diff(x):
    return x[1]-x[0]
res=1
for i in range(N):
    
    x=max(sa[i])-min(st[i])+1
    y=max(st[i])-min(sa[i])+1
    #print(x,y,np.diff(sa))
    if x<=0 or y<=0:
        res=0
        break
    res=res* min(x,y,diff(sa[i])+1,diff(st[i])+1)
    res=res%mod
    
print(res%mod)

#print(st)
#print(sa)

Submission status: Runtime Error

Solution code:
"""from collections import *
from itertools import *
from bisect import *
from heapq import *

import math
from fractions import gcd"""

N=int(input())
T=list(map(int,input().split()))
A=list(map(int,input().split()))
mod=10**9+7
lst=[0 for i in range(N)]


lst[0]=T[0]
lst[N-1]=A[N-1]
if T[N-1]!=A[0]:
    print(0)
    exit()
for i in range(1,N):
    if T[i]>T[i-1]:
        if lst[i] and lst[i]!=T[i]:
            print(0)
            exit()
        lst[i]=T[i]

for i in range(N-2,-1,-1):
    if A[i]>A[i+1]:
        if lst[i] and lst[i]!=A[i]:
            print(0)
            exit()
        elif not lst[i] and T[i]<A[i]
        lst[i]=A[i]

count=1
for i in range(N):
    if not lst[i]:
        count=count*min(T[i],A[i])%mod
print(count)

Submission status: Runtime Error

Solution code:
N = int(input())
T = list(map(int,input().split()))
A = list(map(int,input().split()))
T_min = [0]*N
T_min[0] = T[0]
for i in range(1,N):
  if T[i-1]<T[i]:
    T_min[i] = T[i]
A_min = [0]*N
A_min[N-1] = A[N-1]
for i in range(N-2,-1,-1):
  if A[i+1]<A[i]:
    A_min[i] = A[i]
M = [0]*N
M_min = [0]*N
for i in range(N):
  M = min(T[i],A[i])
  M_min = max(T_min[i],A_min[i])
ans = 1
mod = 10**9+7
for i in range(N):
  if M[i]<M_min[i]:
    ans = 0
  else:
    ans*=M[i]+1-M_min[i]
    ans%=mod
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
tli = [ int(it) for it in input().split() ]
ali = [ int(it) for it in input().split() ]
#print (tli,ali)

mav1 = max(tli)
mav2 = max(ali)
mav = mav1
#print (mav1,mav2)
if (mav1!=mav2):
  print (0)
else:
  mai1 = tli.index(mav)
  mai2 = n-1-list(reversed(ali)).index(mav)
  #print (mai1,mai2)
  if (mai2<mai1):
    print (0)
  else:
    s = 1
    tma = -1
    for i in range(0,mai1+1):
      if tli[i]>tma:
        tma = tli[i]
        #print ("lt",tma)
      else:
        s*=(tli[i])
        #print ("l")
    tma = -1
    for i in range(n-1,mai2-1,-1):
      if ali[i]>tma:
        tma = ali[i]
        #print ("rt",tma)
      else:
        s*=(ali[i])
        #print ("r")
    if (mai2-mai1>1):
      for i in range(mai2-mai1-1):
        s*=mav
  #print ( s )
  print ( s%(1000000007) )

Submission status: Runtime Error

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

if T[N-1]!=A[0]:
    print(0)
elif N==1:
    print(1)
else:
    
    L=[[T[0],0]]
    for i in range(1,N):
        if T[i]==T[i-1]:
            L.append([T[i],1])
        else:
            L.append([T[i],0])
    #print(L)
            
    if L[len(L)-1][0]>A[len(L)-1]:
        L[len(L)-1]=[A[len(L)-1],0]
    else:
        L[len(L)-1][1][0]
    
    for i in range(1,N):
        if L[len(L)-1-i][0]>=A[len(L)-1-i]:
            L[len(L)-1-i][0]=A[len(L)-1-i]
            if A[len(L)-1-i]==A[len(L)-i]:
               pass
            else:
                L[len(L)-1-i][1]=0
            if L[len(L)-1-i][0]>L[len(L)-i][0]:
                L[len(L)-1-i][1]=0
    #print(L)
    ans=1
    for i in range(len(L)):
        if L[i][1]==1:
            ans*=L[i][0]
            ans%=(10**9+7)
    print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N=int(input())
S=input().split()
T=list(int(S[i]) for i in range(len(S)))
B=input().split()
A=list(int(B[i]) for i in range(len(B)))
a=1
if max(T)!=max(A):
    print(0)
else:
    for i in range(1,N-1):
        
        if T[i]==T[i-1] and A[N-i]==A[N-i+1]:
            a*=min(T[i],A[N-i])
        if T[i]>T[i-1] or A[N-i]>A[N-i+1]:
            a*=1
    print(a%(10**9+7))

Submission status: Runtime Error

Solution code:
N=int(input())
S=input().split()
T=list(int(S[i]) for i in range(len(S)))
B=input().split()
A=list(int(B[i]) for i in range(len(B)))
a=1
if max(T)!=max(A):
    print(-1)
else:
    for i in range(1,N-1):
        
        if T[i]==T[i-1] and A[N-i]==A[N-i+1]:
            a*=min(T[i],A[N-i])
        if T[i]>T[i-1] or A[N-i]>A[N-i+1]:
            a*=1
    print(a%(10**9+7))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
N = int(input())

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


def judge(t):
    if t[0] == t[1]:
        return True
    else:
        return False

if judge(A[0:2]) and A[0] < T[0]:
    ans = 0
    ansable = False
elif not judge(A[0:2]) and A[0] != T[0]:
    ans = 0
    ansable = False
else:
    ans = 1
    ansable = True
    

for i in range(1, N-1):
    if ansable == False:
        break
    
    elif not judge(A[i:i+2]) and not judge(T[i-1:i+1]):
        if A[i] != T[i]:
            ans = 0   
            ansable = False
    elif not judge(A[i:i+2]) and judge(T[i-1:i+1]):
        if A[i] > T[i]:
            ans = 0
            ansable = False
    elif judge(A[i:i+2]) and not judge(T[i-1:i+1]):
        if A[i] < T[i]:
            ans = 0  
            ansable = False
    elif judge(A[i:i+2]) and judge(T[i-1:i+1]):
         ans *= (min(A[i],T[i]))%(10**9+7)
            
print(ans%(10**9+7))

Submission status: Runtime Error

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: Runtime Error

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])
print(P % (10 ** 9 + 7)

Submission status: Runtime Error

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]:
    print (0)
    exit()
 
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 *= T[i]
        elif T[i] > A[i]:
            if A[i] == A[i + 1]:
                ans *= A[i]
        else:
            if A[i] == A[i + 1]:
                ans *= T[i]
            else:
                print (0)
                exit()
 
print (ans % mod)