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:
import numpy as np
from itertools import groupby

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

t = (T[1:] != T[:-1]) * T[1:]
a = (A[1:] != A[:-1]) * A[:-1]
seq = np.zeros(N, np.int64)
if T[0] <= A[0] and A[-1] <= T[-1] == A[0]:
    seq[0] = T[0]
    seq[-1] = A[-1]
else:
    print(0)
    exit()

tmp = np.where((t[:-1] != 0) & (a[1:] != 0))[0]
if np.all(t[tmp] == a[tmp + 1]):
    seq[1:-1] = np.maximum(t[:-1], a[1:])
    group = [(i, len(list(j))) for i, j in groupby(seq.tolist())]
    answer = 1
    for i in range(len(group)):
        if group[i][0] == 0:
            answer *= pow(min(group[i - 1][0], group[i + 1][0]), group[i][1], mod)
            answer %= mod
    print(answer)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
n = int(input())
t = list(map(int, input().split()))
a = list(map(int, input().split()))
mt_t=[0]*n
mt_a=[0]*n
last=0
for i in range(len(t)):
    if last < t[i]:
        mt_t[i] = -t[i]
        last=t[i]
    else:
        mt_t[i] = last
last=0
for i in reversed(range(len(t))):
    if last < a[i]:
        mt_a[i] = -a[i]
        last=a[i]
    else:
        mt_a[i] = last
mts = [0]*n
for i in range(len(t)):
    if mt_t[i] < 0 and mt_a[i] < 0:
        if mt_t[i] != mt_a[i]:
            print(0)
            exit()
    elif mt_t[i] < 0 or mt_a[i] < 0:
        kaku =abs(min(mt_t[i],mt_a[i]))
        kouho=max(mt_t[i],mt_a[i])
        if kouho < kaku:
            print(i, kouho, kaku)
            print(0)
            exit()
    mts[i] = min(mt_t[i],mt_a[i])

ans=1
for num in mts:
    if num > 0:
        ans*=num
        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()))

mt_t=[0]*n
mt_a=[0]*n

last=0
for i in range(len(t)):
    if last < t[i]:
        mt_t[i] = -t[i]
        last=t[i]
    else:
        mt_t[i] = last

last=0
for i in reversed(range(len(t))):
    if last < a[i]:
        mt_a[i] = -a[i]
        last=a[i]
    else:
        mt_a[i] = last

mts = [0]*n
for i in range(len(t)):
    if mt_t[i] < 0 and mt_a[i] < 0:
        if mt_t[i] != mt_a[i]:
            print(0)
            exit()
    if mt_t[i] < 0 or mt_a[i] < 0:
        kaku =abs(min(mt_t[i],mt_a[i]))
        kouho=max(mt_t[i],mt_a[i])
        if kaku < kouho:
            print(0)
            exit()
    mts[i] = min(mt_t[i],mt_a[i])

ans=1
for num in mts:
    if num > 0:
        ans*=num
        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()))

mt_t=[0]*n
mt_a=[0]*n

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

last=0
for i in range(len(t)):
    if last < t[i]:
        mt_t[i] = -t[i]
        last=t[i]
    else:
        mt_t[i] = last

last=0
for i in reversed(range(len(t))):
    if last < a[i]:
        mt_a[i] = -a[i]
        last=a[i]
    else:
        mt_a[i] = last

mts = [0]*n
for i in range(len(t)):
    if mt_t[i] < 0 and mt_a[i] < 0:
        if mt_t[i] != mt_a[i]:
            print(0)
            exit()
    mts[i] = min(mt_t[i],mt_a[i])

ans=1
for num in mts:
    if num > 0:
        ans*=num
        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()))

mt_t=[0]*n
mt_a=[0]*n

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

last=0
for i in range(len(t)):
    if last < t[i]:
        mt_t[i] = -t[i]
        last=t[i]
    else:
        mt_t[i] = last

last=0
for i in reversed(range(len(t))):
    if last < a[i]:
        mt_a[i] = -a[i]
        last=a[i]
    else:
        mt_a[i] = last

mts = [0]*n
for i in range(len(t)):
    if mt_t[i] < 0 and mt_a[i] < 0:
        if mt_t[i] != mt_a[i]:
            print(0)
            exit()
    mts[i] = min(mt_t[i],mt_a[i])

ans=1
for num in mts:
    if num > 0:
        ans*=num
        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()))

mt_t=[0]*n
mt_a=[0]*n

last=0
for i in range(len(t)):
    if last < t[i]:
        mt_t[i] = -t[i]
        last=t[i]
    else:
        mt_t[i] = last

last=0
for i in reversed(range(len(t))):
    if last < a[i]:
        mt_a[i] = -a[i]
        last=a[i]
    else:
        mt_a[i] = last

mts = [0]*n
for i in range(len(t)):
    if mt_t[i] < 0 and mt_a[i] < 0:
        if mt_t[i] != mt_a[i]:
            print(0)
            exit()
    mts[i] = min(mt_t[i],mt_a[i])

ans=1
for num in mts:
    if num > 0:
        ans*=num
        ans%=(10**9+7)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
T = [int(t) for t in input().split()]
A = [int(a) for a in input().split()]

mod = 10**9+7
M = [0]*N
M[0] = T[0]
M[-1] = A[-1]
flag = True
for i in range(N-1):
    if T[i+1] > T[i]:
        if M[i+1] > 0 and M[i+1] != T[i+1]:
            flag = False
        M[i+1] = T[i+1]
    if A[i] > A[i+1]:
        if M[i] > 0 and M[i] != A[i]:
            flag = False
        M[i] = A[i]

if flag:
    ans = 1
    for i in range(N):
        if 0 < M[i] <= min(A[i], T[i]):
            continue
        elif min(A[i], T[i]) < M[i]:
            ans = 0
            break
        ans *= min(A[i], T[i])
        ans %= mod
else:
    ans = 0

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
T = [int(t) for t in input().split()]
A = [int(a) for a in input().split()]

mod = 10**9+7
M = [0]*N
M[0] = T[0]
M[-1] = A[-1]
flag = True
for i in range(N-1):
    if T[i+1] > T[i]:
        if M[i+1] > 0 and M[i+1] != T[i+1]:
            flag = False
        M[i+1] = T[i+1]
    if A[i] > A[i+1]:
        if M[i] > 0 and M[i] != A[i]:
            flag = False
        M[i] = A[i]

if flag:
    ans = 1
    for i in range(N):
        if M[i] > 0:
            continue
        ans *= min(A[i], T[i])
        ans %= mod
else:
    ans = 0

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
f=[-1]*n
ma=[-1]*n
f[0]=a[0]
for i in range(n-1):
    if a[i]!=a[i+1]:
        f[i+1]=a[i+1]
    else:
        ma[i+1]=a[i+1]
if f[-1]>=0 and f[-1]!=b[-1]:
    print(0)
    exit()
f[-1]=b[-1]
for i in range(n-1):
    if b[n-i-1]!=b[n-i-2]:
        if f[n-i-2]>=0 and f[n-i-2]!=b[n-i-2]:
            print(0)
            exit()
        f[n-i-2]=b[n-i-2]
    else:
        if ma[n-i-2]==-1:
            ma[n-i-2]=b[n-i-2]
        else:
            ma[n-i-2]=min(ma[n-i-2],b[n-i-2])
ans=1
mod=10**9+7
for i in range(n):
    if f[i]==-1:
        ans*=ma[i]
        ans%=mod
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
f=[-1]*n
ma=[-1]*n
f[0]=a[0]
for i in range(n-1):
    if a[i]!=a[i+1]:
        f[i+1]=a[i+1]
    else:
        ma[i+1]=a[i+1]
if f[-1]>=0 and f[-1]!=b[-1]:
    print(0)
    exit()
f[-1]=b[-1]
for i in range(n-1):
    if b[n-i-1]!=b[n-i-2]:
        if f[n-i-2]>=0 and f[n-i-2]!=b[n-i-2]:
            print(0)
            exit()
        f[n-i-2]=b[n-i-2]
    else:
        ma[n-i-2]=min(ma[n-i-2],b[n-i-2])
ans=1
mod=10**9+7
for i in range(n):
    if f[i]==-1:
        ans*=ma[i]
        ans%=mod
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
f=[-1]*n
ma=[-1]*n
f[0]=a[0]
for i in range(n-1):
    if a[i]!=a[i+1]:
        f[i+1]=a[i+1]
    else:
        ma[i+1]=a[i+1]
if f[-1]>=0 and f[-1]!=b[-1]:
    print(0)
    exit()
f[-1]=b[-1]
for i in range(n-1):
    if b[n-i-1]!=b[n-i-2]:
        if f[n-i-2]>=0 and f[n-i-2]!=b[n-i-2]:
            print(0)
            exit()
        f[n-i-2]=b[n-i-2]
    else:
        ma[n-i-2]=min(ma[n-i-2],b[n-i-2])
ans=1
mod=10**9+7
for i in ma:
    if i>=0:
        ans*=i
        ans%=mod
print(ans)

Submission status: Wrong Answer

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

mod = int(1e9+7)

dp = [0] * (N)
dp[0] = 1
# dp[i] : i番目までの通り数

Amax = max(A)
Aleft, Tright = -1, -1
Tmax = max(T)
for i in range(N):
  if Amax == A[i]:
    Aleft = i
    break
for i in range(N-1,-1,-1):
  if Tmax == T[i]:
    Tright = i
    break
if Aleft > Tright:
  print(0)
  exit(0)
  
for i in range(1,N-1):
  if A[i-1] < A[i] or T[i] > T[i+1]:
    dp[i] = dp[i-1]
    if A[i] != T[i]:
      print(0)
      exit(0)
  else:
    dp[i] = dp[i-1] * min(A[i], T[i]) % mod
    
print(dp[N-2])

Submission status: Wrong Answer

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

mod = int(1e9+7)

dp = [0] * (N)
dp[0] = 1
# dp[i] : i番目までの通り数

Amax = max(A)
Aleft, Tright = -1, -1
Tmax = max(T)
for i in range(N):
  if Amax == A[i]:
    Aleft = i
    break
for i in range(N-1,-1,-1):
  if Tmax == T[i]:
    Tright = i
    break
if Aleft > Tright:
  print(0)
  exit(0)
  
for i in range(1,N-1):
  if A[i-1] < A[i] or T[i] > T[i+1]:
    dp[i] = dp[i-1]
  else:
    dp[i] = dp[i-1] * min(A[i], T[i]) % mod
    
print(dp[N-2])

Submission status: Wrong Answer

Solution code:
n = int(input())
t = list(map(int,input().split()))
a = list(map(int,input().split()))
ans = [0 for i in range(n)]
ans[0] = t[0]
ans[-1] = a[-1]
if n==1 and a[0]!=t[0]:
    print(0)
    exit()
for i in range(n-1):
    if t[i]<t[i+1]:
        ans[i+1] = t[i+1]
for i in range(1,n)[::-1]:
    if a[i]<a[i-1]:
        if ans[i-1]!=0 and ans[i-1]!=a[i-1]:
            print(0)
            exit()
        ans[i-1] = a[i-1]
res = 1
mod = 10**9+7
for i in range(n):
    if  ans[i]!=0 and min(t[i],a[i])>ans[i]:
        res = 0
    fact = min(t[i],a[i])-ans[i]
    if ans[i]!=0:
        continue
    res*=fact
    res%=mod
if t[-1] != a[0]:
    res = 0
print(res)

Submission status: Wrong Answer

Solution code:
n = int(input())
t = list(map(int,input().split()))
a = list(map(int,input().split()))
ans = [0 for i in range(n)]
ans[0] = t[0]
ans[-1] = a[-1]
if n==1 and a[0]!=t[0]:
    print(0)
    exit()
for i in range(n-1):
    if t[i]<t[i+1]:
        ans[i+1] = t[i+1]
for i in range(1,n)[::-1]:
    if a[i]<a[i-1]:
        if ans[i-1]!=0 and ans[i-1]!=a[i-1]:
            print(0)
            exit()
        ans[i-1] = a[i-1]
res = 1
mod = 10**9+7
for i in range(n):
    if  ans[i]!=0 and min(t[i],a[i])>ans[i]:
        res = 0
    fact = min(t[i],a[i])-ans[i]
    if ans[i]!=0:
        continue
    res*=fact
    res%=mod
print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

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

    else:
        if A[i] != A[i+1]:
            if M[i][0] != -1 and M[i][0] != A[i]:
                print(0)
                exit(0)
            else:
                if A[i] <= M[i][1]:
                    M[i][0] = A[i]
        else:
            M[i][1] = min(M[i][1], A[i])
ans = 1
mod = (10 ** 9) + 7
for x, y in M:
    if x == -1:
        ans *= y
        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()))

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

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

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

    else:
        if A[i] != A[i+1]:
            if M[i][0] != -1 and M[i][0] != A[i]:
                print(0)
                exit(0)
            else:
                M[i][0] = A[i]
        else:
            M[i][1] = min(M[i][1], A[i])
ans = 1
mod = (10 ** 9) + 7
for x, y in M:
    if x == -1:
        ans *= y
        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()))

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

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

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

    else:
        if A[i] != A[i+1]:
            if M[i][0] != -1 and M[i][0] != A[i]:
                print(0)
                exit(0)
            else:
                M[i][0] = A[i]
        else:
            M[i][1] = min(M[i][1], A[i])
print(M)
ans = 1
mod = (10 ** 9) + 7
for x, y in M:
    if x == -1:
        ans *= y
        ans %= mod

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

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

N = ir()
T = np.array(lr()).astype('u8')
A = np.array(lr()).astype('u8')
MOD = 10 ** 9 + 7

T_low = T.copy()
T_low[1:][T[1:] <= T[:-1]] = 1
A_low = A.copy()
A_low[:-1][A[:-1] <= A[1:]] = 1

lower = np.maximum(T_low, A_low)
upper = np.minimum(T, A)
domain = np.maximum(0, upper - lower + 1) #変域が0の時は記録が間違い
answer = np.cumprod(domain)[-1]
print(answer)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

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

N = ir()
T = np.array(lr()).astype('f16')
A = np.array(lr()).astype('f16')
MOD = 10 ** 9 + 7

T_low = T.copy()
T_low[1:][T[1:] <= T[:-1]] = 1
A_low = A.copy()
A_low[:-1][A[:-1] <= A[1:]] = 1

lower = np.maximum(T_low, A_low)
upper = np.minimum(T, A)
domain = np.maximum(0, upper - lower + 1) #変域が0の時は記録が間違い
answer = np.cumprod(domain)[-1]
print(answer)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

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

N = ir()
T = np.array(lr()).astype('int64')
A = np.array(lr()).astype('int64')
MOD = 10 ** 9 + 7

T_low = T.copy()
T_low[1:][T[1:] <= T[:-1]] = 1
A_low = A.copy()
A_low[:-1][A[:-1] <= A[1:]] = 1

lower = np.maximum(T_low, A_low)
upper = np.minimum(T, A)
domain = np.maximum(0, upper - lower + 1) #変域が0の時は記録が間違い
answer = np.cumprod(domain)[-1]
print(answer % MOD)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

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

N = ir()
T = np.array(lr()).astype('int64')
A = np.array(lr()).astype('int64')
MOD = 10 ** 9 + 7

T_low = T.copy()
T_low[1:][T[1:] <= T[:-1]] = 1
A_low = A.copy()
A_low[:-1][A[:-1] <= A[1:]] = 1

lower = np.maximum(T_low, A_low)
upper = np.minimum(T, A)
domain = np.maximum(0, upper - lower + 1) #変域が0の時は記録が間違い
answer = np.cumprod(domain)[-1]
print(answer)

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
# Tは左からみた最高峰
# Aは右から見た最高峰
# maxの山は1箇所はある⇢そこではATの値は一致するはず
if A[0]!=T[-1]:
    print(0)
else:
    INFI=10**15
    cnt=[INFI]*(n)
    cnt[0]=1
    cnt[-1]=1
    for i in range(1,n):
        kaisa_a = T[i]-T[i-1]
        if kaisa_a ==0:
            cnt[i]=min(cnt[i],T[i])
        else:
            cnt[i]=1
        kaisa_b = A[-1-i] - A[-i]
        if kaisa_b ==0:
            cnt[-i-1]=min(cnt[-i-1],A[-i-1])
        else:
            cnt[-i-1]=1
    ans=1
    for i in range(n):
        ans*=cnt[i]
        ans%=mod
    print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    mod = 10 ** 9 + 7

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

    if t[-1] != a[0]:
        # 高橋と青木の見た山の高さの最大値が一致しない
        print(0)
        return

    ret = 1

    # [lo,hi]
    for i in range(n):
        if i == 0:
            lo = hi = t[i]
        else:
            if t[i - 1] < t[i]:
                # 山iの高さは一意に定まる
                lo = hi = t[i]
            elif t[i - 1] == t[i]:
                # [1,t[i]]の範囲で自由に選べる
                lo = 1
                hi = t[i]
            else:
                # t[i - 1] > t[i]:
                print(0)
                return

        if i == n - 1:
            lo = max(lo, a[i])
            hi = min(hi, a[i])
        else:
            if a[i] > a[i + 1]:
                # 山iの高さは一意に定まる
                lo = max(lo, a[i])
                hi = min(hi, a[i])
            elif a[i] == a[i + 1]:
                # [1,a[i]]の範囲で自由に選べる
                lo = max(lo, 1)
                hi = min(hi, a[i])
            else:
                # a[i] < a[i + 1]:
                print(0)
                return

        ret = ret * (hi - lo + 1) % mod

    print(ret)


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
ans = 1
if n == 1 and t != a:
    print(0)
    exit
for i in range(1,n-1):
    if t[i-1] == t[i] and a[i] == a[i+1]:
        ans = (ans*min(t[i],a[i]))%mod
    elif t[i-1] < t[i] and t[i] > a[i]:
        print(0)
        exit()
    elif a[i] > a[i+1] and 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()))
mod = 10**9+7
ans = 1
for i in range(1,n-1):
    if t[i-1] == t[i] and a[i+1] == a[i]:
        ans = (ans*min(t[i],a[i]))%mod
    elif t[i] > t[i-1] and t[i] > a[i]:
        print(0)
        exit()
    elif a[i+1] < a[i] and 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()))
mod = 10**9+7
ans = 1
for i in range(1,n-1):
    if t[i-1] == t[i] and a[i+1] == a[i]:
        ans = (ans*min(t[i],a[i]))%mod
    elif t[i] > t[i-1] and a[i+1] == a[i]:
        if t[i] > a[i]:
            print(0)
            exit()
    elif t[i] == t[i-1] and a[i+1] < a[i]:
        if t[i] < a[i]:
            print(0)
            exit()
print(ans)

Submission status: Wrong Answer

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

N=input()
T=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())

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

L=[ None for _ in range(N) ]

#一意に決まる山の高さを配列に代入。配列にNoneで残っているところが山の高さが一意に決まらない箇所
#左から
for i,t in enumerate(T):
    if i==0:
        L[i]=t
    else:
        if prev_t<t:
            L[i]=t
        elif prev_t>t:
            print 0
            quit()
    prev_t=t


#右から
for i,a in enumerate(A[::-1]):
    i=N-i-1 #左から見た順のインデックスに置き換える
    if i==N-1:
        L[i]=a
    else:
        if prev_a<a:
            if L[i] is not None:    #山の高さが既に入っているときは
                if L[i]!=a: #同じ高さじゃないと矛盾があるので、その場合はゼロで終了
                    print 0
                    quit()
            L[i]=a  #それ以外の場合は山の高さを配列に代入
        elif prev_a>a:
            print 0
            quit()
    prev_a=a

ans=1
mod=10**9+7
for x,a,t in zip(L,T,A):
    if x is None:   #山の高さがNoneになっている箇所は自由度がある所なので、aとtの最小値の通り数を答えにかける
        ans*=min(a,t)
        ans%=mod
print ans

Submission status: Wrong Answer

Solution code:
print 0

Submission status: Wrong Answer

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

N=input()
T=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())

L=[ None for _ in range(N) ]

#一意に決まる山の高さを配列に代入。配列にNoneで残っているところが山の高さが一意に決まらない箇所
#左から
for i,t in enumerate(T):
    if i==0:
        L[i]=t
    else:
        if prev_t<t:
            L[i]=t
        elif prev_t>t:
            print 0
            quit()
    prev_t=t


#右から
for i,a in enumerate(A[::-1]):
    i=N-i-1 #左から見た順のインデックスに置き換える
    if i==N-1:
        L[i]=a
    else:
        if prev_a<a:
            if L[i] is not None:    #山の高さが既に入っているときは
                if L[i]!=a: #同じ高さじゃないと矛盾があるので、その場合はゼロで終了
                    print 0
                    quit()
            L[i]=a  #それ以外の場合は山の高さを配列に代入
        elif prev_a>a:
            print 0
            quit()
    prev_a=a

if T[-1]<L[-1]:
    print 0
    quit()
if A[0]<L[0]:
    print 0
    quit()

ans=1
mod=10**9+7
for x,a,t in zip(L,T,A):
    if x is None:   #山の高さがNoneになっている箇所は自由度がある所なので、aとtの最小値の通り数を答えにかける
        ans*=min(a,t)
        ans%=mod
print ans

Submission status: Wrong Answer

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

N=input()
T=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())

L=[ None for _ in range(N) ]

#一意に決まる山の高さを配列に代入。配列にNoneで残っているところが山の高さが一意に決まらない箇所
#左から
for i,t in enumerate(T):
    if i==0:
        L[i]=t
    else:
        if prev_t<t:
            L[i]=t
        elif prev_t>t:
            print 0
            quit()
    prev_t=t


#右から
for i,a in enumerate(A[::-1]):
    i=N-i-1 #左から見た順のインデックスに置き換える
    if i==N-1:
        L[i]=a
    else:
        if prev_a<a:
            if L[i] is not None:    #山の高さが既に入っているときは
                if L[i]!=a: #同じ高さじゃないと矛盾があるので、その場合はゼロで終了
                    print 0
                    quit()
            L[i]=a  #それ以外の場合は山の高さを配列に代入
        elif prev_a>a:
            print 0
            quit()
    prev_a=a

ans=1
mod=10**9+7
for x,a,t in zip(L,T,A):
    if x is None:   #山の高さがNoneになっている箇所は自由度がある所なので、aとtの最小値の通り数を答えにかける
        ans*=min(a,t)
        ans%=mod
print ans

Submission status: Wrong Answer

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

N=input()
T=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())

L=[ None for _ in range(N) ]

#一意に決まる山の高さを配列に代入。配列にNoneで残っているところが山の高さが一意に決まらない箇所
#左から
for i,t in enumerate(T):
    if i==0:
        L[i]=t
    else:
        if prev_t<t:
            L[i]=t
    prev_t=t


#右から
for i,a in enumerate(A[::-1]):
    i=N-i-1 #左から見た順のインデックスに置き換える
    if i==N-1:
        L[i]=a
    else:
        if prev_a<a:
            if L[i] is not None:    #山の高さが既に入っているときは
                if L[i]!=a: #同じ高さじゃないと矛盾があるので、その場合はゼロで終了
                    print 0
                    quit()
            L[i]=a  #それ以外の場合は山の高さを配列に代入
    prev_a=a

ans=1
mod=10**9+7
for x,a,t in zip(L,T,A):
    if x is None:   #山の高さがNoneになっている箇所は自由度がある所なので、aとtの最小値の通り数を答えにかける
        ans*=min(a,t)
        ans%=mod
print ans

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n = II()
    t = LI()
    a = LI()[::-1]
    dt = [None] * n
    da = [None] * n
    ba = 0
    bt = 0
    for i in range(n):
        if bt < t[i]:
            dt[i] = (1, t[i])
            bt = t[i]
        else:
            dt[i] = (bt, bt)
    for i in range(n):
        if ba < a[i]:
            da[i] = (1, a[i])
            ba = a[i]
        else:
            da[i] = (ba, ba)
    da = da[::-1]
    ans = 1
    for i in range(n):
        if da[i][0] == 1:
            if da[i][1] > dt[i][1]:
                print(0)
                return
            continue
        if dt[i][0] == 1:
            if da[i][1] < dt[i][1]:
                print(0)
                return
            continue
        ans *= min(da[i][1], dt[i][1])
        ans %= mod
    print(ans)
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    A=list(map(int,input().split()))
    B=list(map(int,input().split()))
    
    # A,Bそれぞれについて、隣接している値が異なる -> その山の値は確定
    C=[None]*n
    C[0]=A[0]
    for i in range(1,n):
        if(A[i]!=A[i-1]):
            C[i]=A[i]

    D=[None]*n
    D[-1]=B[-1]
    for i in range(n-2,-1,-1):
        if(B[i]!=B[i+1]):
            D[i]=B[i]

    for c,d in zip(C,D):
        if((c and d) and (c!=d)):
            print(0)
            return


    ans=1
    for i in range(n):
        if(C[i] or D[i]): continue
        ans*=min(A[i],B[i])
        ans%=MOD

    print(ans)
resolve()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    A=list(map(int,input().split()))
    B=list(map(int,input().split()))

    # 値が正しいかをcheck
    if(max(A)!=max(B)):
        print(0)
        return

    # A,Bそれぞれについて、隣接している値が異なる -> その山の値は確定
    C=[None]*n
    C[0]=A[0]
    for i in range(1,n):
        if(A[i]!=A[i-1]):
            C[i]=A[i]

    if(C[-1] is not None and C[-1]!=B[-1]):
        print(0)
        return
    C[-1]=B[-1]
    for i in range(n-2,-1,-1):
        if(B[i]!=B[i+1]):
            if(C[i] is not None and C[i]!=B[i]):
                print(0)
                return
            C[i]=B[i]

    ans=1
    for i in range(n):
        if(C[i]): continue
        ans*=min(A[i],B[i])
        ans%=MOD

    print(ans)
resolve()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    A=list(map(int,input().split()))
    B=list(map(int,input().split()))

    # 値が正しいかをcheck
    # A,Bそれぞれについて、隣接している値が異なる -> その山の値は確定
    C=[None]*n
    C[0]=A[0]
    for i in range(1,n):
        if(A[i]!=A[i-1]):
            C[i]=A[i]

    if(C[-1] is not None and C[-1]!=B[-1]):
        print(0)
        return
    C[-1]=B[-1]
    for i in range(n-2,-1,-1):
        if(B[i]!=B[i+1]):
            if(C[i] is not None and C[i]!=B[i]):
                print(0)
                return
            C[i]=B[i]

    ans=1
    for i in range(n):
        if(C[i]): continue
        ans*=min(A[i],B[i])
        ans%=MOD

    print(ans)
resolve()

Submission status: Wrong Answer

Solution code:
n = int(input())
t = list(map(int, input().split()))
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
if t[0] > a[0] or t[-1] < a[-1]:
	print(0)
	exit()
renew = [0] * n
b = 0
for i, h in enumerate(t):
	if b < h:
		renew[i] = h
		b = h
b = 0
for i, h in enumerate(a[::-1]):
	if b < h:
		if 0 != renew[n - i - 1] != h:
			print(0)
			exit()
		renew[n - i - 1] = h
		b = h
ans = 1
for f, th, ah in zip(renew, t, a):
	if not f:
		ans *= min(th, ah)
		ans %= mod
print(ans)