Problem:


Score : 400 points

Problem Statement

Mountaineers Mr. Takahashi and Mr. Aoki recently trekked across a certain famous mountain range.
The mountain range consists of N mountains, extending from west to east in a straight line as Mt. 1, Mt. 2, ..., Mt. N.
Mr. Takahashi traversed the range from the west and Mr. Aoki from the east.  

The height of Mt. i is h_i, but they have forgotten the value of each h_i.
Instead, for each i (1 ≤ i ≤ N), they recorded the maximum height of the mountains climbed up to the time they reached the peak of Mt. i (including Mt. i).
Mr. Takahashi's record is T_i and Mr. Aoki's record is A_i.  

We know that the height of each mountain h_i is a positive integer.
Compute the number of the possible sequences of the mountains' heights, modulo 10^9 + 7.

Note that the records may be incorrect and thus there may be no possible sequence of the mountains' heights.
In such a case, output 0.

Constraints

1 ≤ N ≤ 10^5

1 ≤ T_i ≤ 10^9

1 ≤ A_i ≤ 10^9

T_i ≤ T_{i+1} (1 ≤ i ≤ N - 1)

A_i ≥ A_{i+1} (1 ≤ i ≤ N - 1)

Input

The input is given from Standard Input in the following format:

N
T_1 T_2 ... T_N
A_1 A_2 ... A_N

Output

Print the number of possible sequences of the mountains' heights, modulo 10^9 + 7.

Sample Input 1

5
1 3 3 3 3
3 3 2 2 2

Sample Output 1

4

The possible sequences of the mountains' heights are:

1, 3, 2, 2, 2 

1, 3, 2, 1, 2 

1, 3, 1, 2, 2 

1, 3, 1, 1, 2 

for a total of four sequences.

Sample Input 2

5
1 1 1 2 2
3 2 1 1 1

Sample Output 2

0

The records are contradictory, since Mr. Takahashi recorded 2 as the highest peak after climbing all the mountains but Mr. Aoki recorded 3.

Sample Input 3

10
1 3776 3776 8848 8848 8848 8848 8848 8848 8848
8848 8848 8848 8848 8848 8848 8848 8848 3776 5

Sample Output 3

884111967

Don't forget to compute the number modulo 10^9 + 7.

Sample Input 4

1
17
17

Sample Output 4

1

Some mountain ranges consist of only one mountain.



Sample input:
5
1 3 3 3 3
3 3 2 2 2


Sample output:
4

Submission status: Wrong Answer

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
from heapq import heapify, heappop, heappush
import math
import random
import string
from copy import deepcopy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

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

#############
# Main Code #
#############

N = getN()
T = getList()
A = getList()

"""
山の高さの列が何通りあるか　dp? 組み合わせ?
dpなら漸化式を考える

5
1 3 3 3 3 高橋くんは左 → 右
3 3 2 2 2 青木くんは右 → 左
最大値のみをレコード

最大値が更新された時のみ高さがわかる
1 3 ? ? ?
? 3 ? ? 2

1 3 ? ? 2
3番目は3以下かつ2以下
4番目は3以下かつ2以下

5
1 1 1 2 2
3 2 1 1 1

1 ? ? 2 ?
3 2 ? ? 1 矛盾
"""

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

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

c_det = [-1] * N
for i in range(N):
    if c_t[i] >= 0 and c_a[i] >= 0:
        if c_t[i] != c_a[i]:
            print(0)
            exit()
        else:
            c_det[i] = c_t[i]
    elif c_t[i] >= 0:
        c_det[i] = c_t[i]
    elif c_a[i] >= 0:
        c_det[i] = c_a[i]

ans = 1
for i in range(N):
    if c_det[i] == -1:
        ans *= min(T[i], A[i])
        ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
mod = int(1e9) + 7
N = int(input())
T = list(map(int, input().split()))
A = list(map(int, input().split()))
LM = [-1 for _ in range(N)]
RM = [-1 for _ in range(N)]
LM[0] = T[0]
S = 1
for i in range(1, N):
    if T[i-1] != T[i]:
        LM[i] = T[i]
RM[-1] = A[-1]
for i in range(N, 1, -1):
    if A[i-1] != A[i-2]:
        RM[i-2] = A[i-2]
for i in range(N):
    if LM[i] != -1 and RM[i] != -1:
        if LM[i] != RM[i]:
            S *= 0
            break
    else:
        if LM[i] == -1 and RM[i] == -1:
            S *= min(T[i], A[i])
            S %= mod
print(S)

Submission status: Wrong Answer

Solution code:
N = int(input())
T = list(map(int, input().split()))
A = list(map(int, input().split()))
INF = 10**13
minH = [1]*N
maxH = [INF]*N
pre_ti = 0
MOD = 10**9 + 7
for i, ti in enumerate(T):
    if ti > pre_ti:
        minH[i] = ti
        maxH[i] = ti
    else:
        maxH[i] = min(ti, maxH[i])
    pre_ti = ti
pre_ai = 0
for i, ai in enumerate(A[::-1], 1):
    if ai > pre_ai:
        minH[-i] = ai
        maxH[-i] = ai
    else:
        maxH[-i] = min(ai, maxH[-i])
    pre_ai = ai

ans = 1
for lh, uh in zip(minH, maxH):
    if lh > uh:
        ans = 0
        break
    ans = (ans * (uh-lh+1)) % MOD
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
t=list(map(int,input().split()))
a=list(map(int,input().split()))
tt=[float("inf") for i in range(n)]
ans=[False for i in range(n)]
tmp=0
mod=10**9+7
for i in range(n):
    if t[i]>tmp:
        
        tt[i]=t[i]
        ans[i]=True
        tmp=t[i]
    else:
        tt[i]=tmp
tmp=0
for i in range(n-1,-1,-1):
    if a[i]>tmp:
        if ans[i]==True and a[i]!=t[i]:
            print(0)
            exit()
        tt[i]=a[i]
        ans[i]=True
        tmp=a[i]
    else:
        if ans[i]==False and tmp<tt[i]:
            tt[i]=tmp    
tmp=1
for i in range(n):
    if ans[i]==False:
        tmp*=tt[i]
        tmp%=mod
print(tmp%mod)

Submission status: Wrong Answer

Solution code:
n=int(input())
t=list(map(int,input().split()))
a=list(map(int,input().split()))
tt=[float("inf") for i in range(n)]
ans=[False for i in range(n)]
tmp=0
mod=10**9+7
for i in range(n):
    if t[i]>tmp:
        
        tt[i]=t[i]
        ans[i]=True
        tmp=t[i]
    elif tt[i]>tmp:
        tt[i]=tmp
tmp=0
for i in range(n-1,-1,-1):
    if a[i]>tmp:
        if ans[i]==True:
            print(0)
            exit()
        tt[i]=a[i]
        ans[i]=True
        tmp=a[i]
    elif tt[i]>tmp:
        tt[i]=tmp    
tmp=1
for i in range(n):
    if ans[i]==False:
        tmp*=tt[i]
        tmp%=mod
print(tmp%mod)

Submission status: Wrong Answer

Solution code:
n=int(input())
t=list(map(int,input().split()))
a=list(map(int,input().split()))
tt=[float("inf") for i in range(n)]
ans=[False for i in range(n)]
tmp=0
mod=10**9+7
for i in range(n):
    if t[i]>tmp:
        if ans[i]==True:
            print(0)
            exit()
        tt[i]=t[i]
        ans[i]=True
        tmp=t[i]
    elif tt[i]>tmp:
        tt[i]=tmp
tmp=0
for i in range(n-1,-1,-1):
    if a[i]>tmp:
        tt[i]=a[i]
        ans[i]=True
        tmp=a[i]
    elif tt[i]>tmp:
        tt[i]=tmp    
tmp=1
for i in range(n):
    if ans[i]==False:
        tmp*=tt[i]
        tmp%=mod
print(tmp%mod)

Submission status: Wrong Answer

Solution code:
def main():
    MOD = 10 ** 9 + 7
    N = int(input())
    T = list(map(int, input().split()))
    A = list(map(int, input().split()))
    fixed = [0] * N
    max_t = 0
    for i in range(N):
        t = T[i]
        if t > max_t:
            fixed[i] = 1
        max_t = max(max_t, t)
    max_a = 0
    for i in range(N - 1, -1, -1):
        a = A[i]
        if a > max_a:
            if fixed[i] == 1 and a != T[i]:
                print(0)
                return
            fixed[i] = 1
        max_a = max(max_a, a)
    ans = 1
    for i in range(N):
        if fixed[i] == 1:
            continue
        bound_h = min(A[i], T[i])
        ans *= bound_h
        ans %= MOD
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import math
from collections import deque
import copy
import bisect
MOD=10**9+7
n=int(input())
#k, n = map(int,input().split())
T = list(map(int,input().split()))
A = list(map(int,input().split()))
L=[0]*n
L[0]=T[0]
L[n-1]=A[n-1]
for i in range(n-2):
    if T[i]!=T[i+1]:
        L[i+1]=T[i+1]
    else:
        L[i+1]=-1
#print(L)
for i in range(n-1, 0, -1):
    if A[i]!=A[i-1]:
        L[i-1]=A[i-1]
ans=1
cnt=0
#print(L)

flg=False#-1を数えているフラグ
for i in range(n):
    if L[i]==-1:
        flg=True
        cnt+=1
    else:
        if not flg:
            a=L[i]
        else:
            ans*=min(L[i], a)**cnt
            ans%=MOD
            cnt=0
            flg=False
print(ans)

Submission status: Wrong Answer

Solution code:
MOD=10**9+7
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
if N==1:
    print(1 if A[0]==B[0] else 0)
    exit()

ans=1
tmp=[-1]*N
for i in range(N):
    if i==0 or (i>0 and A[i-1]<A[i]):
        tmp[i]=A[i]
for i in reversed(range(N)):
    if i==N-1 or (i<N-1 and B[i]>B[i+1]):
        if tmp[i]!=-1 and tmp[i]!=B[i]:
            print(0)
            exit()
        tmp[i]=B[i]
for i in range(N):
    if tmp[i]==-1:
        ans=ans*min(A[i],B[i])%MOD
print(ans)

Submission status: Wrong Answer

Solution code:
MOD=10**9+7
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
if N==1:
    print(1 if A[0]==B[0] else 0)
    exit()

ans=1
for i in range(N):
    if i==0 or (i>0 and A[i-1]<A[i]):
        if (i==N-1 or (i<N-1 and B[i]>B[i+1])) and B[i]!=A[i]:
            ans=0
            break
    elif i==N-1 or (i<N-1 and B[i]>B[i+1]):
        if (i==0 or (i>0 and A[i-1]<A[i])) and A[i]!=B[i]:
            ans=0
            break
    else:
        ans=ans*min(A[i],B[i])%MOD
    
print(ans)

Submission status: Wrong Answer

Solution code:
MOD=10**9+7
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
if N==1:
    print(1 if A[0]==B[0] else 0)
    exit()

if(A[N-2]<A[N-1] and B[N-1]!=A[N-1]) or (B[0]>B[1] and B[0]!=A[0]):
    print(0)
    exit()

ans=1
for i in range(1,N-1):
    if A[i-1]<A[i] and B[i]>B[i+1] and A[i]!=B[i]:
        print(0)
        exit()
    if A[i-1]>A[i] or B[i]<B[i+1]:
        print(0)
        exit()
    elif A[i-1]==A[i] and B[i]==B[i+1]:
        ans=ans*min(A[i],B[i])%MOD
    else:
        continue
print(ans)

Submission status: Wrong Answer

Solution code:
MOD=10**9+7
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
if N==1:
    print(1 if A[0]==B[0] else 0)
    exit()

if(A[N-2]<A[N-1] and B[N-1]!=A[N-1]) or (B[0]>B[1] and B[0]!=A[0]):
    print(0)
    exit()

ans=1
for i in range(1,N-1):
    if A[i-1]<A[i] and B[i]>B[i+1] and A[i]!=B[i]:
        print(0)
        exit()
    elif A[i-1]==A[i] and B[i]==B[i+1]:
        ans=ans*min(A[i],B[i])%MOD
    else:
        continue
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

tmax, amax = max(t), max(a)
if any(ti == ai == tmax == amax for ti, ai in zip(t, a)):
    print(0)
    exit()

ans = 1
for i in range(1, n - 1):
    if ((t[i - 1] < t[i] and t[i] > a[i])
        or (a[i] > a[i + 1] and t[i] < a[i])):
            print(0)
            break
    elif t[i - 1] == t[i] and a[i] == a[i + 1]:
        ans = ans * min(t[i], a[i]) % mod
else:
    print(ans)

Submission status: Wrong Answer

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
    indH=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
    indA = 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: Wrong Answer

Solution code:
import sys


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


mod = pow(10, 9)+7
N = int(input())
T = LI()
A = LI()
ans = 1
for i in range(1, N-1):
    if T[i-1] != T[i]:
        if A[i] < T[i]:
            print(0)
            sys.exit()
        continue
    if A[i] != A[i+1]:
        if A[i] > T[i]:
            print(0)
            sys.exit()
        continue
    ans = (ans * min(T[i], A[i])) % mod
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    N = int(input())
    T = list(map(int, input().split()))
    A = list(map(int, input().split()))

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

    upper = T[:]# それぞれの山の高さの上限
    flag = [0] * N# 山の高さが一意かどうか

    # Tくんの目線で見る
    now = 0
    for i in range(N):
        if T[i] > now:
            flag[i] = 1
            now = T[i]

    # A君の目線で見る
    now = 0
    for i in range(N):
        if A[N-1-i] > now:
            if flag[N-1-i] and T[N-1-i] != A[N-1-i]:# 矛盾
                print(0)
                exit()
            flag[N-1-i] = 1
            now = A[N-1-i]

        upper[N-1-i] = min(upper[N-1-i],A[N-1-i])

    ans = 1
    m = 10**9+7
    for i in range(N):
        if flag[i]==0:
            ans *= upper[i]
            ans %= m
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    N = int(input())
    T = list(map(int, input().split()))
    A = list(map(int, input().split()))

    upper = T[:]# それぞれの山の高さの上限
    flag = [0] * N# 山の高さが一意かどうか

    # Tくんの目線で見る
    now = 0
    for i in range(N):
        if T[i] > now:
            flag[i] = 1
            now = T[i]

    # A君の目線で見る
    now = 0
    for i in range(N):
        if A[N-1-i] > now:
            if flag[N-1-i] and T[N-1-i] != A[N-1-i]:# 矛盾
                print(0)
                exit()
            flag[N-1-i] = 1
            now = A[N-1-i]

        upper[N-1-i] = min(upper[N-1-i],A[N-1-i])

    ans = 1
    m = 10**9+7
    for i in range(N):
        if flag[i]==0:
            ans *= upper[i]
            ans %= m
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
Ts = list(mapint())
As = list(mapint())
mod = 10**9 + 7

maxi = [10**18]*N
confirmed = [-1]*N

now = 0
for i in range(N):
    t = Ts[i]
    if t>now:
        now = t
        confirmed[i] = t
    else:
        maxi[i] = t

now = 0
for i in range(N-1, -1, -1):
    a = As[i]
    if a>now:
        now = a
        if confirmed[i]==-1:
            confirmed[i] = a
        elif confirmed[i]!=a:
            print(0)
            exit()
    else:
        maxi[i] = min(maxi[i], a)

ans = 1
for i in range(N):
    if confirmed[i]!=-1:
        continue
    ans *= maxi[i]
    ans %= mod

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
arr = [[1] * n for _ in range(4)]
t = list(map(int, input().split()))
a = list(map(int, input().split()))[::-1]

MOD = 10**9 + 7

max_t = 0
max_a = 0

for i in range(n):
    if max_t < t[i]:
        arr[0][i] = t[i]
        arr[1][i] = t[i]
        max_t = t[i]
    else:
        arr[1][i] = t[i]
    if max_a < a[i]:
        arr[2][i] = a[i]
        arr[3][i] = a[i]
        max_a = a[i]
    else:
        arr[3][i] = a[i]

arr[2] = arr[2][::-1]
arr[3] = arr[3][::-1]

ans = 1
for i in range(n):
    ans *= (min(arr[1][i], arr[3][i]) - max(arr[0][i], arr[2][i]) + 1)
    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()))
ans = 1; MOD = pow(10,9)+7
if N == 1:
  if A[0] == T[0]:
    print(1)
  else:
    print(0)
  exit()
for i in range(N):
  if i == 0: #T[0]以下
    if A[i] == A[i+1]: #なんでもおk
      fac = T[0]
    else: #A[i]じゃなとダメ
      if A[i] > T[0]: #矛盾
        fac = 0
      else:
        fac = 1
  elif i < N-1: #間
    if T[i] == T[i-1]: #T[i]以下ならなんでもおｋ
      if A[i] == A[i+1]: #A[i]以下ならなんでもおｋ
        fac = min(T[i],A[i])
      else: #A[i]じゃないとダメ
        if A[i] > T[i]: #矛盾
          fac = 0
        else:
          fac = 1
    else: #T[i]じゃないとだめ
      if A[i] == A[i+1]: #A[i]以下ならなんでもｋ
        if T[i] > A[i]: #矛盾
          fac = 0
        else:
          fac = 1
      else: #A[i]じゃないとだめ
        if T[i] == A[i]:
          fac = 1
        else:
          fac = 0
  else: #i = N-1。A[i]じゃないとダメ
    if T[i] == T[i-1]: #T[i]以下ならなんでもおｋ
      if A[i] > T[i]: #矛盾
        fac = 0
      else:
        fac = 1
    else: #T[i]じゃないとダメ
      if T[i] == A[i]:
        fac = 1
      else:
        fac = 0
  ans = (ans*fac)%MOD
print(ans%MOD)

Submission status: Wrong Answer

Solution code:
n = int(input())
tl = list(map(int, input().split()))
al = list(map(int, input().split()))

if tl[-1] != al[0]:
    print(0)
    exit()

ans = 1
for i in range(1,n-1):
    if tl[i] > tl[i-1] or al[i] > al[i+1]:
        ans *= 1
    else:
        ans *= min(tl[i],al[i])
    ans%=(10**9+7)

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
tl = list(map(int, input().split()))
al = list(map(int, input().split()))

ans = 1
for i in range(1,n-1):
    if tl[i] > tl[i-1] or al[i] > al[i+1]:
        ans *= 1
    else:
        ans *= min(tl[i],al[i])
    ans%=(10**9+7)

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

def err():
    print(0)
    quit()

n = inp()
a = inpl()
b = inpl()
de = [-1] * n
mx = [INF] * n
now = -1
for i in range(n):
    if a[i] > now:
        now = a[i]
        de[i] = now
    else:
        mx[i] = now
now = -1
for i in range(n)[::-1]:
    if b[i] > now:
        now = b[i]
        if de[i] != -1 and de[i] != now:
            err()
        de[i] = now
    else:
        mx[i] = min(mx[i], now)
res = 1
for i in range(n):
    if de[i] == -1:
        res *= mx[i]
        res %= mod
print(res)

Submission status: Wrong Answer

Solution code:
n=int(input())
ht=list(map(int,input().split()))
ha=list(map(int,input().split()))
ans=ha[0]==ht[-1] and ha[ht.index(ht[-1])]==ht[-1]
m=10**9+7
cnt=0
for i,(t,a) in enumerate(zip(ht[1:n-1],ha[1:n-1])):
  if t<a and t==ht[i]:ans*=t
  elif t>a and a==ha[i+2]:ans*=a
  cnt+=t==a
  ans%=m
ans*=pow(max(ha),max(cnt-2,0),m)
print(ans%m)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ai = [-1] * n
bi = [-1] * n
r = [-1] * n
ai[0]=a[0]
bi[-1]=b[-1]
for i in range(1,n):
  if a[i] > a[i-1]:
    ai[i] = a[i]
for i in range(n-2,-1,-1):
  if b[i] > b[i+1]:
    bi[i] = b[i]
ret = 1
r = [0] * n
for i in range(n):
  if ai[i]!=bi[i] and ai[i]!=-1 and bi[i]!=-1:
    print(0)
    exit()
  elif ai[i]==-1 and bi[i]==-1:
    ret *= min(a[i], b[i])
    ret %= 10**9+7
  else:
    r[i]=max(ai[i], bi[i])
x = 0
for i in range(n):
  x = max(x, r[i])
  if x!=a[i]:
    print(0)
    exit()
for i in range(n-1, -1, -1):
  x = max(x, r[i])
  if x!=b[i]:
    print(0)
    exit()
print(ret)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ai = [-1] * n
bi = [-1] * n
r = [-1] * n
ai[0]=a[0]
bi[-1]=b[-1]
for i in range(1,n):
  if a[i] > a[i-1]:
    ai[i] = a[i]
for i in range(n-2,-1,-1):
  if b[i] > b[i+1]:
    bi[i] = b[i]
ret = 1
for i in range(n):
  if ai[i]!=bi[i] and ai[i]!=-1 and bi[i]!=-1:
    print(0)
    exit()
  elif ai[i]==-1 and bi[i]==-1:
    ret *= min(a[i], b[i])
    ret %= 10**9+7
print(ret)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
import math
import copy
from heapq import heappush, heappop, heapify
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
# sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

def nck(n, k, kaijyo):
    return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD

def npk(n, k, kaijyo):
    if k == 0 or k == n:
        return n % MOD
    return (kaijyo[n] * divide(kaijyo[n-k])) % MOD

def fact_and_inv(SIZE):
    inv = [0] * SIZE  # inv[j] = j^{-1} mod MOD
    fac = [0] * SIZE  # fac[j] = j! mod MOD
    finv = [0] * SIZE  # finv[j] = (j!)^{-1} mod MOD
    inv[1] = 1
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    for i in range(2, SIZE):
        inv[i] = MOD - (MOD // i) * inv[MOD % i] % MOD
        fac[i] = fac[i - 1] * i % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    return fac, finv

def renritsu(A, Y):
    # example 2x + y = 3, x + 3y = 4
    # A = [[2,1], [1,3]])
    # Y = [[3],[4]] または [3,4]
    A = np.matrix(A)
    Y = np.matrix(Y)
    Y = np.reshape(Y, (-1, 1))
    X = np.linalg.solve(A, Y)

    # [1.0, 1.0]
    return X.flatten().tolist()[0]

class TwoDimGrid:
    # 2次元座標 -> 1次元
    def __init__(self, h, w, wall="#"):
        self.h = h
        self.w = w
        self.size = (h+2) * (w+2)
        self.wall = wall
        self.get_grid()
        self.init_cost()

    def get_grid(self):
        grid = [self.wall * (self.w + 2)]
        for i in range(self.h):
            grid.append(self.wall + getS() + self.wall)

        grid.append(self.wall * (self.w + 2))
        self.grid = grid
    def init_cost(self):
        self.cost = [INF] * self.size

    def pos(self, x, y):
        # 壁も含めて0-indexed 元々の座標だけ考えると1-indexed
        return y * (self.w + 2) + x
    def getgrid(self, x, y):
        return self.grid[y][x]
    def get(self, x, y):
        return self.cost[self.pos(x, y)]
    def set(self, x, y, v):
        self.cost[self.pos(x, y)] = v
        return
    def show(self):
        for i in range(self.h+2):
            print(self.cost[(self.w + 2) * i:(self.w + 2) * (i+1)])
    def showsome(self, tgt):
        for t in tgt:
            print(t)
        return
    def showsomejoin(self, tgt):
        for t in tgt:
            print("".join(t))
        return

    def search(self, start, goal):
        grid = self.grid
        cy, cx = start
        move = [(0, 1), (0, -1), (1, 0), (-1, 0)]
        warp = []
        for i in range(-2, 3):
            for j in range(-2, 3):
                if (i, j) not in move and (i != 0 or j != 0):
                    warp.append((i, j))
        # print(warp)
        d = deque()
        wd = deque()
        self.set(start[1], start[0], 0)
        d.append((0, cx, cy))
        while (d or wd):
            # print(len(d), len(wd))
            if d:
                cc, cx, cy = d.popleft()
            else:
                cc, cx, cy = wd.popleft()
            # print(cc, cx, cy)
            if self.get(cx, cy) < cc:
                continue
            for dx, dy in move:

                nx, ny = dx + cx, dy + cy
                if self.getgrid(nx, ny) == "#":
                    continue
                if self.get(nx, ny) > cc:
                    self.set(nx, ny, cc)
                    d.append((cc, nx, ny))

            for dx, dy in warp:
                nx, ny = dx + cx, dy + cy
                if nx < 0 or nx > self.w + 1 or ny < 0 or ny > self.h + 1:
                    continue
                if self.getgrid(nx, ny) == "#":
                    continue
                if self.get(nx, ny) > cc + 1:
                    self.set(nx, ny, cc + 1)
                    wd.append((cc + 1, nx, ny))
        # print(warp)
        # self.show()
        # self.showsomejoin(self.grid)
        ans = self.get(goal[1], goal[0])
        if ans == INF:
            print(-1)
            return
        else:
            print(ans)
            return

def solve():
    n = getN()
    anums = getList()
    bnums = getList()

    fixed = [0 for i in range(n)]
    ans = [INF for i in range(n)]
    fixed[0] = 1
    fixed[-1] = 1
    ans[0] = anums[0]
    ans[-1] = bnums[-1]
    for i in range(n-1):
        if anums[i] != anums[i+1]:
            if fixed[i+1] and ans[i+1] != anums[i+1]:
                print(0)
                return
            if ans[i+1] < anums[i+1]:
                print(0)
                return
            fixed[i+1] = 1
        ans[i+1] = min(ans[i+1], anums[i+1])

    for i in range(n-1):
        if bnums[i] != bnums[i+1]:
            if fixed[i] and ans[i] != bnums[i]:
                print(0)
                return
            if ans[i] < bnums[i]:
                print(0)
                return
            fixed[i] = 1
        ans[i] = min(ans[i], bnums[i])

    a = 1
    for i in range(n):
        if not fixed[i]:
            a *= ans[i]
            a %= MOD

    print(a)

def main():
    n = getN()
    for _ in range(n):
        solve()

    return
if __name__ == "__main__":
    # main()
    solve()

Submission status: Wrong Answer

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

ans, l, r = 1, 0, n - 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 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: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *

N = INT()
T = LIST()
A = LIST()

mountain = [0]*N

h_t = 0
for i in range(N):
	if T[i] != h_t:
		h_t = T[i]
		mountain[i] = T[i]

h_a = 0
for i in range(N-1, -1, -1):
	if h_a < mountain[i]:
		print(0)
		exit()
	if A[i] != h_a:
		h_a = A[i]
		if mountain[i] == 0:
			mountain[i] = A[i]
		elif mountain[i] != A[i]:
			print(0)
			exit()

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

print(ans)

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *

N = INT()
T = LIST()
A = LIST()

mountain = [0]*N

h_t = 0
for i in range(N):
	if T[i] != h_t:
		h_t = T[i]
		mountain[i] = T[i]

h_a = 0
for i in range(N-1, -1, -1):
	if A[i] != h_a:
		h_a = A[i]
		if mountain[i] == 0:
			mountain[i] = A[i]
		elif mountain[i] != A[i]:
			print(0)
			exit()

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

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect, copy, heapq, math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

kakutei=[-1]*n
kakutei[0]=t[0]
kakutei[-1]=a[-1]
for i in range(n-1):
    if t[i]<t[i+1]:
        kakutei[i+1]=t[i+1]
    if a[-2-i]>a[-1-i]:
        kakutei[-2-i]=a[-2-i]
# print(kakutei)

ans=1
for i in range(n):
    if kakutei[i]>min(t[i],a[i]):
        ans=0

for i in range(n):
    if kakutei[i]==-1:
        ans*=min(t[i],a[i])
    ans%=mod

print(ans)