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(input())
T = list(map(int,input().split()))
A = list(map(int,input().split()))
exact = [0] * N # 高さが確定した場所
seem = [0] * N # 高さが確定しない場所のあり得る最大値

cur = 0
for i in range(len(T)):
  if cur < T[i]:
    exact[i] = T[i]
    cur = T[i]
  else:
    seem[i] = cur
    
cur = 0
for i in range(len(A)-1,-1,-1):
  if cur < A[i]:
    if exact[i] != 0 and exact[i] != A[i]: # 既に記録された確定数値が今回の値と異なる場合
      print(0)
      exit(0)
    exact[i] = A[i]
    cur = A[i]
  else:
    seem[i] = min(seem[i],cur)
    
DIV = 10 ** 9 + 7
ans = 1
for i in range(N):
  if exact[i] != 0: # 確定数値があれば場合の数は変わらない
    continue
  else:
    ans *= seem[i]
    ans %= DIV

print(ans)

Submission status: Wrong Answer

Solution code:
MOD = 10 **9 + 7
INF = 10 ** 10

def main():
    n = int(input())
    T = list(map(int,input().split()))
    A = list(map(int,input().split()))
    mountainT = [-1] * n
    mountainT[0] = T[0]
    for i in range(1,n):
        if T[i - 1] != T[i]:
            mountainT[i] = T[i]
    
    mountainA = [-1] * n
    mountainA[-1] = A[-1]
    for i in range(n - 1,0,-1):
        if A[i - 1] != A[i]:
            mountainA[i - 1] = A[i - 1]
    
    ans = 1
    for i in range(n):
        if mountainA[i] != -1 and mountainT[i] != -1:
            if mountainT[i] != mountainA[i]:
                ans = 0
                break
        elif mountainA[i] != -1 or mountainT[i] != -1:
            continue
        else:
            ans *= min(T[i],A[i])
            ans %= MOD
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
t = list(map(int,input().split()))
mod = 10**9+7
h = [[1,10**12] for i in range(n)]
h[0] = [a[0],a[0]]
h[-1] = [t[-1],t[-1]]
for i in range(1,n-1):
  h[i][1] = min(h[i][1],a[i])
  if a[i-1] < a[i]:
    h[i][0] = a[i]
for i in range(n-2,0,-1):
  h[i][1] = min(h[i][1],t[i])
  if t[i+1] < t[i]:
    h[i][0] = t[i]
ans = 1
for i in range(n):
  if h[i][0] > h[i][1]:
    print(0)
    quit()
  ans = (ans*(h[i][1]-h[i][0]+1))%mod
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
t=[int(j) for j in input().split()]
a=[int(j) for j in input().split()]
tmp=0
l=[]
for i in t:
    if tmp<i:
        l.append(-i)
        tmp=i
    else:
        l.append(i)
tmp=0
ans=1
mod=10**9+7
for i in range(n)[::-1]:
    if tmp<a[i]:
        if l[i]>=a[i]:
            tmp=a[i]
        else:
            if l[i]>0 or -l[i]!=a[i]:
                print(0)
                exit()
    else:
        if l[i]>0:
            ans=(ans*min(a[i],l[i]))%mod
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
t=[int(j) for j in input().split()]
a=[int(j) for j in input().split()]
tmp=0
l=[]
for i in t:
    if tmp<i:
        l.append(-i)
        tmp=i
    else:
        l.append(i)
tmp=0
ans=1
mod=10**9+7
for i in range(n)[::-1]:
    if tmp<a[i]:
        if l[i]>=a[i]:
            tmp=a[i]
        else:
            if l[i]>0:
                print(0)
                exit()
    else:
        if l[i]>0:
            ans=(ans*min(a[i],l[i]))%mod
print(ans)

Submission status: Wrong Answer

Solution code:

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)
    res%=mod
    
print(res%mod)

Submission status: Wrong Answer

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

directions=np.array([[1,0],[0,1],[-1,0],[0,-1]])
directions = list(map(np.array, directions))

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

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))
    res*= min(x,y,np.diff(sa[i])[0]+1,np.diff(st[i])[0]+1)%mod
    
print(res%mod)

Submission status: Wrong Answer

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

if T[-1] != A[0]:
    print(0)
elif all([max(T[i], A[i]) == T[-1] for i in range(N)]):
    pT = T[0]
    for i in range(1, N):
        if T[i] != pT:
            T[i], pT = 1, T[i]

    pA = A[-1]
    for i in range(N-1)[::-1]:
        if A[i] != pA:
            A[i], pA = 1, A[i]

    ans = 1
    for i in range(1, N-1):
        ans = (ans * min(T[i], A[i])) % (10**9+7)
    print(ans)
else:
    print(0)

Submission status: Wrong Answer

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

if T[-1] != A[0]:
    print(0)
elif all([max(T[i], A[i]) == T[-1] for i in range(N)]):
    T[0], pT = 1, T[0]
    for i in range(1, N):
        if T[i] != pT:
            T[i], pT = 1, T[i]

    A[-1], pA = 1, A[-1]
    for i in range(N-1)[::-1]:
        if A[i] != pA:
            A[i], pA = 1, A[i]

    ans = 1
    for i in range(N):
        ans = (ans % (10**9+7)) * min(T[i], A[i]) 
    print(ans % (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()))

maxTA = min(T[-1], A[0])
if all([max(T[i], A[i]) == maxTA for i in range(N)]):
    T[0], pT = 1, T[0]
    for i in range(1, N):
        if T[i] != pT:
            T[i], pT = 1, T[i]

    A[-1], pA = 1, A[-1]
    for i in range(N-1)[::-1]:
        if A[i] != pA:
            A[i], pA = 1, A[i]

    ans = 1
    for i in range(N):
        ans = (ans % (10**9+7)) * min(T[i], A[i]) 
    print(ans % (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()))

if all([max(T[i], A[i]) == A[0] for i in range(N)]):
    T[0], pT = 1, T[0]
    for i in range(1, N):
        if T[i] != pT:
            T[i], pT = 1, T[i]

    A[-1], pA = 1, A[-1]
    for i in range(N-1)[::-1]:
        if A[i] != pA:
            A[i], pA = 1, A[i]

    ans = 1
    for i in range(N):
        ans = (ans % (10**9+7)) * min(T[i], A[i]) 
    print(ans % (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()))

if all([max(T[i], A[i]) == A[0] for i in range(N)]):
    T[0], pT = 1, T[0]
    for i in range(1, N):
        if T[i] != pT:
            T[i], pT = 1, T[i]

    A[-1], pA = 1, A[-1]
    for i in range(N-1)[::-1]:
        if A[i] != pA:
            A[i], pA = 1, A[i]

    ans = 1
    for i in range(N):
        ans *= min(T[i], A[i]) % (10**9+7)
    print(ans % (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()))

if all([max(T[i], A[i]) == T[0] for i in range(N)]):
    T[0], pT = 1, T[0]
    for i in range(1, N):
        if T[i] != pT:
            T[i], pT = 1, T[i]

    A[-1], pA = 1, A[-1]
    for i in range(N-1)[::-1]:
        if A[i] != pA:
            A[i], pA = 1, A[i]

    ans = 1
    for i in range(N):
        ans *= min(T[i], A[i]) % (10**9+7)
    print(ans % (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()))

maxTA = [max(T[i], A[i]) for i in range(N)]
if all([maxTA[i-1] <= maxTA[i] for i in range(1, N)]):
    T[0], pT = 1, T[0]
    for i in range(1, N):
        if T[i] != pT:
            T[i], pT = 1, T[i]

    A[-1], pA = 1, A[-1]
    for i in range(N-1)[::-1]:
        if A[i] != pA:
            A[i], pA = 1, A[i]

    ans = 1
    for i in range(N):
        ans *= min(T[i], A[i]) % (10**9+7)
    print(ans%(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()))

maxTA = [max(T[i], A[i]) for i in range(N)]
if all([maxTA[i-1] <= maxTA[i] for i in range(1, N)]):
    T[0], pT = 1, T[0]
    for i in range(1, N):
        if T[i] != pT:
            T[i], pT = 1, T[i]

    A[-1], pA = 1, A[-1]
    for i in range(N-1)[::-1]:
        if A[i] != pA:
            A[i], pA = 1, A[i]

    ans = 1
    for i in range(N):
        ans *= min(T[i], A[i])
    print(ans%(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()))
mod=10**9+7

#0:高さ確定 1:高さそれ以下
for i in range(0,N):
    if i==0:
        T[i]=(T[i],0)
    else:
        h,query=T[i-1]
        if h==T[i]:
            T[i]=(T[i],1)
        else:
            T[i]=(T[i],0)

for i in range(0,N):
    if i==0:
        A[-i-1]=(A[-i-1],0)
    else:
        h,query=A[-i]
        if h==A[-i-1]:
            A[-i-1]=(A[-i-1],1)
        else:
            A[-i-1]=(A[-i-1],0)

ans=1
for i in range(0,N):
    h1,q1=T[i]
    h2,q2=A[i]
    if q1==0:
        if q2==0:
            if q1!=q2:
                ans=0
        else:
            if h1>h2:
                ans=0
    else:
        if q2==0:
            if h2>h1:
                ans=0
        else:
            ans=(ans*min(h1,h2))%mod

print(ans)

Submission status: Wrong Answer

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

H = [0] * N
_H = [INF] * N
pre_h = 0
for i, h in enumerate(T):
  if h != pre_h:
    H[i] = h
    pre_h = h
  else:
    _H[i] = min(_H[i], h)

pre_h = 0
for i, h in enumerate(A[::-1], 1):
  if h != pre_h:
    if H[-i] > 0 and H[-i] != h:
      print(0)
      quit()
    H[-i] = h
    pre_h = h
  else:
    _H[-i] = min(_H[-i], h)

ans = 1
for h, _h in zip(H, _H):
  if h == 0:
    ans *= _h
    ans %= MOD

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
T = list(map(int, input().split(" ")))
A = list(map(int, input().split(" ")))
h = [-1 for i in range(N)]
mod = 10 ** 9 + 7

h[0] = T[0]
h[N - 1] = A[N - 1]
for i in range(N - 1):
    if T[i] < T[i + 1]:
        h[i + 1] = T[i + 1]

for i in range(N - 1):
    if A[i + 1] < A[i] and (h[i] == -1 or h[i] == A[i]):
        h[i] = A[i]
    elif A[i + 1] < A[i]:
        h[i] = 0

ans = 1
for i in range(N):
    if h[i] == 0:
        ans *= h[i]
        break
    elif h[i] == -1:
        ans *= min(T[i], A[i])
        ans %= mod

if A[0] != T[N - 1]:
    print(0)
else:
    print(ans % mod)

Submission status: Wrong Answer

Solution code:
N = int(input())
T = list(map(int, input().split(" ")))
A = list(map(int, input().split(" ")))
h = [-1 for i in range(N)]
mod = 10 ** 9 + 7

h[0] = T[0]
h[N - 1] = A[N - 1]
for i in range(N - 1):
    if T[i] < T[i + 1]:
        h[i + 1] = T[i + 1]

for i in range(N - 1):
    if A[i + 1] < A[i] and (h[i] == -1 or h[i] == A[i]):
        h[i] = A[i]
    elif A[i + 1] < A[i]:
        h[i] = 0

ans = 1
for i in range(N):
    if h[i] == 0:
        ans *= h[i]
        break
    elif h[i] == -1:
        ans *= min(T[i], A[i])
        ans %= mod

if A[N - 1] != T[0]:
    print(0)
else:
    print(ans % mod)

Submission status: Wrong Answer

Solution code:
N = int(input())
T = list(map(int, input().split(" ")))
A = list(map(int, input().split(" ")))
h = [-1 for i in range(N)]
mod = 10 ** 9 + 7

h[0] = T[0]
h[N - 1] = A[N - 1]
for i in range(N - 1):
    if T[i] < T[i + 1]:
        h[i + 1] = T[i + 1]

for i in range(N - 1):
    if A[i + 1] < A[i] and (h[i] == -1 or h[i] == A[i]):
        h[i] = A[i]
    elif A[i + 1] < A[i]:
        h[i] = 0

ans = 1
for i in range(N):
    if h[i] == 0:
        ans *= h[i]
        break
    elif h[i] == -1:
        ans *= min(T[i], A[i])
        ans %= mod

print(ans % mod)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
T = list(map(int,input().split()))
A = list(map(int,input().split()))
mod = 10**9+7
if max(A)!=max(T):
    print(0)
    exit()

l = [True] + [False]*(n-1)
r = [False]*(n-1) + [True]
for i in range(1, n):
    if T[i] != T[i-1]:
        l[i] = True
for i in range(n-1):
    if A[i] != A[i+1]:
        r[i] = True
ans = 1
for i in range(n):
    if l[i]==False and r[i]==False:
        ans *= min(T[i], A[i])
        ans %= mod
print(ans%mod)

Submission status: Wrong Answer

Solution code:
n = int(input())
t = list(map(int, input().split()))
a = list(map(int, input().split()))
mod = 10 ** 9 + 7

if max(t) != max(a):
    print(0)
    exit()


def f(li):
    ret = [0] * n
    mx = 0
    cnt = 0
    for i, e in enumerate(li):
        if e > mx:
            ret[i] = 1
            cnt = e
            mx = e
        else:
            ret[i] = cnt

    return ret


tt = f(t)
aa = f(a[::-1])[::-1]
ans = 1
for e1, e2 in zip(tt, aa):
    ans *= min(e1, e2)
    ans %= mod

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
T = list(map(int, input().split(' ')))
A = list(map(int, input().split(' ')))


nums = [-1 for _ in range(N)]
prev = -1
for i in range(N):
    if T[i] != prev:
        nums[i] = T[i]

    prev = T[i]

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

        nums[i] = A[i]

    prev = A[i]

ret = 1
for i in range(N):
    if nums[i] == -1:
        ret *= min(A[i], T[i])
        ret %= 10**9 + 7

print(ret)

Submission status: Wrong Answer

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

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]>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()
        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: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline
from operator import itemgetter

def main():
    N = int(input())
    
    t = list(map(int,input().split()))
    a = list(map(int,input().split()))
    MOD = 10**9+7
    
    tt,tl = 0,[False for _ in range(N)]
    for x,num in enumerate(t):
        if num > tt:
            tl[x] = True
            tt = num
            
    at,al = 0,[False for _ in range(N)]
    for x,num in enumerate(a[::-1]):
        if num > at:
            al[-x-1] = True
            at = num

    ans,now = 1,0
    for i in range(N):
        if tl[i] == True and al[i] == True:
            if now < t[i] == a[i]:
                now = min(t[i],a[i])
            else:
                print(-1)
                exit()
        elif tl[i] == True and al[i] == False:
            if now > t[i]:
                print(-1)
                exit()
            else:
                now = t[i]
        elif tl[i] == False and al[i] == True:
            if now < a[i]:
                print(-1)
                exit()
            else:
                now = a[i]
        else:
            ans *= min(t[i],a[i])
            ans %= MOD
    
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline
from operator import itemgetter

def main():
    N = int(input())
    
    t = list(map(int,input().split()))
    a = list(map(int,input().split()))
    MOD = 10**9+7
    
    tt,tl = 0,[False for _ in range(N)]
    for x,num in enumerate(t):
        if num > tt:
            tl[x] = True
            tt = num
            
    at,al = 0,[False for _ in range(N)]
    for x,num in enumerate(a[::-1]):
        if num > at:
            al[-x-1] = True
            at = num
            
    print(tl,al)
    ans,now = 1,0
    for i in range(N):
        print(i,now)
        if tl[i] == True and al[i] == True:
            if now < t[i] == a[i]:
                now = min(t[i],a[i])
            else:
                print(-1)
                exit()
        elif tl[i] == True and al[i] == False:
            if now > t[i]:
                print(-1)
                exit()
            else:
                now = t[i]
        elif tl[i] == False and al[i] == True:
            if now < a[i]:
                print(-1)
                exit()
            else:
                now = a[i]
        else:
            ans *= min(t[i],a[i])
            ans %= MOD
    
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

N = int(input())
T = list(map(int, input().split()))
A = list(map(int, input().split()))
maxh = [0] * N
if T[0] > A[0] or T[-1] < A[-1]:
    print(0)
    exit()

maxh[0] = -T[0]
for i in range(1, N):
    if T[i-1] == T[i]:
        maxh[i] = T[i]
    else:
        maxh[i] = -T[i]

maxh[-1] = -A[-1]
for i in range(N-1)[::-1]:
    if maxh[i] > 0:
        if A[i] == A[i+1]:
            maxh[i] = min(maxh[i], A[i])
        else:
            maxh[i] = -A[i]
    else:
        if A[i] == A[i+1]:
            if -maxh[i] < A[i]:
                print(0)
                exit()
        else:
            if -maxh[i] != A[i]:
                print(0)
                exit()

ans = 1
mod = 10**9+7
for i in range(N):
    if maxh[i] > 0:
        ans = ans * maxh[i] % mod
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
t = list(map(int, input().split()))
a = list(map(int, input().split()))
mod = 10**9 + 7
#どちらかの値が切り替わってるところは、1通りしかなく、そこが矛盾している場合は弾く
h = [-1 for i in range(n)]
t = [0] + t
a += [0]
for i in range(n):
    if t[i+1] != t[i]:
        h[i] = t[i+1]
for i in range(n,0,-1):
    if a[i] != a[i-1]:
        if h[i-1] == -1:
            h[i-1] = a[i-1]
        else:
            if h[i-1] != a[i-1]:
                print(0)
                exit()
ans = 1
for i in range(n):
    if h[i] == -1:
        #print("index:{} t:{} a:{}".format(i,t[i+1], a[i]))
        ans *= min(t[i+1], a[i])
        ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
t = list(map(int, input().split()))
a = list(map(int, input().split()))
mod = 10**9 + 7
#どちらかの値が切り替わってるところは、1通りしかなく、そこが矛盾している場合は弾く
h = [-1 for i in range(n)]
t = [0] + t
a += [0]
for i in range(n):
    if t[i+1] != t[i]:
        h[i] = t[i+1]
for i in range(n,0,-1):
    if a[i] != a[i-1]:
        if h[i-1] != -1 and h[i-1] != a[i-1]:
            print(0)
            exit()
        else:
            h[i-1] = a[i-1]
ans = 1
for i in range(n):
    if h[i] == -1:
        ans *= min(t[i+1], a[i])
        ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

def solve():
    input = sys.stdin.readline
    N = int(input())
    T = [int(t) for t in input().split()]
    A = [int(a) for a in input().split()]
    mod = 7 + 10 ** 9
    Ans = 1
    H = [-1] * N
    H[0] = T[0]
    for i in range(1, N):
        if T[i] > T[i-1]: H[i] = T[i]
    if H[N-1] > -1 and H[N-1] != A[N-1]: Ans = 0
    else: H[N-1] = A[N-1]
    for i in reversed(range(N-1)):
        if A[i] > A[i+1]:
            if H[i] > -1 and H[i] != A[i]: Ans = 0
            else: H[i] = A[i]
        else:
            if H[i] == -1: 
                Ans *= min(T[i], A[i])
                Ans %= mod
    print(Ans)
            
    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

H = [[-1, 10**9] for i in range(N)]

prev = 0
for i, x in enumerate(T):
    if prev != x:
        if H[i][0] < 0:
            H[i][0] = x
        elif H[i][0] != x:
            print(0)
            exit(0)
    prev = x
    H[i][1] = min(H[i][1], x)
prev = 0
for i in range(N-1, -1, -1):
    x = A[i]
    if prev != x:
        if H[i][0] < 0:
            H[i][0] = x
        elif H[i][0] != x:
            print(0)
            exit(0)
    prev = x
    H[i][1] = min(H[i][1], x)
ans = 1
mod = 10**9 + 7
for x, y in H:
    if x > 0:
        continue
    else:
        ans *= y
        ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
MOD=10**9+7
class Fp(int):
    def __new__(self,x=0):return super().__new__(self,x%MOD)
    def inv(self):return self.__class__(super().__pow__(MOD-2,MOD))
    def __add__(self,value):return self.__class__(super().__add__(value))
    def __sub__(self,value):return self.__class__(super().__sub__(value))
    def __mul__(self,value):return self.__class__(super().__mul__(value))
    def __floordiv__(self,value):return self.__class__(self*self.__class__(value).inv())
    def __pow__(self,value):return self.__class__(super().__pow__(value%(MOD-1), MOD))
    __radd__=__add__
    __rmul__=__mul__
    def __rsub__(self,value):return self.__class__(-super().__sub__(value))
    def __rfloordiv__(self,value):return self.__class__(self.inv()*value)
    def __iadd__(self,value):self=self+value;return self
    def __isub__(self,value):self=self-value;return self
    def __imul__(self,value):self=self*value;return self
    def __ifloordiv__(self,value):self=self //value;return self
    def __ipow__(self,value):self=self**value;return self
    def __neg__(self):return self.__class__(super().__neg__())

N=int(input())
T=list(map(int, input().split()))+[0]
A=list(map(int, input().split()))+[0]
ans=Fp(1)
for i in range(N):
    if T[i-1]<T[i] and A[i]>A[i+1]:
        ans*=T[i]==A[i]
    elif not T[i-1]<T[i] and not A[i]>A[i+1]:
        ans*=min(T[i],A[i])
print(ans)

Submission status: Wrong Answer

Solution code:
MOD=10**9+7
class Fp(int):
    def __new__(self,x=0):return super().__new__(self,x%MOD)
    def inv(self):return self.__class__(super().__pow__(MOD-2,MOD))
    def __add__(self,value):return self.__class__(super().__add__(value))
    def __sub__(self,value):return self.__class__(super().__sub__(value))
    def __mul__(self,value):return self.__class__(super().__mul__(value))
    def __floordiv__(self,value):return self.__class__(self*self.__class__(value).inv())
    def __pow__(self,value):return self.__class__(super().__pow__(value%(MOD-1), MOD))
    __radd__=__add__
    __rmul__=__mul__
    def __rsub__(self,value):return self.__class__(-super().__sub__(value))
    def __rfloordiv__(self,value):return self.__class__(self.inv()*value)
    def __iadd__(self,value):self=self+value;return self
    def __isub__(self,value):self=self-value;return self
    def __imul__(self,value):self=self*value;return self
    def __ifloordiv__(self,value):self=self //value;return self
    def __ipow__(self,value):self=self**value;return self
    def __neg__(self):return self.__class__(super().__neg__())

def ng():
    print(0)
    exit()
N=int(input())
T=list(map(int, input().split()))
A=list(map(int, input().split()))
if T[-1]!=A[0]: ng()
M=[-1]*N
M[0]=T[0]
for i in range(N-1):
    if T[i]<T[i+1]:
        M[i+1]=T[i+1]
if M[-1]>0 and M[-1]!=A[-1]: ng()
M[-1]=A[-1]
for i in range(N-1)[::-1]:
    if A[i]>A[i+1]:
        if M[i]<0:
            M[i]=A[i]
        elif M[i]!=A[i]:
            ng()
ans=Fp(1)
for i in range(N):
    if M[i]<0:
        ans*=min(T[i],A[i])
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
from itertools import groupby

N = int(input())
T = np.array(input().split(), np.int64)
A = np.array(input().split(), np.int64)
mod = 10 ** 9 + 7

t = (T[1:] != T[:-1]) * T[1:]
a = (A[1:] != A[:-1]) * A[:-1]
seq = np.zeros(N, np.int64)
seq[0] = T[0]
seq[-1] = A[-1]

flag = True
tmp = np.where((t[:-1] != 0) & (a[1:] != 0))[0]
if np.any(t[tmp] != a[tmp + 1]):
    flag = False
seq[1:-1] = np.maximum(t[:-1], a[1:])
if np.any(seq > T) or np.any(seq > A):
    flag = False

if flag:
    group = [(i, len(list(j))) for i, j in groupby(seq.tolist())]
    answer = 1
    for i in range(len(group)):
        if group[i][0] == 0:
            answer *= pow(min(group[i - 1][0], group[i + 1][0]), group[i][1], mod)
            answer %= mod
    print(answer)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
import numpy as np
from itertools import groupby

N = int(input())
T = np.array(input().split(), np.int64)
A = np.array(input().split(), np.int64)
mod = 10 ** 9 + 7

t = (T[1:] != T[:-1]) * T[1:]
a = (A[1:] != A[:-1]) * A[:-1]
seq = np.zeros(N, np.int64)
seq[0] = T[0]
seq[-1] = A[-1]

flag = True
tmp = np.where((t[:-1] != 0) & (a[1:] != 0))[0]
if np.any(t[tmp] != a[tmp + 1]):
    flag = False
seq[1:-1] = np.maximum(t[:-1], a[1:])
if np.any(seq > T) and np.any(seq > A):
    flag = False

if flag:
    group = [(i, len(list(j))) for i, j in groupby(seq.tolist())]
    answer = 1
    for i in range(len(group)):
        if group[i][0] == 0:
            answer *= pow(min(group[i - 1][0], group[i + 1][0]), group[i][1], mod)
            answer %= mod
    print(answer)
else:
    print(0)