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:
N = int(raw_input())
T = map(int, raw_input().split())
A = map(int, raw_input().split())

flag = [-1 for i in range(N)]
old = -1
for i, t in enumerate(T):
    if old < t:
        flag[i] = 1
        old = t
    elif old == t:
        flag[i] = t
    else:
        flag[i] = 0

old = -1
for i, a in zip(range(N)[::-1], A[::-1]):
    if old < a:
        flag[i] = 1
        old = a
    elif old == a:
        if a < flag[i]:
            flag[i] = a
    else:
        flag[i] = 0

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

print reduce(lambda x, y: x*y, flag)%(10**9+7)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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 i+1 <N and A[i+1] <A[i]:
        H=1
        if i==0 and T[i]!=A[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: Wrong Answer

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

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

ans = 1

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(A[i+1]!=A[i] and A[i]>T[i]):
        print '0'
        sys.exit()

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

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

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    t = [int(n) for n in input().split()]
    a = [int(n) for n in input().split()]

    if not max(t) == max(a):
        print(0)
        return

    max_h = 0
    for i in range(n):
        if max_h < t[i]:
            max_h = t[i]
            t[i] = 1
        elif max_h == t[i]:
            t[i] = max_h
        else:
            print(0)
            return

    max_h = 0
    for i in range(n - 1, -1, -1):
        if max_h < a[i]:
            max_h = a[i]
            a[i] = 1
        elif max_h == a[i]:
            a[i] = max_h
        else:
            print(0)
            return

    l = [min(n1, n2) for n1, n2 in zip(t, a)]
    c = 1
    for n in l:
        c *= n

    print(c % (10 ** 9 + 7))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
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)]
h[0] = t[0]
p = [False for i in range(n)]
p[0] = True

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

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

mod = 10**9 + 7
total = 1
for i in range(n):
    if not p[i]:
        total = (total * h[i]) % mod
print(total)

Submission status: Wrong Answer

Solution code:
def times(A):
    ret = A[0]
    for i in A[1:]:
        ret = (i * ret) % 1000000007
    return ret % 1000000007

N = input()
T = map(int, raw_input().strip().split(' '))
A = map(int, raw_input().strip().split(' '))
limit = [1000000005 for i in xrange(N)]

cur = T[0]
limit[0] = 1
limit[-1] = 1
flag = True
for i in xrange(1, N-1):
    if T[i] > cur:
        if T[i] > A[i]:
            print 0
            flag = False
            break
        cur = T[i]
        limit[i] = 1
    else:
        limit[i] = min(limit[i], T[i])
cur = A[-1]
if flag:
    for i in xrange(N-2, 0, -1):
        if A[i] > cur:
            if A[i] > T[i]:
                print 0
                flag = False
                break
            limit[i] = 1
            cur = A[i]
        else:
            limit[i] = min(limit[i], A[i])
if flag: print times(limit)

Submission status: Wrong Answer

Solution code:
DIV = 1000000007

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 *= (b - a + 1)
    ans %= DIV


print(ans)

Submission status: Wrong Answer

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+1 <N and A[i+1] <A[i]:
        H=1
    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: Wrong Answer

Solution code:
def times(A):
    ret = A[0]
    for i in A[1:]:
        ret = (i * ret) % 1000000007
    return ret


N = input()
T = map(int, raw_input().strip().split(' '))
A = map(int, raw_input().strip().split(' '))
limit = [1000000005 for i in xrange(N)]

cur = T[0]
limit[0] = 1
limit[-1] = 1
flag = True
for i in xrange(1, N-1):
    if T[i] > cur:
        if T[i] > A[i]:
            print 0
            flag = False
            break
        cur = T[i]
        limit[i] = 1
    else:
        limit[i] = min(limit[i], T[i])
cur = A[-1]
if flag:
    for i in xrange(N-2, 0, -1):
        if A[i] > cur:
            if A[i] > T[i]:
                print 0
                flag = False
                break
            limit[i] = 1
            cur = A[i]
        else:
            limit[i] = min(limit[i], A[i])
if flag: print times(limit)

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


print p % 1000000007

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import math,string,itertools,fractions,heapq,collections,re,array,bisect

def s():
    return raw_input().strip()
def n():
    return int(raw_input())
def d():
    return float(raw_input())

def ls():
    return raw_input().strip().split()
def ln():
    return map(int, raw_input().strip().split())
def ld():
    return map(float, raw_input().strip().split())

def fs():
    return [raw_input().strip() for i in xrange(input())]
def fn():
    return [int(raw_input().strip()) for i in xrange(input())]
def fd():
    return [float(raw_input().strip()) for i in xrange(input())]

EPS = 1e-9

#http://www.deqnotes.net/acmicpc/2d_geometry/lines
def dot(a, b):
    return a.real * b.real + a.imag * b.imag

def cross(a, b):
    return a.real * b.imag - a.imag * b.real

def distance(a, b, c):
    if dot(b-a, c-a) <= EPS:
        return abs(c-a)
    if dot(a-b, c-b) <= EPS:
        return abs(c-b)
    return abs(cross(b-a, c-a)/(b-a))

N = n()
T = ln()
A = ln()

fixed = [-1] * N

before = 0
lte = [1e10] * N

for i in xrange(N):
    if T[i] > before:
        fixed[i] = T[i]
        before = T[i]
        if T[i] > A[i]:
            ok = False
    else:
        if lte[i] > before:
            lte[i] = before

ok = True

before = 0
for i in xrange(N - 1, -1, -1):
    if A[i] > before:
        fixed[i] = A[i]
        before = A[i]
        if A[i] > T[i]:
            ok = False
    else:
        if lte[i] > before:
            lte[i] = before

comb = 1

for i in xrange(N):
    if fixed[i] == -1:
        comb *= lte[i]
        comb %= 10**9 + 7

if ok:
    print comb
else:
    print 0

Submission status: Wrong Answer

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

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
    else:
        result *= min(ba, bt)
        result %= MOD
print result

Submission status: Wrong Answer

Solution code:
N = int(input())
T = list(map(int,input().split()))
A = list(map(int,input().split()))
B = [0] * N

h = 0
for i in range(N):
    if h < T[i]:
        B[i] = T[i]
        h = T[i]

h = 0
check = True
for i in range(N):
    if h < A[N-i-1]:
        if B[N-i-1] and B[N-i-1] != A[N-i-1]:
            check = False
        else:
            B[N-i-1] = A[N-i-1]
            h = A[N-i-1]

for i in range(N):
    if B[i] == 0:
        B[i] = min((T[i],A[i]))
    else:
        B[i] = 1
if check:
    ans = 1
    for i in range(N):
        ans = (ans * B[i]) % (int(1e9 + 7))
    print (ans)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
N = input()
T = map(int, raw_input().strip().split(' '))
A = map(int, raw_input().strip().split(' '))
limit = [1000000005 for i in xrange(N)]

cur = T[0]
limit[0] = 1
limit[-1] = 1
flag = True
for i in xrange(1, N-1):
    if T[i] > cur:
        if T[i] > A[i]:
            print 0
            flag = False
            break
        cur = T[i]
        limit[i] = 1
    else:
        limit[i] = min(limit[i], T[i])
cur = A[-1]
if flag:
    for i in xrange(N-2, 0, -1):
        if A[i] > cur:
            if A[i] > T[i]:
                print 0
                flag = False
                break
            limit[i] = 1
            cur = A[i]
        else:
            limit[i] = min(limit[i], A[i])
if flag: print reduce(lambda a, b: a*b, limit, 1) % 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 
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                                                                                                                                                                                                       


print p

Submission status: Wrong Answer

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
    m = a[0]
    if a[-1] < t[0] or a[0] > t[-1]:
        return 0
    for i in range(1,n):
        if m > a[i]:
            m = a[i]
        if (t[i-1] < t[i] and t[i] > m) or t[i] < t[i-1]:
            return 0
    m = t[n-1]
    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: Wrong Answer

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

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

ans = 1


for i in range(N):
    if(i==0 or i==N-1):
        continue
    elif((T[i-1]!=T[i])or(A[i+1]!=A[i])):
        continue

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

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
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:
N = input()
T = map(int, raw_input().strip().split(' '))
A = map(int, raw_input().strip().split(' '))
limit = [1000000005 for i in xrange(N)]

cur = T[0]
limit[0] = 1
limit[-1] = 1
for i in xrange(1, N-1):
    if T[i] > cur:
        if T[i] > A[i]:
            print 0
            quit()
        cur = T[i]
        limit[i] = 1
    else:
        limit[i] = min(limit[i], T[i])
print limit
cur = A[-1]
for i in xrange(N-2, 0, -1):
    if A[i] > cur:
        if A[i] > T[i]:
            print 0
            quit()
        limit[i] = 1
        cur = A[i]
    else:
        limit[i] = min(limit[i], A[i])
print reduce(lambda a, b: a*b, limit, 1) % 1000000007

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)]
h[0] = t[0]
h[n-1] = a[n-1]
p = [False for i in range(n)]
p[0] = True
p[n-1] = True
for i in range(1,n):
    if t[i] > t[i-1]:
        p[i] = True
    h[i] = t[i]
for i in range(n-2,-1,-1):
    if a[i] > a[i+1]:
        p[i] = True
    h[i] = min(t[i],a[i])

mod = 10**9+7
total = 1
for i in range(n):
    if not p[i]:
        total = (total * h[i]) % (mod)
print(total)

Submission status: Wrong Answer

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 * (hi - lo + 1)) % M
print ans % M

Submission status: Time Limit Exceeded

Solution code:
while True:
  print()

Submission status: Time Limit Exceeded

Solution code:

#import numpy as np
from functools import *
import sys
sys.setrecursionlimit(100000)
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*= min(x,y,diff(sa[i])+1,diff(st[i])+1)%mod
    
print(res%mod)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
T = lr()
A = lr()
MOD = 10 ** 9 + 7

T_spe = [T[0]] + [0] * (N-1)
A_spe = [0] * (N-1) + [A[-1]]

for i in range(1, N):
    if T[i] > T[i-1]:
        T_spe[i] = T[i]

for i in range(N-2, -1, -1):
    if A[i] > A[i+1]:
        A_spe[i] = A[i]

answer = 1
for i in range(N):
    if T_spe[i] > A[i] or A_spe[i] > T[i]:
        print(0)
        exit()
    if T_spe[i] == 0 and A_spe[i] == 0:
        answer *= min(T[i], A[i])

print(answer%MOD)
# 34

Submission status: Time Limit Exceeded

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
T = lr()
A = lr()
MOD = 10 ** 9 + 7

T_spe = [T[0]] + [0] * (N-1)
A_spe = [0] * (N-1) + [A[-1]]

for i in range(1, N):
    if T[i] > T[i-1]:
        T_spe[i] = T[i]

for i in range(N-2, -1, -1):
    if A[i] > A[i+1]:
        A_spe[i] = A[i]

answer = 1
for i in range(N):
    if T_spe[i] > A[i] or A_spe[i] > T[i]:
        print(0)
        exit()
    if T_spe[i] == 0 and A_spe[i] == 0:
        answer *= min(T[i], A[i])

print(answer%MOD)
# 34

Submission status: Time Limit Exceeded

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
T = lr()
A = lr()
MOD = 10 ** 9 + 7

T_spe = [T[0]] + [0] * (N-1)
A_spe = [0] * (N-1) + [A[-1]]

for i in range(1, N):
    if T[i] > T[i-1]:
        T_spe[i] = T[i]

for i in range(N-2, -1, -1):
    if A[i] > A[i+1]:
        A_spe[i] = A[i]

for i in range(N):
    if T_spe[i] > A[i] or A_spe[i] > T[i]:
        print(0)
        exit()

answer = 1
for i in range(N):
    if T_spe[i] == 0 and A_spe[i] == 0:
        answer *= min(T[i], A[i]) % MOD

print(answer%MOD)
# 34

Submission status: Time Limit Exceeded

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
T = lr()
A = lr()
MOD = 10 ** 9 + 7

T_spe = [T[0]] + [0] * (N-1)
A_spe = [0] * (N-1) + [A[-1]]

for i in range(1, N):
    if T[i] > T[i-1]:
        T_spe[i] = T[i]

for i in range(N-2, -1, -1):
    if A[i] > A[i+1]:
        A_spe[i] = A[i]

for i in range(N):
    if T_spe[i] > A[i] or A_spe[i] > T[i]:
        print(0)
        exit()

answer = 1
for i in range(N):
    if T_spe[i] == 0 and A_spe[i] == 0:
        answer *= min(T[i], A[i])

print(answer%MOD)
# 34

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
t = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
result = 1
for i in range(n):
    h_max, h_min = min(t[i], a[i])+1, 1

    if i != 0:
        if t[i-1] < t[i]:
            h_min = t[i]
    else:
        h_min = t[0]

    if i != n-1:
        if a[i] > a[i+1]:
            h_min = a[i]
    else:
        h_min = max(a[n-1], h_min)


    result = result * max(h_max - h_min, 0)
print(result % (10**9+7))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
n=int(input())
t=list(map(int,input().split()))
a=list(map(int,input().split()))
t=[0]+t+[t[-1]]
a=[a[0]]+a+[0]
ans=1
#print(t,a)
for i in range(n):
    if t[i]<t[i+1]:
        if a[i+1]<t[i+1]:
            ans=0
            break
    if a[i]>a[i+1]:
        if t[i]<a[i]:
            ans=0
            break
    if t[-2]!=a[1]:
        ans=0
        break
    if t[i]==t[i+1] and a[i+1]==a[i+2]:
        ans*=min(t[i+1],a[i+1])
print(ans%(10**9+7))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
n=int(input())
t=list(map(int,input().split()))
a=list(map(int,input().split()))
t=[0]+t+[t[-1]]
a=[a[0]]+a+[0]
ans=1
#print(t,a)
for i in range(n):
    if t[i]<t[i+1]:
        if a[i+1]<t[i+1]:
            ans=0
    if a[i]>a[i+1]:
        if t[i]<a[i]:
            ans=0
    if t[-2]!=a[1]:
        ans=0
    if t[i]==t[i+1] and a[i+1]==a[i+2]:
        ans*=min(t[i+1],a[i+1])
print(ans%(10**9+7))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
n=int(input())
t=list(map(int,input().split()))
a=list(map(int,input().split()))
t=[0]+t+[t[-1]]
a=[a[0]]+a+[0]
ans=1
#print(t,a)
for i in range(n):
    if t[i]<t[i+1]:
        if a[i+1]<t[i+1]:
            ans=0
    if a[i]>a[i+1]:
        if t[i]<a[i]:
            ans=0
    if t[-2]!=a[1]:
        ans=0
for i in range(n):
    if t[i]==t[i+1] and a[i+1]==a[i+2]:
        ans*=min(t[i+1],a[i+1])
print(ans%(10**9+7))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
n=int(input())
t=list(map(int,input().split()))
a=list(map(int,input().split()))
t=[0]+t+[t[-1]]
a=[a[0]]+a+[0]
ans=1
#print(t,a)
for i in range(n):
    if t[i]<t[i+1]:
        if a[i+1]<t[i+1]:
            ans=0
    if a[i]>a[i+1]:
        if t[i]<a[i]:
            ans=0
    if t[-2]!=a[1]:
        ans=0
for i in range(n):
    if t[i]==t[i+1] and a[i+1]==a[i+2]:
        ans*=min(t[i+1],a[i+1])%(10**9+7)
print(ans%(10**9+7))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
mod=10**9+7
n=int(input())
T=[int(i) for i in input().split()]
A=[int(i) for i in input().split()]
Chk_T,Chk_A=[False]*n,[False]*n
Max_T,Max_A=[0]*n,[0]*n
Chk_T[0],Chk_A[-1]=True,True
Max_T[0],Max_A[-1]=T[0],A[-1]

for i in range(1,n):
  if T[i]!=T[i-1]:
    Chk_T[i]=True
  Max_T[i]=T[i]
  if A[-i-1]!=A[-i]:
    Chk_A[-i-1]=True
  Max_A[-i-1]=A[-i-1]
ans=1
flag=True
for i in range(n):
  if Chk_T[i]==False and Chk_A[i]==False:
    ans=ans*min(Max_T[i],Max_A[i])
  elif Chk_T[i]==True and Chk_A[i]==False:
    if Max_T[i]>Max_A[i]:
      flag=False
  elif Chk_T[i]==False and Chk_A[i]==True:
    if Max_T[i]<Max_A[i]:
      flag=False
  else:
    if Max_T[i]!=Max_A[i]:
      flag=False
print(0 if flag==False else ans%mod)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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