Problem:


Score : 400 points

Problem Statement

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

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

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

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

Constraints

1 ≤ N ≤ 10^5

1 ≤ T_i ≤ 10^9

1 ≤ A_i ≤ 10^9

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

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

Input

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

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

Output

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

Sample Input 1

5
1 3 3 3 3
3 3 2 2 2

Sample Output 1

4

The possible sequences of the mountains' heights are:

1, 3, 2, 2, 2 

1, 3, 2, 1, 2 

1, 3, 1, 2, 2 

1, 3, 1, 1, 2 

for a total of four sequences.

Sample Input 2

5
1 1 1 2 2
3 2 1 1 1

Sample Output 2

0

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

Sample Input 3

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

Sample Output 3

884111967

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

Sample Input 4

1
17
17

Sample Output 4

1

Some mountain ranges consist of only one mountain.



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


Sample output:
4

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

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

# 累積MAXが変化するところは確定と言える
Hs1=[0]*N
Hs1[0]=T[0]
fixed=[False]*N
fixed[0]=True
for i in range(1, N):
    if T[i-1]!=T[i]:
        Hs1[i]=T[i]
        fixed[i]=True
Hs2=[0]*N
Hs2[-1]=A[-1]
fixed[-1]=True
for i in range(N-2, -1, -1):
    if A[i+1]!=A[i]:
        Hs2[i]=A[i]
        fixed[i]=True

for i in range(N):
    # 両者の確定箇所について矛盾がないか確認
    if Hs1[i]!=0 and Hs2[i]!=0 and Hs1[i]!=Hs2[i]:
        print(0)
        exit()
    if Hs1[i]!=0:
        if Hs1[i]>A[i]:
            print(0)
            exit()
    if Hs2[i]!=0:
        if Hs2[i]>T[i]:
            print(0)
            exit()
ans=1
for i in range(N):
    if not fixed[i]:
        # 未確定の場所について、低い方の数だけ可能性がある
        ans=(ans*min(T[i], A[i]))%MOD
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
t = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 1
mod = 10**9 + 7
h = [0 for _ in range(n)]
last = 0
for i in range(n):
    if t[i] != last:
        h[i] = t[i]
        last = t[i]
last = 0
for i in range(n):
    j = n-i-1
    if a[j] != last:
        if (h[j] > 0 and h[j] != a[j]) or a[j] > t[j]:
            ans = 0
            break
        h[j] = a[j]
        last = a[j]
for i in range(n):
    if h[i] == 0:
        ans = ans * min(t[i], a[i]) % mod
print(ans)

Submission status: Accepted

Solution code:
mod = 1000000007
N = int(input())
T = list(map(int, input().split()))
A = list(map(int, input().split()))
T_max = 0
T_max_i = 0
for i in range(N):
    if T[i] > T_max:
        T_max = T[i]
        T_max_i = i
A_max = 0
A_max_i = 0
for i in range(N):
    if A[i] >= A_max:
        A_max = A[i]
        A_max_i = i
if T_max != A_max or T_max_i > A_max_i:
    print(0)
else:
    ans = 1
    h = 0
    for i in range(T_max_i + 1):
    	if T[i] > h:
    		h = T[i]
    	else:
    		ans = (ans * h) % mod
    for i in range(T_max_i + 1, A_max_i):
    	ans = (ans * h) % mod
    for i in range(A_max_i + 1, N):
    	if A[i] < h:
    		h = A[i]
    	else:
    		ans = (ans * h) % mod
    print(ans)

Submission status: Accepted

Solution code:
from itertools import groupby
from functools import reduce

MOD = 1000000007

def mod_pow(x, k):
    ret = 1
    while k > 0:
        if k & 1:
            ret *= x
            ret %= MOD
        k >>= 1
        x *= x
        x %= MOD
    return ret


def max_t():
    i, t_max = 0, 0
    for j, v in enumerate(t):
        if v < t_max:
            print(0)
            quit(0)
        if v > t_max:
            t_max = v
            i = j
    return i, t_max


def validate_a(i, t_max):
    for v in a[:i + 1]:
        if v != t_max:
            print(0)
            quit(0)
    for v in a[i + 1:]:
        if v > t_max:
            print(0)
            quit(0)


if __name__ == '__main__':
    n = int(input())
    t = [int(s) for s in input().split()]
    a = [int(s) for s in input().split()]
    i, t_max = max_t()
    validate_a(i, t_max)

    highests = [min(x, y) for x, y in zip(a, t)]
    print(reduce(lambda x, y: x * y % MOD, (mod_pow(v, ((length - 1) if v < t_max else max(length - 2, 0))) for v, length in ((v, len(list(l))) for v, l in groupby(highests)))))

Submission status: Accepted

Solution code:
n = int(input())
mount = [0]*n
a = list(map(int, input().split()))
b = list(map(int, input().split()))
mount[0] = a[0]
mount[-1] = b[-1]
pat = 1
mod = 10**9+7
for i in range(1,n):
  if a[i] > a[i-1]:
    if mount[i] != 0 and mount[i] != a[i]:
      print(0)
      exit()
    else:
      mount[i] = a[i]
  if b[n-i-1] > b[n-i]:
    if mount[n-i-1] != 0 and mount[n-i-1] != b[n-i-1]:
      print(0)
      exit()
    else:
      mount[n-i-1] = b[n-i-1]
for i in range(n):
  if mount[i] == 0:
    dx = 1
    while mount[i+dx] == 0:
      dx += 1
    this = min(mount[i-1],mount[i+dx])
    pat = pat*(this**dx)%mod
    for j in range(dx):
      mount[i+j] = this
a2 = [0]*n
b2 = [0]*n
a2[0] = mount[0]
b2[-1] = mount[-1]
for i in range(1,n):
  a2[i] = max(mount[i],a2[i-1])
  b2[n-i-1] = max(mount[n-i-1],b2[n-i])
if a != a2 or b != b2:
  print(0)
else:
  print(pat)

Submission status: Accepted

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

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

m_right = [[1,a[n-1]]]
for i in range(n-2,-1,-1):
    if a[i] > a[i+1]:
        m_right.append([1,a[i]])
    else:
        m_right.append([0,a[i]])
m_right = m_right[::-1]
#print(m_left)
#print(m_right)

ans = 1
for i in range(n):
    if m_right[i][0] == 1 and m_left[i][0] == 1:
        if m_right[i][1] != m_left[i][1]:
            ans = 0
    elif m_right[i][0] == 1:
        if m_right[i][1] > m_left[i][1]:
            ans = 0
    elif m_left[i][0] == 1:
        if m_left[i][1] > m_right[i][1]:
            ans = 0
    else:
        ans *= min(m_right[i][1], m_left[i][1])
        ans %= MOD

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
tl = list(map(int, input().split()))
al = list(map(int, input().split()))

mod = 10**9 + 7

res = 1
for i in range(1,n-1):
    if tl[i-1]<tl[i]:
        if tl[i]>al[i]:
            res = 0
        continue
    elif al[i]>al[i+1]:
        if al[i]>tl[i]:
            res = 0
        continue
    else:
        res *= min(al[i],tl[i])
        res %= mod
if tl[-1]!=al[0]:
    res = 0
print(res)

Submission status: Accepted

Solution code:
n=int(input())
t=tuple([0]+list(map(int,input().split()))+[0])
a=tuple([0]+list(map(int,input().split()))+[0])
c=1
m=10**9+7
for i in range(1,n+1):
    if t[i]>t[i-1]:
        if a[i]>a[i+1]and a[i]!=t[i]or a[i]==a[i+1]and a[i]<t[i]:
            print(0)
            exit()
    else:
        if a[i]>a[i+1]and a[i]>t[i]:
            print(0)
            exit()
        if a[i]==a[i+1]:
            c=min(t[i],a[i])*c%m
print(c)

Submission status: Accepted

Solution code:
n=int(input())
t=tuple([0]+list(map(int,input().split()))+[0])
a=tuple([0]+list(map(int,input().split()))+[0])
if n==1 and t[1]!=a[1]:
    print(0)
    exit()
c=1
m=10**9+7
for i in range(1,n):
    if t[i]>t[i-1]:
        if a[i]>a[i+1]and a[i]!=t[i]or a[i]==a[i+1]and a[i]<t[i]:
            print(0)
            exit()
    else:
        if a[i]>a[i+1]and a[i]>t[i]:
            print(0)
            exit()
        if a[i]==a[i+1]:
            c=min(t[i],a[i])*c%m
print(c)

Submission status: Accepted

Solution code:
n = int(input())
t = [int(m) for m in input().split()]
a = [int(m) for m in input().split()]
trcd = []
arcd = []
for i in range(n):
    if i == 0:
        trcd.append([t[i], 1])
    else:
        if t[i - 1] < t[i]:
            trcd.append([t[i], 1])
        elif t[i - 1] == t[i]:
            trcd.append([t[i], t[i]])

for i in reversed(range(n)):
    if i == n - 1:
        arcd.append([a[i], 1])
    else:
        if a[i] > a[i + 1]:
            arcd.append([a[i], 1])
        elif a[i] == a[i + 1]:
            arcd.append([a[i], a[i]])

ans = 1
mod = 10 ** 9 + 7
for i in range(n):
    if trcd[i][1] == arcd[-i-1][1] and trcd[i][1] == 1:#候補がひとつしかなくて
        if trcd[i][0] != arcd[-i-1][0]:#その候補の数字が違うとき
            print(0)
            exit()
    elif (trcd[i][0] > arcd[-i-1][0] and trcd[i][1] == 1) or (trcd[i][0] < arcd[-i-1][0] and arcd[-i-1][1] == 1):
        print(0)
        exit()
    else:
        ans *= min(trcd[i][1], arcd[-i-1][1])#候補が小さいほうをとる
        ans %= mod
print(ans)

Submission status: Accepted

Solution code:
MOD = 10**9 + 7

N = int(input())
Ts = list(map(int, input().split()))
As = list(map(int, input().split()))

minTs, maxTs = [1]*N, [10**9]*N
minAs, maxAs = [1]*N, [10**9]*N

minTs[0] = maxTs[0] = Ts[0]
for i in range(1, N):
    if Ts[i-1] < Ts[i]:
        minTs[i] = maxTs[i] = Ts[i]
    else:
        maxTs[i] = Ts[i]

minAs[-1] = maxAs[-1] = As[-1]
for i in reversed(range(N-1)):
    if As[i] > As[i+1]:
        minAs[i] = maxAs[i] = As[i]
    else:
        maxAs[i] = As[i]

ans = 1
for minT, maxT, minA, maxA in zip(minTs, maxTs, minAs, maxAs):
    minH = max(minT, minA)
    maxH = min(maxT, maxA)
    ans *= max(0, maxH - minH + 1)
    ans %= MOD
print(ans)

Submission status: Accepted

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

not_fixed = [True] * N

# 矛盾のチェック
MAX = 0
for i in range(N):
    if T[i] > MAX:
        MAX = T[i]
        not_fixed[i] = False
        if T[i] > A[i]:
            print(0)
            exit()

MAX = 0
for i in reversed(range(N)):
    if A[i] > MAX:
        MAX = A[i]
        not_fixed[i] = False
        if A[i] > T[i]:
            print(0)
            exit()


MIN = [min(t, a) for t, a in zip(T, A)]
ans = 1
mod = 10 ** 9 + 7

for i in range(N):
    if not_fixed[i]:
        ans *= MIN[i]
        ans %= mod

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


def main():
    from bisect import bisect_right
    from bisect import bisect_left

    n = int(input())
    t = list(map(int, input().split()))
    a = list(map(int, input().split()))[::-1]
    t_candidates = [1 for _ in range(n)]
    a_candidates = [1 for _ in range(n)]
    ans = 1

    if max(t) != max(a):
        print(0)
        exit()
    else:
        if bisect_left(t, max(t)) > (n - bisect_left(a, max(a)) - 1):
            print(0)
            exit()

    for i in range(1, n):
        if t[i] == t[i - 1]:
            t_candidates[i] = t[i]

        if a[i] == a[i - 1]:
            a_candidates[i] = a[i]

    for t_candidate, a_candidate in zip(t_candidates, a_candidates[::-1]):
        ans *= min(t_candidate, a_candidate)
        ans %= 10 ** 9 + 7

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
t_list = list(map(int, input().split()))
a_list = list(map(int, input().split()))

t_max = -1
t_i = 0
a_max = -1
a_i = 0
ans = 1
mod = 10 ** 9 + 7

for i in range(N):
    t = t_list[i]
    a = a_list[i]
    if t > t_max:
        t_max = t
        t_i = i
    if a >= a_max:
        a_max = a
        a_i = i
if a_max == t_max and t_i <= a_i:
    prev_a = a_list[-1]
    prev_t = t_list[0]
    for i in range(1, t_i):
        t = t_list[i]
        if t == prev_t:
            ans *= prev_t
            ans %= mod
        prev_t = t
    for i in range(N-2, t_i, -1):
        a = a_list[i]
        if a == prev_a:
            ans *= prev_a
            ans %= mod
        prev_a = a
    print(ans % mod)
else:
    print(0)

Submission status: Accepted

Solution code:
N = int(input())
T = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]
mod = 10**9 + 7
upT = set()
upA = set()
for i in range(N):
    if i == 0 or T[i - 1] < T[i]:
        upT.add(i)
    if i == N - 1 or A[i] > A[i + 1]:
        upA.add(i)
ans = 1
for i in range(N):
    if i in upT:
        if T[i] > A[i]:
            ans = 0
            break
    elif i in upA:
        if T[i] < A[i]:
            ans = 0
            break
    else:
        ans *= min(T[i], A[i])
        ans %= mod
print(ans)

Submission status: Accepted

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

tmax = T[-1]
amax = A[0]
tf = T.index(tmax)
af = N - (list(reversed(A))).index(amax) - 1

if tf > af or tmax != amax:
	print(0)

else:
	ans = 1
	for i in range(1, tf + 1):
		if T[i] == T[i - 1]:
			ans = (ans * T[i - 1]) % 1000000007

	for i in range(af - tf - 1):
		ans = (ans * tmax) % 1000000007
	for i in range(af + 1, N):
		if A[i] == A[i - 1]:
			ans = (ans * A[i - 1]) % 1000000007

	print(ans)

Submission status: Accepted

Solution code:
def inpl(): return [int(i) for i in input().split()]
mod = 10**9+7
N = int(input())
T = inpl()
A = inpl()
summit =  T[-1]
S = [i == j == summit for i, j in zip(A, T)]
ans, pre = 1, 0
if A[0] != summit:
    ans = 0
if not any(S):
    ans = 0

k = sum([min(i, j) == summit for i, j in zip(A, T)]) 
for i in T:
    if i == summit:
        break
    if i == pre:
        ans = ans*pre %mod
    else:
        pre = i
pre = 0
for i in reversed(A):
    if i == summit:
        break
    if i == pre:
        ans = ans*pre %mod
    else:
        pre = i
print(ans*summit**max(k-2, 0)%mod)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
T = [int(t) for t in input().split()]
A = [int(a) for a in input().split()]
mod = 7 + 10**9

TS, AS = [None for i in range(N)], [None for j in range(N)] #山の高さが一意に決まるならTrue
TS[0], AS[-1] = True, True
for i in range(1, N):
    if T[i] > T[i-1]: TS[i] = True
    else: TS[i] = False
    if A[N-1-i] > A[N-i]: AS[N-1-i] = True
    else: AS[N-1-i] = False

Ans = 1
for i in range(N): 
    if TS[i] and AS[i]:
        if T[i] != A[i]: 
            Ans = 0
            break
    elif TS[i]:
        if A[i] < T[i]:
            Ans = 0
            break
    elif AS[i]:
        if T[i] < A[i]:
            Ans = 0
            break
    else:
        Ans *= min(T[i], A[i])
        Ans %= mod

print(Ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
s=list(map(int, input().split()))
t=list(map(int, input().split()))
ans=1
if s[s.index(max(s))]!=t[s.index(max(s))]:ans=0
t=t[::-1]
a=[1]+[0]*(n-2)+[1]
for i in range(1,n-1):
    if s[i]<a[i-1]:
        ans=0
        break
    elif s[i]>s[i-1]:a[i]=1
    else:a[i]=s[i-1]
for i in range(1,n-1):
    if t[i]<t[i-1]:
        ans=0
        break
    elif t[i]>t[i-1]:a[n-i-1]=1
    else:a[n-i-1]=min(t[i-1],a[-1*(i+1)])
for i in a:
    ans=(ans*i)%(10**9+7)
print(ans)

Submission status: Accepted

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

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

ans = 1
for i in range(1, n - 1):
    if t[i] != t[i - 1]:
        if a[i] < t[i]:
            print(0)
            exit()
    elif a[i] != a[i + 1]:
        if t[i] < a[i]:
            print(0)
            exit()
    else:
        ans *= min(a[i], t[i])
        ans %= MOD

print(ans)

Submission status: Accepted

Solution code:
read=lambda :list(map(int,input().split()))
n=int(input())
t,a=read(),read()
mod=10**9+7
t=[(i!=j,i) for i,j in zip(t,[0]+t)]
a=[(i!=j,i) for i,j in zip(a,(a+[0])[1:])]

ans=1
for p,q in zip(t,a):
  if(p[0]):
    if(q[0]):
      if(p[1]!=q[1]):
        print(0)
        exit()  
    elif(q[1]<p[1]):
      print(0)
      exit()
  elif(q[0]):
    if(p[1]<q[1]):
      print(0)
      exit()
  else:
	  ans=ans*min(p[1],q[1])%mod
print(ans)

Submission status: Accepted

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

    a_comb = 1
    if A[i] == A[i + 1]:
        a_comb *= A[i]
    ans = (ans * min(t_comb, a_comb)) % MOD
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())

N = int(input())
tt = inpl()
aa = inpl()
hh = [[0,0] for i in range(N)]

bt = 0
for i in range(N):
	t = tt[i]
	if t != bt:
		hh[i] = [t,t]
	else:
		hh[i] = [1,t]
	bt = t

ba = 0
for i in reversed(range(N)):
	a = aa[i]
	MIN,MAX = hh[i]
	if a != ba:
		if MIN <= a <= MAX:
			hh[i] = [a,a]
		else:
			print(0)
			sys.exit()
	else:
		hh[i] = [max(1,MIN),min(a,MAX)]

	ba = a

ans = 1
for azu,nyan in hh:
	ans *= (nyan-azu+1)
	ans %= mod

print(ans)

Submission status: Accepted

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

if t[0] > a[0] or t[-1] < a[-1] or t[-1] != a[0]:
	ans *= 0

for i in range(1, n - 1):
	if t[i - 1] != t[i]:
		if t[i] > a[i]:
			ans *= 0
			break
		else:
			continue
	if a[i + 1] != a[i]:
		if t[i] < a[i]:
			ans *= 0
			break
		else:
			continue
	ans = (ans * min(t[i], a[i])) % MOD

print(ans)

Submission status: Accepted

Solution code:
# 入力
import sys
stdin = sys.stdin

def li(): return [int(x) for x in stdin.readline().split()]
def li_(): return [int(x)-1 for x in stdin.readline().split()]
def lf(): return [float(x) for x in stdin.readline().split()]
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(ns())
def nf(): return float(ns())

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

MOD = 10**9 + 7

# T,A の更新と矛盾を確認
t_modified = [0]*n
a_modified = [0]*n
t_modified[0] = 1
a_modified[-1] = 1
consistent = True

if max(a) != max(t):
    consistent = False

for i in range(n-1):
    if t[i+1] != t[i]:
        t_modified[i+1] = 1
        
        if t[i+1] > a[i+1]:
            consistent = False
            
    if a[i] != a[i+1]:
        a_modified[i] = 1
        
        if a[i] > t[i]:
            consistent = False

# 更新があった点は1通り、無かった点はmin(ti,ai)通りの高さが考えられる      
ans = 1
for i in range(n):
    if t_modified[i] == 0 and a_modified[i] == 0:
        ans = (ans * min(t[i], a[i])) % MOD

if consistent:
    print(ans)
else:
    print(0)

Submission status: Accepted

Solution code:
n = int(input())

a = map(int,raw_input().split())
b = map(int,raw_input().split())

combs = [0 for i in range(n)]
unique = [0 for i in range(n)]
t = 0
mod = 10**9 + 7
for i in range(n):
	if a[i] > t or a[i] == 1:
		combs[i] = 1
		t = a[i]
		unique[i] = 1

	else :
		combs[i] = t
t = 0
for i in range(n-1,-1,-1):
	if unique[i] == 1 and a[i] > b[i] :
		print 0
		quit()

	if b[i] > t:
		combs[i] = 1
		t = b[i]

	else :
		combs[i] = min(combs[i], t)

ans = 1
for i in combs:
	ans = (ans * i)%mod

print ans

Submission status: Accepted

Solution code:
import sys

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [long(input_raw[0])]
    input_rawT = read_func().strip().split()
    input_rawA = read_func().strip().split()
    T = []
    A = []
    for i in range(N):
        T.append(long(input_rawT[i]))
        A.append(long(input_rawA[i]))
    count = 1
    h_T = [(0, 0) for i in range(N)]
    h_A = [(0, 0) for i in range(N)]
    h_T[0] = (T[0], T[0])
    h_A[N - 1] = (A[N - 1], A[N - 1])

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

        if A[j] == A[j + 1]:
            h_A[j] = (1, A[j])
        else:
            h_A[j] = (A[j], A[j])

    count = 1L
    for i in range(0, N):
        h1 = max(h_A[i][0], h_T[i][0])
        h2 = min(h_A[i][1], h_T[i][1])
        if h1 > h2:
            print 0
            exit()
        count = ((count * long(h2 - h1 + 1)) % long(1e9 + 7))

    print count




if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split()))
r = list(map(int,input().split()))
l1 = max(l)
r1 = max(r)
left = 1
if l[0] == l1:left = 0

right = n-2
if r[-1] == l1:right = n-1

res = 1
if l1 != r1:print(0)
else:
    while l[left] != l1:
        if l[left] == l[left-1]:
            res = (res*l[left])%1000000007
        left += 1
    while r[right] != r1:
        if r[right] == r[right+1]:
            res = (res*r[right])%1000000007
        right -=1

    if  left > right:print(0)
    else:
        if  left < right:
            for i in range(right-left-1):
                res = (res*l1)%1000000007

        print(res%1000000007)

Submission status: Accepted

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

    is_certain, h = [0 for i in range(n)], t[:]
    is_certain[0] = 1
    for i in range(1, n):
        if t[i] > t[i-1]:
            is_certain[i] = 1

    if is_certain[-1] and h[-1] != a[-1]:
        print(0)
        exit()
    else:
        h[-1] = a[-1]
        is_certain[-1] = 1

    for i in range(n-2, -1, -1):
        if a[i] > a[i+1]:
            if is_certain[i] and h[i] != a[i]:
                print(0)
                exit()
            else:
                h[i] = a[i]
                is_certain[i] = 1
        elif is_certain[i] and a[i] < h[i]:
            print(0)
            exit()
        else:
            h[i] = min(h[i], a[i])

    ans = 1
    for i in range(n):
        if not is_certain[i]:
            ans = ans * h[i] % 1000000007

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
T = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
p = 10 ** 9 + 7

B = [10**9] * n

h = 0

for i, t in enumerate(T):
    if h < t:
        B[i] = 1
        h = t
    else:
        B[i] = h

h = 0

for i, a in zip(range(n-1, -1, -1), A[::-1]):
    if h < a:
        if a > T[i] or (B[i] == 1 and a != T[i]):
            print(0)
            exit(0)
        B[i] = 1
        h = a
    elif B[i] == 1 and T[i] > a:
        print(0)
        exit(0)
    elif B[i] > h:
        B[i] = h

ans = 1

for b in B:
    ans *= b
    ans %= p

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
T = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
p = 10 ** 9 + 7

B = [10**9] * n

if max(T) != max(A):
    print(0)
    exit()

h = 0

for i, t in enumerate(T):
    if h < t:
        B[i] = 1
        h = t
    else:
        B[i] = h

h = 0

for i, a in zip(range(n-1, -1, -1), A[::-1]):
    if h < a:
        B[i] = 1
        h = a
    elif B[i] == 1 and T[i] > A[i]:
        print(0)
        exit(0)
    elif B[i] > h:
        B[i] = h

ans = 1

for b in B:
    ans *= b
    ans %= p

print(ans)

Submission status: Accepted

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

T = [T[0]] + T + [T[-1]]
A = [A[0]] + A + [A[-1]]

ans = 1
mod = 10**9 + 7
for i in range(1, N+1):
    if T[i-1] < T[i] and A[i + 1] < A[i]:
        if T[i] != A[i]:
            ans *= 0
        continue

    if T[i-1] < T[i]:
        if A[i] < T[i]:
            ans *= 0
        continue

    if A[i] > A[i+1]:
        if T[i] < A[i]:
            ans *= 0
        continue

    if i == 1 or i == N:
        if T[N] != A[1]:
            ans *= 0
        else:
            ans *= 1
        continue

    ans = (ans * min(T[i], A[i])) % mod

print(ans % mod)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
try: from typing import Any, Union, List, Tuple, Dict
except ImportError: pass
def debug(*args): print(*args, file=sys.stderr)
def exit(): sys.exit(0)


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


T_ = []
A_ = []

T_.append(("=", T[0]))
for i in range(1, N):
    if T[i-1] == T[i]:
        T_.append(("<=", T[i]))
    else:
        T_.append(("=", T[i]))

for i in range(N-1):
    if A[i] == A[i+1]:
        A_.append(("<=", A[i]))
    else:
        A_.append(("=", A[i]))
A_.append(("=", A[N-1]))


count = 1
for i in range(N):
    t0, t1 = T_[i]
    a0, a1 = A_[i]
    if t0 == "=" and a0 == "=":
        if t1 == a1:
            continue
        else:
            print(0)
            exit()
    elif t0 == "=":
        if t1 > a1:
            print(0)
            exit()
        else:
            continue
    elif a0 == "=":
        if a1 > t1:
            print(0)
            exit()
        else:
            continue
    else:
        count = (count * min(t1, a1)) % (10**9+7)

print(count)

Submission status: Accepted

Solution code:
n = int(input())
t = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
x,flag,ans,mod = [[t[0],t[0]]]+[[1,1] for _ in range(n-1)],True,1,10**9+7
if max(t)!=max(a): flag = False
for i in range(1,n):
	if t[i-1]<t[i]: x[i] = [t[i],t[i]]
	else: x[i][0] = t[i]
if x[-1][0]<a[-1]: flag = False
else:
	x[-1] = [a[-1],a[-1]]
	for i in range(n-2,0,-1):
		if a[i]>a[i+1]:
			if x[i][0]>=a[i]: x[i]=[a[i],a[i]]
			else: flag = False
		else:
			if x[i][0]>=a[i]: x[i][0]=a[i]
			else: pass
if flag:
	for i,j in x: ans = (ans*(i-j+1))%mod
else: ans = 0
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
yama=[0 for i in range(int(input()))]
t=list(map(int,input().split()))
a=list(map(int,input().split()))

check=0
for i in range(len(yama)):
    if check!=t[i]:
        check=t[i]
        yama[i]=check
    else:
        yama[i]=-check
check=0
for i in range(len(yama)):
    if yama[len(yama)-i-1]>0 and yama[len(yama)-i-1]>a[len(yama)-i-1]:
        print(0)
        exit()
    if check!=a[len(yama)-i-1]:
        check=a[len(yama)-i-1]
        if yama[len(yama)-i-1]>0 and yama[len(yama)-i-1]>check:
            print(0)
            exit()
        yama[len(yama)-i-1]=check
    else:
        if yama[len(yama)-i-1]<0:
            yama[len(yama)-i-1]=max(-check,yama[len(yama)-i-1])

x=1
for i in yama:
    if i<0:
        x=(x*-i)%1000000007

print(x)