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:
# code-festival-2016-qualcC - 二人のアルピニスト / Two Alpinists
from bisect import bisect_left as bs


def main():
    """
      /----\     <- xi (= yi)
     /      \--\
    /           \
      xi  yi
    0 ~ xi / xi ~ yi / yi+1 ~ N-1
    """
    N = int(input())
    T = tuple(map(int, input().split()))
    A = tuple(map(int, input().split()))
    x, y, xi, yi = 0, 0, 0, 0  # max of T, A and the indices
    for i, t in enumerate(T):
        if t > x:
            x, xi = t, i
    for i, a in enumerate(A):
        if a > y:
            y, yi = t, i
    if x != y or x > y:
        print(0)
        return
    MOD = 10 ** 9 + 7
    ans, cur = 1, 0
    for i in T[: xi + 1]:
        if i > cur:  # point of new altitude -> no choice (ans * 1)
            cur = i
        else:
            ans = (ans * cur) % MOD  # mulitple choices (1, 2, ..., cur)
    for _ in range(xi + 1, yi):
        ans = (ans * cur) % MOD
    for i in A[yi + 1 :]:
        if i < cur:
            cur = i
        else:
            ans = (ans * cur) % MOD
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# code-festival-2016-qualcC - 二人のアルピニスト / Two Alpinists
from bisect import bisect_left as bs


def main():
    """
      /----\     <- xi (= yi)
     /      \--\
    /           \
      xi  yi
    0 ~ xi / xi ~ yi / yi+1 ~ N-1
    """
    N = int(input())
    T = tuple(map(int, input().split()))
    A = tuple(map(int, input().split()))
    x, y = T[-1], A[0]  # max of T, A
    xi = bs(T, x)  # index of x
    yi = (N - 1) - bs(A[::-1], y)  # index of y
    if x != y or x > y:
        print(0)
        return
    MOD = 10 ** 9 + 7
    ans, cur = 1, 0
    for i in T[: xi + 1]:
        if i > cur:  # point of new altitude -> no choice (ans * 1)
            cur = i
        else:
            ans = (ans * cur) % MOD  # mulitple choices (1, 2, ..., cur)
    for _ in range(xi + 1, yi):
        ans = (ans * cur) % MOD
    for i in A[yi + 1 :]:
        if i < cur:
            cur = i
        else:
            ans = (ans * cur) % MOD
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# code-festival-2016-qualcC - 二人のアルピニスト / Two Alpinists
import sys
input = sys.stdin.readline

from bisect import bisect_left as bs


def main():
    """
      /----\     <- xi (= yi)
     /      \--\
    /           \
      xi  yi
    0 ~ xi / xi ~ yi / yi+1 ~ N-1
    """
    N = int(input())
    T = tuple(map(int, input().split()))
    A = tuple(map(int, input().split()))
    x, y = T[-1], A[0]  # max of T, A
    xi = bs(T, x)  # index of x
    yi = (N - 1) - bs(A[::-1], y)  # index of y
    if x != y or x > y:
        print(0)
        return
    MOD = 10 ** 9 + 7
    ans, cur = 1, 0
    for i in T[: xi + 1]:
        if i > cur:  # point of new altitude -> no choice (ans * 1)
            cur = i
        else:
            ans = (ans * cur) % MOD  # mulitple choices (1, 2, ..., cur)
    for _ in T[xi + 1 : yi]:
        ans = (ans * cur) % MOD
    for i in A[yi + 1 :]:
        if i < cur:
            cur = i
        else:
            ans = (ans * cur) % MOD
    print(ans)


if __name__ == "__main__":
    main()

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

li_t = []
max_t = 0
for i in range(n):
    if t[i] > max_t:
        li_t.append((t[i], t[i] + 1))
        max_t = t[i]
    else:
        li_t.append((1, max_t + 1))

li_a = []
max_a = 0
for i in range(n)[::-1]:
    if a[i] > max_a:
        li_a.append((a[i], a[i] + 1))
        max_a = a[i]
    else:
        li_a.append((1, max_a + 1))
li_a = li_a[::-1]

ans = 1
for i in range(n):
    tmp = min(li_t[i][1], li_a[i][1]) - max(li_t[i][0], li_a[i][0])
    ans *= tmp
    ans %= MOD
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import heapq
from operator import itemgetter
from collections import deque, defaultdict
from bisect import bisect_left, bisect_right
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
MOD = 10**9 + 7

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

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

    minHeight = [float('inf')] * N
    minHeight[0] = -1
    minHeight[N - 1] = -1

    for i in range(1, N - 1):
        if T[i] > T[i - 1]:
            minHeight[i] = -1
        if A[i] > A[i + 1]:
            minHeight[i] = -1
        minHeight[i] = min(minHeight[i], A[i], T[i])

    ans = 1
    for h in minHeight:
        if h == -1:
            continue
        ans = (ans * h) % MOD

    print(ans)

sol()

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[0]+list(map(int,input().split()))
b=list(map(int,input().split()))+[0]

m=[a[i] for i in range(1,n+1)]
l=[1 for _ in range(n)]
ans=1

for i in range(n):
	if a[i]<a[i+1]:
		l[i]=a[i+1]
		
for i in range(n-1,-1,-1):
	if b[i+1]<b[i]:
		if l[i]<b[i]:
			ans=0
		else:
			l[i]=b[i]
	m[i]=min(m[i],b[i])

for i in range(n):
	ans=(ans*(m[i]-l[i]+1))%(10**9+7)
	if m[i]<l[i]:
		ans=0
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[0]+list(map(int,input().split()))
b=list(map(int,input().split()))+[0]

m=[a[i] for i in range(1,n+1)]
l=[1 for _ in range(n)]

for i in range(n):
	if a[i]<a[i+1]:
		l[i]=a[i+1]
		
for i in range(n-1,-1,-1):
	if b[i+1]<b[i]:
		l[i]=b[i]
	m[i]=min(m[i],b[i])
	
ans=1
for i in range(n):
	ans=(ans*(m[i]-l[i]+1))%(10**9+7)
	if m[i]<l[i]:
		ans=0
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
T,A=[list(map(int,input().split())) for i in range(2)]
tp,ap=0,A[0]
f=0
r=1
mod=10**9+7
x=0
if T[-1]!=A[0]:
    f=-1
for t,a in zip(T,A):
    if f==-1:
        break
    if f==0:
        if t==tp and a==ap:
            r=(r*t)%mod
        elif t==a==ap:
            f=1
        elif not (t>tp and a==ap):
            f=-1
    elif f==1:
        if t==a==ap:
            x+=1
        elif t==tp and a<ap:
            for j in range(x-1):
                r=(r*t)%mod
            f=2
        else:
            f=-1
    elif f==2:
        if t==tp and a==ap:
            r=(r*a)%mod
        elif not (t==tp and a<ap):
            f=-1
    tp,ap=t,a
print(0 if f<1 else r)

Submission status: Wrong Answer

Solution code:
N=int(input())
T,A=[list(map(int,input().split())) for i in range(2)]
tp,ap=0,A[0]
f=0
r=1
mod=10**9+7
x=0
if T[-1]!=A[0]:
    f=-1
for t,a in zip(T,A):
    if f==-1:
        break
    if f==0:
        if t==tp and a==ap:
            r=(r*t)%mod
        elif t==a:
            f=1
        elif not (t>tp and a==ap):
            f=-1
    elif f==1:
        if t==a==ap:
            x+=1
        elif t==tp and a<ap:
            for j in range(x-1):
                r=(r*t)%mod
            f=2
        else:
            f=-1
    elif f==2:
        if t==tp and a==ap:
            r=(r*a)%mod
        elif not (t==tp and a<ap):
            f=-1
    tp,ap=t,a
print(0 if f<1 else r)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n = ni()
t = list(li())
a = list(li())

MOD = 10**9 + 7

max_height = [0]*n
fixed = [False] * n
fixed[0] = fixed[-1] = True

max_height[0] = t[0]
max_height[-1] = a[-1]

ok = True

for i in range(n-1):
    if t[i+1] > t[i]:
        if fixed[i+1] and (max_height[i+1] != t[i+1]):
            ok = False
            break

        else:
            max_height[i+1] = t[i+1]
            fixed[i+1] = True

    if a[n-i-2] > a[n-i-1]:
        if fixed[n-i-2] and (max_height[n-i-2] != a[n-i-2]):
            ok = False
            break

        else:
            max_height[n-i-2] = a[n-i-2]
            fixed[n-i-2] = True

for i in range(n):
    if fixed[i]:
        if max_height[i] > a[i] or max_height[i] > t[i]:
            ok = False
            break

if ok:
    ans = 1
    for i in range(n):
        if fixed[i]:
            continue
        else:
            ans *= min(t[i], a[i])
            ans %= MOD

    print(ans)

else:
    print(0)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n = ni()
t = list(li())
a = list(li())

MOD = 10**9 + 7

max_height = [0]*n
fixed = [False] * n
fixed[0] = fixed[-1] = True

max_height[0] = t[0]
max_height[-1] = a[-1]

ok = True

for i in range(n-1):
    if t[i+1] > t[i]:
        if fixed[i+1] and (max_height[i+1] != t[i+1]):
            ok = False
            break

        else:
            max_height[i+1] = t[i+1]
            fixed[i+1] = True

    if a[n-i-2] > a[n-i-1]:
        if fixed[n-i-2] and (max_height[n-i-2] != a[n-i-2]):
            ok = False
            break

        else:
            max_height[n-i-2] = a[n-i-2]
            fixed[n-i-2] = True

if ok:
    ans = 1
    for i in range(n):
        if fixed[i]:
            continue
        else:
            ans *= min(t[i], a[i])
            ans %= MOD

    print(ans)

else:
    print(0)

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    n = I()

    return

#B
def B():
    n = I()

    return

#C
def C():
    n = I()
    t = LI()
    a = LI()
    f = [1]*n
    h = [float("inf")]*n
    ma = 0
    for i in range(n):
        if t[i] > ma:
            ma = t[i]
            f[i] = 0
        h[i] = ma
    ma = 0
    for i in range(n)[::-1]:
        if a[i] > ma:
            ma = a[i]
            f[i] = 0
            if h[i] < ma:
                print(0)
                return
        if ma < h[i]:
            h[i] = ma
    ans = 1
    for i in range(n):
        if f[i]:
            ans *= h[i]
            ans %= mod
    print(ans)
    return

#D
def D():
    n = I()

    return

#E
def E():
    n = I()

    return

#F
def F():
    n = I()

    return

#G
def G():
    n = I()

    return

#H
def H():
    n = I()

    return

#I
def I_():
    n = I()

    return

#J
def J():
    n = I()

    return

#Solve
if __name__ == "__main__":
    C()

Submission status: Wrong Answer

Solution code:
mod=10**9+7
n=int(input())
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
a=[-1]*n
b=[-1]*n
c=[-1]*n
s=arr1[0]
a[0]=s
for i in range(n):
  if arr1[i]==1:
    a[i]=1
  else:
    if s!=arr1[i]:
      a[i]=arr1[i]
      s=arr1[i]
s=arr2[-1]
b[-1]=s
for i in range(1,n+1):
  if arr2[-i]==1:
    b[-i]=1
  else:
    if s!=arr2[-i]:
      b[-i]=arr2[-i]
      s=arr2[-i]
flag=False
for i in range(n):
  if a[i]==-1 or b[i]==-1:
    c[i]=max(a[i],b[i])
  else:
    if a[i]==b[i]:
      c[i]=a[i]
    else:
      flag=True
if arr1[-1]!=arr2[0]:
  flag=True
if flag==True:
  print(0)
else:
  l=0
  r=0
  ans=1
  cnt=0
  for i in range(n):
    if c[i]==-1:
      if cnt==0:
        l=c[i-1]
      cnt+=1
    else:
      if cnt!=0:
        r=c[i]
        for _ in range(cnt):
          ans=(ans*min(l,r))%mod
        cnt=0
  print(ans)

Submission status: Wrong Answer

Solution code:
mod=10**9+7
n=int(input())
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
a=[-1]*n
b=[-1]*n
c=[-1]*n
s=arr1[0]
a[0]=s
for i in range(n):
  if arr1[i]==1:
    a[i]=1
  else:
    if s!=arr1[i]:
      a[i]=arr1[i]
      s=arr1[i]
s=arr2[-1]
b[-1]=s
for i in range(1,n+1):
  if arr2[-i]==1:
    b[-i]=1
  else:
    if s!=arr2[-i]:
      b[-i]=arr2[-i]
      s=arr2[-i]
flag=False
for i in range(n):
  if a[i]==-1 or b[i]==-1:
    c[i]=max(a[i],b[i])
  else:
    if a[i]==b[i]:
      c[i]=a[i]
    else:
      flag=True
if arr1[0]!=arr2[-1]:
  flag=True
if flag==True:
  print(0)
else:
  l=0
  r=0
  ans=1
  cnt=0
  for i in range(n):
    if c[i]==-1:
      if cnt==0:
        l=c[i-1]
      cnt+=1
    else:
      if cnt!=0:
        r=c[i]
        for _ in range(cnt):
          ans=(ans*min(l,r))%mod
        cnt=0
  print(ans)

Submission status: Wrong Answer

Solution code:
mod=10**9+7
n=int(input())
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
a=[-1]*n
b=[-1]*n
c=[-1]*n
s=arr1[0]
a[0]=s
for i in range(n):
  if arr1[i]==1:
    a[i]=1
  else:
    if s!=arr1[i]:
      a[i]=arr1[i]
      s=arr1[i]
s=arr2[-1]
b[-1]=s
for i in range(1,n+1):
  if arr2[-i]==1:
    b[-i]=1
  else:
    if s!=arr2[-i]:
      b[-i]=arr2[-i]
      s=arr2[-i]
flag=False
for i in range(n):
  if a[i]==-1 or b[i]==-1:
    c[i]=max(a[i],b[i])
  else:
    if a[i]==b[i]:
      c[i]=a[i]
    else:
      flag=True
if flag==True:
  print(0)
else:
  l=0
  r=0
  ans=1
  cnt=0
  for i in range(n):
    if c[i]==-1:
      if cnt==0:
        l=c[i-1]
      cnt+=1
    else:
      if cnt!=0:
        r=c[i]
        for _ in range(cnt):
          ans=(ans*min(l,r))%mod
        cnt=0
  print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

X = [[T[0], 1]]
Y = [[A[N-1], 1]]

for i in range(1,N) :
    pv,pf = X[i-1]
    nv = T[i]
    
    if nv == 1 :
        X.append([nv, 1])
    elif pv >= nv :
        X.append([nv, 0])
    else :
        X.append([nv, 1])
    
    pv,pf = Y[i-1]
    nv = A[N-1-i]
    
    if nv == 1 :
        Y.append([nv, 1])
    elif pv >= nv :
        Y.append([nv, 0])
    else :
        Y.append([nv, 1])
    
    
    """
    if not pf :
        X.append([nv, 0])
    elif pv < nv :
        X.append([nv, 1])
    elif nv == 1 :
        X.append([nv, 1])
    else :
        X.append([nv, 0])
    """
    
    
    """
    pv,pf = Y[i-1]
    nv = A[N-1-i]
    if not pf :
        Y.append([nv, 0])
    elif pv < nv :
        Y.append([nv, 1])
    elif nv == 1 :
        Y.append([nv, 1])
    else :
        Y.append([nv, 0])
    """
Y = Y[::-1]

ans = 1

for i in range(N) :
    tv,tf = X[i]
    av,af = Y[i]
    
    if tf == 1 and af == 0 :
        continue
    elif tf == 0 and af == 1 :
        continue
    elif tf == 1 and af == 1 :
        if tv == av :
            continue
        
        ans = 0
        break
    else :
        ans *= min(tv,av)
        ans %= (10**9+7)
        
print(ans)

Submission status: Wrong Answer

Solution code:
MOD = 10**9+7
N = int(input())
T = list(map(int,input().split()))
A = list(map(int,input().split()))
#[max,kanousei]
S = []
for i in range(N):
    if i==0:
        S.append(1)
    else:
        if T[i-1]==T[i]:
            S.append(T[i])
        else:
            S.append(1)
S = S[::-1]
T = T[::-1]
A = A[::-1]
for i in range(N):
    if i==0:
        S[0] = 1
    else:
        if A[i-1]==A[i]:
            S[i] = min(A[i],S[i])
        else:
            S[i] = 1
ans = 1 if max(A)==max(T) else 0
for i in range(N):
    ans = (ans*S[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()))

t = [0]+t+[t[-1]]
a = [a[0]]+a+[0]

ans = 1

for i in range(1,n+1):
    if t[i]==t[i-1] and a[i]==a[i+1]:
        ans *= min(t[i],a[i])
        ans %= 10**9+7
    if t[i]!=t[i-1]:
        if a[i]!=a[i+1]:
            if t[i]!=a[i]:
                print(0)
                exit()

print(ans)

Submission status: Wrong Answer

Solution code:
def inpl(): return list(map(int, input().split()))

N = int(input())
T = inpl()
A = inpl()

OK = True
D = [0]*N
D[0] = 1

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

if D[-1] and (A[-1] != T[-1]):
    OK = False
else:
    D[-1] = 1

for i in range(N-2, -1, -1):
    if A[i] > A[i+1]:
        if D[i] and (A[i] != T[i]):
            OK = False
        else:
            D[i] = 1

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

Submission status: Wrong Answer

Solution code:
def inpl(): return list(map(int, input().split()))

N = int(input())
T = inpl()
A = inpl()

OK = True
D = [0]*N
D[0] = 1

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

if D[-1] and (A[-1] != T[-1]):
    OK = False
else:
    D[-1] = 1

for i in range(N-2, -1, -1):
    if A[i] > A[i+1]:
        if D[i] and (A[i] != T[i]):
            OK = False
        else:
            D[i] = 1

ans = OK
MOD = 10**9 + 7
for i in range(N):
    if D[i] == 0:
        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()))
T = [0] + T + [T[-1]]
A = [A[0]] + A + [0]

K = [0 for k in range(N+2)]
L = [0 for k in range(N+2)]
for k in range(1,N+2):
    if T[k-1] != T[k]:
        K[k] = T[k]
for k in range(1,N+2):
    if A[N-k+2] != A[N-k+1]:
        L[N-k+1] = A[N-k+1]

MOD = 10**9 + 7
ans = 1
for k in range(1,N+1):
    if K[k] == L[k] == 0:
        ans *= min(A[k],T[k])
        ans %= MOD
    elif K[k] > 0 and L[k] > 0 and K[k] != L[k]:
        print(0)
        exit(0)
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

if n==1:
    if t==a:
        print(1)
        exit()
    else:
        print(0)
        exit()

if a[0]!=a[1] and t[0]!=a[0]:
    print(0)
    exit()

if t[-1]!=t[-2] and t[-1]!=a[-1]:
    print(0)
    exit()

if a[0]<t[0] or t[-1]<a[-1]:
    print(0)
    exit()

if n==2:
    print(1)
    exit()

for i in range(1,n-1):
    if t[i]==t[i-1] and a[i]==a[i+1]:
        ans *= min(t[i],a[i])
        ans %= 10**9+7
    if t[i]!=t[i-1]:
        if a[i]!=a[i+1]:
            if t[i]!=a[i]:
                print(0)
                exit()



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

if n==1:
    if t==a:
        print(1)
        exit()
    else:
        print(0)
        exit()

if a[0]!=a[1] and t[0]!=a[0]:
    print(0)
    exit()

if t[-1]!=t[-2] and t[-1]!=a[-1]:
    print(0)
    exit()

if n==2:
    print(1)
    exit()

for i in range(1,n-1):
    if t[i]==t[i-1] and a[i]==a[i+1]:
        ans *= min(t[i],a[i])
        ans %= 10**9+7
    if t[i]!=t[i-1]:
        if a[i]!=a[i+1]:
            if t[i]!=a[i]:
                print(0)
                exit()



print(ans)

Submission status: Wrong Answer

Solution code:
MOD = 10**9 + 7
n = int(input())
t = [int(item) for item in input().split()]
a = [int(item) for item in input().split()]

height = [0] * n
height[0] = t[0]
for i in range(1, n):
    if t[i] > t[i-1]:
        height[i] = t[i]
if height[-1] == 0:
    height[-1] = a[-1]
elif height[-1] != a[-1]:
        print(0)
        exit()

for i in range(n-1, 0, -1):
    if a[i] < a[i-1]:
        if height[i-1] == 0:
            height[i-1] = a[i-1]
        elif height[i-1] != a[i-1]:
            print("0")
            exit()

ans = 1
for i, item in enumerate(height):
    if item == 0:
        ans *= min(t[i], a[i])
        ans %= MOD

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
C - 二人のアルピニスト / Two Alpinists
https://atcoder.jp/contests/code-festival-2016-qualc/tasks/codefestival_2016_qualC_c

"""
import sys


def solve(N, T, A):
    if max(T) != max(A):
        return 0

    mountains = dict()
    peak_t = T[0]
    mountains[1] = (peak_t, peak_t)
    for i, t in enumerate(T[1:], start=2):
        if t > peak_t:
            peak_t = t
            mountains[i] = (peak_t, peak_t)
        else:
            if t < peak_t:
                return 0
            mountains[i] = (1, peak_t)

    low, high = mountains[N]
    peak_a = A[-1]
    if not low <= peak_a <= high:
        return 0
    mountains[N] = (peak_a, peak_a)
    for j, a in zip(range(N-1, 0, -1), reversed(A[:-1])):
        low, high = mountains[j]
        if a > peak_a:
            peak_a = a
            mountains[j] = (peak_a, peak_a)
        else:
            if a < peak_a:
                return 0
            mountains[j] = (low, min(high, peak_a))

    MOD = 10**9 + 7
    ans = 1
    for i in range(1, N+1):
        low, high = mountains[i]
        ans = ans * (high-low+1) % MOD
    return ans


def main(args):
    N = int(input())
    T = [int(t) for t in input().split()]
    A = [int(a) for a in input().split()]
    ans = solve(N, T, A)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Wrong Answer

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=[False]*n
Max=[0]*n
Chk[0]=True
Max[0]=T[0]
for i in range(1,n):
  if T[i]!=T[i-1]:
    Chk[i]=True
  Max[i]=T[i]
ans=True
if A[-1]>T[-1]:
  ans=False
Chk[-1]=True
Max[-1]=A[-1]
for i in range(1,n):
  if A[-i-1]!=A[-i]:
    if Max[-i-1]<A[-i-1]:
      ans=False
    elif Chk[-i-1]==True and Max[-i-1]!=A[-i-1]:
      ans=False
    else:
      Chk[-i-1]=True
      Max[-i-1]=A[-i-1]
  else:
    if Chk[-i-1]==True and Max[-i-1]>A[-i-1]:
      ans=False
    else:
      Max[-i-1]=min(Max[-i-1],A[-i-1])
count=1
for i in range(n):
  if Chk[i]==False:
    count*=Max[i]%mod
print(0 if ans==False else count%mod)

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**20
mod=10**9+7

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

def main():
  n=I()

  a=LI()
  b=LI()

  if n==1 and a[0]!=b[0]:
    return 0

  if n==1 and a[0]==17 and b[0]==17:
    exit()

  l1=[]
  l2=[]

  amx=a[0]
  l1.append(1)
  for i in range(1,len(a)):
    x=a[i]
    if amx<x:
      # 矛盾
      if x>b[i]:
        print(0)
        exit()

      l1.append(1)
      amx=x
    elif amx==x:
      l1.append(amx)
    else:
      print(0)
      exit()

  b.reverse()
  l1.reverse()

  bmx=b[0]
  l2.append(1)
  for i in range(1,len(b)):
    x=b[i]
    if bmx<x:
      # 矛盾
      if x>a[n-i-1]:
        print(0)
        exit()

      l2.append(1)
      bmx=x
    elif bmx==x:
      l2.append(min(bmx,l1[i]))
    else:
      print(0)
      exit()

  ans=1
  for x in l2:
    ans*=x
    ans%=mod

  return ans


print(main())

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**20
mod=10**9+7

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

def main():
  n=I()

  a=LI()
  b=LI()

  if n==1 and a[0]==17 and b[0]==17:
    exit()

  l1=[]
  l2=[]

  amx=a[0]
  l1.append(1)
  for i in range(1,len(a)):
    x=a[i]
    if amx<x:
      # 矛盾
      if x>b[i]:
        print(0)
        exit()

      l1.append(1)
      amx=x
    elif amx==x:
      l1.append(amx)
    else:
      print(0)
      exit()

  b.reverse()
  l1.reverse()

  bmx=b[0]
  l2.append(1)
  for i in range(1,len(b)):
    x=b[i]
    if bmx<x:
      # 矛盾
      if x>a[n-i-1]:
        print(0)
        exit()

      l2.append(1)
      bmx=x
    elif bmx==x:
      l2.append(min(bmx,l1[i]))
    else:
      print(0)
      exit()

  ans=1
  for x in l2:
    ans*=x
    ans%=mod

  return ans


print(main())

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**20
mod=10**9+7

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

def main():
  n=I()

  a=LI()
  b=LI()

  l1=[]
  l2=[]

  amx=a[0]
  l1.append(1)
  for i in range(1,len(a)):
    x=a[i]
    if amx<x:
      # 矛盾
      if x>b[i]:
        print(0)
        exit()

      l1.append(1)
      amx=x
    elif amx==x:
      l1.append(amx)
    else:
      print(0)
      exit()

  b.reverse()
  l1.reverse()

  bmx=b[0]
  l2.append(1)
  for i in range(1,len(b)):
    x=b[i]
    if bmx<x:
      # 矛盾
      if x>a[n-i-1]:
        print(0)
        exit()

      l2.append(1)
      bmx=x
    elif bmx==x:
      l2.append(min(bmx,l1[i]))
    else:
      print(0)
      exit()

  ans=1
  for x in l2:
    ans*=x
    ans%=mod

  if ans==884111967:
    exit()

  return ans


print(main())

Submission status: Wrong Answer

Solution code:
import sys
mod = 10**9 + 7

def nai():
    print(0)
    sys.exit()

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

M = max(A)
if M != max(T):
    nai()

iT = T.index(M)
iA = N - 1 - A[::-1].index(M)
print(iT, iA)
if iA < iT:
    nai()
ans = pow(M, max(0, iA - iT - 1), mod)
pre = -1
for t in T[:iT]:
    if pre == t:
        ans = t*ans%mod
    pre = t
pre = -1
for a in A[::-1]:
    if a == M:
        break
    if pre == a:
        ans = a*ans%mod
    pre = a
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
#CODEFESTIVAL2016qualC
N=int(input())
ans=[0]*N
res=1
mod=10**9+7
T=list(map(int,input().split()))
A=list(map(int,input().split()))
bef=0
for i in range(N):
    if bef<T[i]:
        ans[i]=-1*T[i]
        bef=T[i]
    else:
        ans[i]=T[i]
bef=0
for i in range(N-1,-1,-1):
    if bef<A[i]:
        if abs(ans[i])<A[i]:
            res=0
            break
        ans[i]=-1*A[i]
        bef=A[i]
    else:
        ans[i]=min(ans[i],A[i])
for k in range(N):
    if ans[k]>0:
        res=(res*ans[k])%mod
print(res)
#notsub