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 sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())

mod = 10**9 + 7


def main():

    N = in_n()
    A = in_nl()
    T = in_nl()

    yama = [(0, 0)] * N
    yama[0] = (A[0], A[0])

    for i in range(1, N):
        pre_min_h, pre_max_h = yama[i - 1]
        now_h = A[i]

        if pre_max_h < now_h:
            now_min_h = now_h
            now_max_h = now_h
        else:
            now_min_h = 1
            now_max_h = pre_max_h

        yama[i] = (now_min_h, now_max_h)

    yama[-1] = (T[-1], T[-1])

    for i in range(N - 2, -1, -1):

        now_min_h, now_max_h = yama[i]
        pre_h = T[i + 1]
        now_h = T[i]

        if now_h > pre_h:
            if now_min_h <= now_h <= now_max_h:
                now_min_h = now_h
                now_max_h = now_h
            else:
                print(0)
                exit()
        else:
            now_max_h = min(now_max_h, now_h)

        yama[i] = (now_min_h, now_max_h)

    ans = 1
    for i in range(N):
        ans *= (yama[i][1] - yama[i][0] + 1)
        ans %= mod
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# 単調増加したらそこの山の高さは決まる。それ以外ならある高さ以内で自由に取れる。t,aで矛盾したら0
# 一人ずつ考えた
n = int(input())
t = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 1
MOD = 10**9 + 7
a_fixed = [-1] * n
t_fixed = [-1] * n

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

a_fixed[-1] = a[-1]
for i in range(n - 2, -1, -1):
    if a[i] > a[i + 1]:
        a_fixed[i] = a[i]


for i in range(n):
    if t_fixed[i] != -1 and a_fixed[i] != -1:
        if t_fixed[i] != a_fixed[i]:
            print(0)
            exit()
    elif t_fixed[i] != -1 or a_fixed[i] != -1:
        continue
    else:
        ans *= min(a[i], t[i])
        ans %= MOD
# print(fixed)
print(ans)

# if t[-1] != a[0]:
#     print(0)
#     exit()
# # # 最初の山チェック
# # if t[0] > a[0]:
# #     print(0)
# #     exit()
# # 最後の山の照合
# if fixed[-1] != -1:
#     if fixed[-1] != a[-1]:
#         print(0)
#         exit()
# fixed[-1] = a[-1]

# for i in range(n - 2, -1, -1):
#     if a[i] > a[i + 1]:  # この状況下でしか照合されないので最初の山の照合を取り逃している！
#         if fixed[i] == -1:
#             fixed[i] = a[i]
#         else:
#             if fixed[i] != a[i]:
#                 print(0)
#                 exit()
# for i in range(n):
#     if fixed[i] != -1:
#         continue
#     else:
#         ans *= min(a[i], t[i])
#         ans %= MOD
# # print(fixed)
# print(ans)

Submission status: Wrong Answer

Solution code:
# 単調増加したらそこの山の高さは決まる。それ以外ならある高さ以内で自由に取れる。t,aで矛盾したら0
# 一人ずつ考えた
n = int(input())
t = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 1
MOD = 10**9 + 7
fixed = [-1] * n
fixed[0] = t[0]
for i in range(1, n):
    if t[i] > t[i - 1]:
        fixed[i] = t[i]

if t[-1] != a[0]:
    print(0)
    exit()
# # 最初の山チェック
# if t[0] > a[0]:
#     print(0)
#     exit()
# 最後の山の照合
if fixed[-1] != -1:
    if fixed[-1] != a[-1]:
        print(0)
        exit()
fixed[-1] = a[-1]

for i in range(n - 2, -1, -1):
    if a[i] > a[i + 1]:  # この状況下でしか照合されないので最初の山の照合を取り逃している！
        if fixed[i] == -1:
            fixed[i] = a[i]
        else:
            if fixed[i] != a[i]:
                print(0)
                exit()
for i in range(n):
    if fixed[i] != -1:
        continue
    else:
        ans *= min(a[i], t[i])
        ans %= MOD
# print(fixed)
print(ans)

Submission status: Wrong Answer

Solution code:
# 単調増加したらそこの山の高さは決まる。それ以外ならある高さ以内で自由に取れる。t,aで矛盾したら0
# 一人ずつ考えた
n = int(input())
t = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 1
MOD = 10**9 + 7
fixed = [-1] * n
fixed[0] = t[0]
for i in range(1, n):
    if t[i] > t[i - 1]:
        fixed[i] = t[i]

# 最初の山チェック
if t[0] > a[0]:
    print(0)
    exit()
# 最後の山の照合
if fixed[-1] != -1:
    if fixed[-1] != a[-1]:
        print(0)
        exit()
fixed[-1] = a[-1]

for i in range(n - 2, -1, -1):
    if a[i] > a[i + 1]:  # この状況下でしか照合されないので最初の山の照合を取り逃している！
        if fixed[i] == -1:
            fixed[i] = a[i]
        else:
            if fixed[i] != a[i]:
                print(0)
                exit()
for i in range(n):
    if fixed[i] != -1:
        continue
    else:
        ans *= min(a[i], t[i])
        ans %= MOD
# print(fixed)
print(ans)

Submission status: Wrong Answer

Solution code:
print(0)

Submission status: Wrong Answer

Solution code:
# 単調増加したらそこの山の高さは決まる。それ以外ならある高さ以内で自由に取れる。t,aで矛盾したら0
# 一人ずつ考えた
n = int(input())
t = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 1
MOD = 10**9 + 7
fixed = [-1] * n
fixed[0] = t[0]
for i in range(1, n):
    if t[i] > t[i - 1]:
        fixed[i] = t[i]
if fixed[-1] != -1:
    if fixed[-1] != a[-1]:
        print(0)
        exit()
fixed[-1] = a[-1]

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

Submission status: Wrong Answer

Solution code:
# 単調増加したらそこの山の高さは決まる。それ以外ならある高さ以内で自由に取れる。t,aで矛盾したら0
# 一人ずつ考えた
n = int(input())
t = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 1
MOD = 10**9 + 7
fixed = [-1] * n
fixed[0] = t[0]
for i in range(1, n):
    if t[i] > t[i - 1]:
        fixed[i] = t[i]
if fixed[-1] != a[-1]:
    print(0)
    exit()
fixed[-1] = a[-1]

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

Submission status: Wrong Answer

Solution code:
# 単調増加したらそこの山の高さは決まる。それ以外ならある高さ以内で自由に取れる。t,aで矛盾したら0
# 一人ずつ考えた
n = int(input())
t = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 1
MOD = 10**9 + 7
fixed = [-1] * n
fixed[0] = t[0]
fixed[-1] = a[-1]
for i in range(1, n):
    if t[i] > t[i - 1]:
        fixed[i] = t[i]

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

Submission status: Wrong Answer

Solution code:
n = int(input())
t = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 1
MOD = 10**9 + 7
fixed = [-1] * n
fixed[0] = t[0]
fixed[-1] = a[0]
# a_fixed = [False] * n
# t_fixed = [False] * n
# t_fixed[0] = True
# a_fixed[-1] = True
for i in range(1, n):
    if t[i] > t[i - 1]:
        fixed[i] = t[i]

for i in range(n - 2, -1, -1):
    if a[i] > a[i + 1]:
        if fixed[i] == -1:
            fixed[i] = a[i]
        else:
            if fixed[i] != a[i]:
                print(0)
                exit()

# for i in range(1, n):
#     if a[i] > a[i - 1]:
#         if fixed[n - 1 - i] == -1:
#             fixed[n - 1 - i] = a[i]
#         else:
#             if fixed[n - 1 - i] != a[i]:
#                 print(0)
#                 exit()

for i in range(n):
    if fixed[i] != -1:
        continue
    else:
        ans *= min(a[i], t[i])
        ans %= MOD
# print(fixed)
print(ans)
# for i in range(1, n - 1):
#     if t[i] > t[i - 1] or a[i] > a[i - 1]:
#         fixed[i] = True
# for i in range(n):
#     if fixed[i]:
#         if a[i] != t[i]:
#             print(0)
#             exit()
#     else:
#         ans *= min(a[i], t[i])
#         ans %= MOD
# print(fixed)
# print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
t = list(map(int,input().split()))
a = list(map(int,input().split()))
k = [-1 for i in range(n)]
now = 0
if t[-1] != a[0]:
    print(0)
    exit()
for i in range(n):
    if t[i] > now:
        k[i] = t[i]
        now = t[i]

now = 0
for i in reversed(range(n)):
    if a[i] > now:
        if k[i] == -1:
            k[i] = a[i]
            now = a[i]
        elif k[i] == a[i]:
            now = a[i]
        elif k[i] != a[i]:
            print(0)
            exit()
ans = 1
l = 0
cou = 0
mod = 10**9 + 7
for i in range(n):
    if k[i] != -1 and l == 0:
        l = k[i]
    elif k[i] != -1 and l != 0:
        ans *= pow(min(l,k[i]),cou,mod)
        ans %= mod
        l = k[i]
        cou = 0
    elif k[i] == -1:
        cou += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
t = list(map(int,input().split()))
a = list(map(int,input().split()))
k = [-1 for i in range(n)]
now = 0
if t[0] != a[-1]:
    print(0)
    exit()
for i in range(n):
    if t[i] > now:
        k[i] = t[i]
        now = t[i]

now = 0
for i in reversed(range(n)):
    if a[i] > now:
        if k[i] == -1:
            k[i] = a[i]
            now = a[i]
        elif k[i] == a[i]:
            now = a[i]
        elif k[i] != a[i]:
            print(0)
            exit()
ans = 1
l = 0
cou = 0
mod = 10**9 + 7
for i in range(n):
    if k[i] != -1 and l == 0:
        l = k[i]
    elif k[i] != -1 and l != 0:
        ans *= pow(min(l,k[i]),cou,mod)
        ans %= mod
        l = k[i]
        cou = 0
    elif k[i] == -1:
        cou += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
t = list(map(int,input().split()))
a = list(map(int,input().split()))
k = [-1 for i in range(n)]
now = 0
for i in range(n):
    if t[i] > now:
        k[i] = t[i]
        now = t[i]

now = 0
for i in reversed(range(n)):
    if a[i] > now:
        if k[i] == -1:
            k[i] = a[i]
            now = a[i]
        elif k[i] == a[i]:
            now = a[i]
        elif k[i] != a[i]:
            print(0)
            exit()
ans = 1
l = 0
cou = 0
mod = 10**9 + 7
for i in range(n):
    if k[i] != -1 and l == 0:
        l = k[i]
    elif k[i] != -1 and l != 0:
        ans *= pow(min(l,k[i]),cou,mod)
        ans %= mod
        l = k[i]
        cou = 0
    elif k[i] == -1:
        cou += 1
print(ans)

Submission status: Wrong Answer

Solution code:
mod=10**9+7
n=int(input())
rec,cer=tuple(map(int,input().split())),tuple(map(int,input().split()))
table=[-1]*n#確定した山ならば、その高さ。-1は未確定
table[0]=rec[0]
table[-1]=cer[-1]
if rec[-1]!=cer[0]:print(0);exit()#同じ山々を踏破してるので全体の最大値が違うのは矛盾
for i in range(1,n):
    if rec[i]>rec[i-1]:
        if table[i]!=-1 and table[i]!=rec[i]:print(0);exit()
        table[i]=rec[i]
        if cer[i]<table[i]:print(0);exit()
    if cer[n-i-1]>cer[n-i]:
        if table[n-i-i]!=-1 and table[n-i-1]!=cer[n-i-1]:print(0);exit()
        table[n-i-1]=cer[n-i-1]
        if rec[n-i-1]<table[n-i-1]:print(0);exit()
#各人について、「記録が真に増加したなら、その山の高さは確定する」は真。もう一人の情報と矛盾しないかも見る。
start,f=0,0
now=0
won=0
for i in range(n):
    now=max(now,table[i])
    won=max(won,table[-i-1])
    if now!=rec[i] or won!=cer[-i-1]:print(0);exit()
if now!=won:print(0);exit()
#念のため二人の情報と矛盾しないかシミュレーションしておく
ans=1
for i,v in enumerate(table):
    if v==-1 and f==0:
        start=i
        f=1    #[start,?)に連続して高さ未定義の山が続く
    if v!=-1 and f:
        kosuu=i-start #[start,v)であることがわかったので個数は左
        f=0
        ans=ans*pow(min(v,table[start-1]),kosuu,mod)%mod
        #高さ未定義の山が続く群に対して、そいつらは両隣に高さを抑えられるので、場合の数はこうなる
        #table[0]とtable[n-1]については高さが必ず確定するので、両隣に確定済みの山は必ず存在する

print(ans)

Submission status: Wrong Answer

Solution code:
mod=10**9+7
n=int(input())
rec,cer=tuple(map(int,input().split())),tuple(map(int,input().split()))
table=[-1]*n#確定した山ならば、その高さ。-1は未確定
table[0]=rec[0]
table[-1]=cer[-1]
if rec[-1]!=cer[0]:print(0);exit()#同じ山々を踏破してるので全体の最大値が違うのは矛盾
for i in range(1,n):
    if rec[i]>rec[i-1]:
        if table[i]!=-1 and table[i]!=rec[i]:print(0);exit()
        table[i]=rec[i]
        if cer[i]<table[i]:print(0);exit()
    if cer[n-i-1]>cer[n-i]:
        if table[n-i-i]!=-1 and table[n-i-1]!=cer[n-i-1]:print(0);exit()
        table[n-i-1]=cer[n-i-1]
        if rec[n-i-1]<table[n-i-1]:print(0);exit()
#二人の記録が真に増加したなら、その山の高さは確定する。もう一人の情報と矛盾しないかも見る。
start,f=0,0
now=0
won=0
for i in range(n):
    now=max(now,table[i])
    won=max(won,table[-i-1])
    if now!=rec[i] or won!=cer[-i-1]:print(0);exit()
if now!=won:print(0);exit()
#念のため二人の情報と矛盾しないかシミュレーションしておく
ans=1
for i,v in enumerate(table):
    if v==-1 and f==0:
        start=i
        f=1    #[start,?)に連続して高さ未定義の山が続く
    if v!=-1 and f:
        kosuu=i-start #[start,v)であることがわかったので個数は左
        f=0
        ans=ans*pow(min(v,table[start-1]),kosuu,mod)%mod
        #高さ未定義の山が続く群に対して、そいつらは両隣に高さを抑えられるので、場合の数はこうなる
        #table[0]とtable[n-1]については高さが必ず確定するので、両隣に確定済みの山は必ず存在する

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
ans=1
rec,cer=tuple(map(int,input().split())),tuple(map(int,input().split()))
table=[-1]*n
table[0]=rec[0]
table[-1]=cer[-1]
if rec[-1]!=cer[0]:print(0);exit()
for i in range(1,n):
    if rec[i]>rec[i-1]:
        if table[i]!=-1 and table[i]!=rec[i]:print(0);exit()
        table[i]=rec[i]
        if cer[i]<table[i]:print(0);exit()
    if cer[n-i-1]>cer[n-i]:
        if table[n-i-i]!=-1 and table[n-i-1]!=cer[n-i-1]:print(0);exit()
        table[n-i-1]=cer[n-i-1]
        if rec[n-i-1]<table[n-i-1]:print(0);exit()
start,f=0,0
mod1,mod2=10**9+7,998244353
now=0
won=0
for i in range(n):
    now=max(now,table[i])
    won=max(won,table[-i-1])
    if now!=rec[i] or won!=cer[-i-1]:print(0);exit()
mod=mod1
if now!=won:print(0);exit()
for i,v in enumerate(table):
    if v==-1 and f==0:
        start=i
        f=1
    if v!=-1 and f:
        kosuu=i-start
        f=0
        ans=ans*pow(min(v,table[start-1]),kosuu,mod)%mod
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
ans=1
rec,cer=tuple(map(int,input().split())),tuple(map(int,input().split()))
table=[-1]*n
table[0]=rec[0]
table[-1]=cer[-1]
if rec[-1]!=cer[0]:print(0);exit()
for i in range(1,n):
    if rec[i]>rec[i-1]:
        if table[i]!=-1 and table[i]!=rec[i]:print(0);exit()
        table[i]=rec[i]
    if cer[n-i-1]>cer[n-i]:
        if table[n-i-i]!=-1 and table[n-i-1]!=cer[n-i-1]:print(0);exit()
        table[n-i-1]=cer[n-i-1]
start,f=0,0
mod1,mod2=10**9+7,998244353
now=0
won=0
for i in range(n):
    now=max(now,table[i])
    won=max(won,table[-i-1])
    if now!=rec[i] or won!=cer[-i-1]:print(0);exit()
mod=mod1
if now!=won:print(0);exit()
for i,v in enumerate(table):
    if v==-1 and f==0:
        start=i
        f=1
    if v!=-1 and f:
        kosuu=i-start
        f=0
        ans=ans*pow(min(v,table[start-1]),kosuu,mod)%mod
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
ans=1
rec,cer=tuple(map(int,input().split())),tuple(map(int,input().split()))
table=[-1]*n
table[0]=rec[0]
table[-1]=cer[-1]
if rec[-1]!=cer[0]:print(0);exit()
for i in range(1,n):
    if rec[i]>rec[i-1]:
        if table[i]!=-1 and table[i]!=rec[i]:print(0);exit()
        table[i]=rec[i]
    if cer[n-i-1]>cer[n-i]:
        if table[n-i-i]!=-1 and table[n-i-1]!=cer[n-i-1]:print(0);exit()
        table[n-i-1]=cer[n-i-1]
start,f=0,0
mod1,mod2=10**9+7,998244353
now=0
won=0
for i in range(n):
    now=max(now,table[i])
    won=max(won,table[-i-1])
    if now!=rec[i] or won!=cer[-i-1]:print(0);exit()
mod=mod1
for i,v in enumerate(table):
    if v==-1 and f==0:
        start=i
        f=1
    if v!=-1 and f:
        kosuu=i-start
        f=0
        ans=ans*pow(min(v,table[start-1]),kosuu,mod)%mod
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
ans=1
rec,cer=tuple(map(int,input().split())),tuple(map(int,input().split()))
table=[-1]*n
table[0]=rec[0]
table[-1]=cer[-1]

for i in range(1,n):
    if rec[i]>rec[i-1]:
        if table[i]!=-1 and table[i]!=rec[i]:print(0);exit()
        table[i]=rec[i]
    if cer[n-i-1]>cer[n-i]:
        if table[n-i-i]!=-1 and table[n-i-1]!=cer[n-i-1]:print(0);exit()
        table[n-i-1]=cer[n-i-1]
start,f=0,0
mod1,mod2=10**9+7,998244353
mod=mod1
for i,v in enumerate(table):
    if v==-1 and f==0:
        start=i
        f=1
    if v!=-1 and f:
        kosuu=i-start
        f=0
        ans=ans*pow(min(v,table[start-1]),kosuu,mod)%mod
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
ans=1
rec,cer=tuple(map(int,input().split())),tuple(map(int,input().split()))
table=[-1]*n
table[0]=rec[0]
table[-1]=cer[-1]
if rec[-1]!=cer[0]:print(0);exit()
for i in range(1,n):
    if rec[i]>rec[i-1]:
        if table[i]!=-1 and table[i]!=rec[i]:print(0);exit()
        table[i]=rec[i]
    if cer[n-i-1]>cer[n-i]:
        if table[n-i-i]!=-1 and table[n-i-1]!=cer[n-i-1]:print(0);exit()
        table[n-i-1]=cer[n-i-1]
start,f=0,0
mod1,mod2=10**9+7,998244353
mod=mod1
for i,v in enumerate(table):
    if v==-1 and f==0:
        start=i
        f=1
    if v!=-1 and f:
        kosuu=i-start
        f=0
        ans=ans*pow(min(v,table[start-1]),kosuu,mod)%mod
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = [0]*n
ans2 = [0]*n
a=list(map(int, input().split()))
b=list(map(int, input().split()))
amax=0
bmax=0

for i in range(n//2):
  if a[i]>amax:
    amax=a[i]
    ans[i]=1
    ans2[i]=a[i]
  elif a[i]<amax:
    print(0)
    quit()
  else:
    ans[i]=amax
  if b[-(i+1)]>bmax:
    bmax=b[-(i+1)]
    ans[-(i+1)]=1
    ans2[-(i+1)]=b[-(i+1)]
  elif b[-i-1]<bmax:
    print(0)
    quit()
  else:
    ans[-(i+1)]=bmax

if n%2==1:  
  i=n//2
  if a[i]>amax:
    amax=a[i]
    ans[i]=1
    ans2[i]=a[i]
  elif a[i]<amax:
    print(0)
    quit()
  else:
    ans[i]=amax
    
for i in range(n//2, n):
  if a[i]>amax:
    amax=a[i]
    if ans[i]==1 and a[i]!=ans2[i]:
        print(0)
        quit()
    elif ans[i]<a[i]:
      print(0)
      quit()
    ans[i]=1
  elif a[i]<amax:
    print(0)
    quit()
  else:
    ans[i]=min(amax, ans[i])
  if b[-(i+1)]>bmax:
    bmax=b[-(i+1)]
    if ans[-(i+1)]==1 and b[-(i+1)]!=ans2[-(i+1)]:
        print(0)
        quit()
    elif ans[-i-1]<b[-i-1]:
      print(0)
      quit()
    ans[-(i+1)]=1
  elif b[-i-1]<bmax:
    print(0)
    quit()
  else:
    ans[-(i+1)]=min(bmax, ans[-(i+1)])
    
kotae=1
for i in range(n):
  kotae*=ans[i]
  kotae%=(10**9+7)
print(kotae)

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = [0]*n
ans2 = [0]*n
a=list(map(int, input().split()))
b=list(map(int, input().split()))
amax=0
bmax=0

for i in range(n//2):
  if a[i]>amax:
    amax=a[i]
    ans[i]=1
    ans2[i]=a[i]
  elif a[i]<amax:
    print(0)
    quit()
  else:
    ans[i]=amax
  if b[-(i+1)]>bmax:
    bmax=b[-(i+1)]
    ans[-(i+1)]=1
    ans2[-(i+1)]=b[-(i+1)]
  elif b[-i-1]<bmax:
    print(0)
    quit()
  else:
    ans[-(i+1)]=bmax

if n%2==1:  
  i=n//2
  if a[i]>amax:
    amax=a[i]
    ans[i]=1
    ans2[i]=a[i]
  elif a[i]<amax:
    print(0)
    quit()
  else:
    ans[i]=amax
    
for i in range(n//2, n):
  if a[i]>amax:
    amax=a[i]
    #if ans[i]==1 and a[i]!=ans2[i]:
    #    print(0)
    #    quit()
    if ans[i]<a[i]:
      print(0)
      quit()
    ans[i]=1
  elif a[i]<amax:
    print(0)
    quit()
  else:
    ans[i]=min(amax, ans[i])
  if b[-(i+1)]>bmax:
    bmax=b[-(i+1)]
    #if ans[-(i+1)]==1 and b[-(i+1)]!=ans2[-(i+1)]:
        #print(0)
        #quit()
    if ans[-i-1]<b[-i-1]:
      print(0)
      quit()
    ans[-(i+1)]=1
  elif b[-i-1]<bmax:
    print(0)
    quit()
  else:
    ans[-(i+1)]=min(bmax, ans[-(i+1)])
    
kotae=1
for i in range(n):
  kotae*=ans[i]
  kotae%=(10**9+7)
print(kotae)

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = [0]*n
ans2 = [0]*n
a=list(map(int, input().split()))
b=list(map(int, input().split()))
amax=0
bmax=0

for i in range(n//2):
  if a[i]>amax:
    amax=a[i]
    ans[i]=1
    ans2[i]=a[i]
  elif a[i]<amax:
    print(0)
    quit()
  else:
    ans[i]=amax
  if b[-(i+1)]>bmax:
    bmax=b[-(i+1)]
    ans[-(i+1)]=1
    ans2[-(i+1)]=b[-(i+1)]
  elif b[-i-1]<bmax:
    print(0)
    quit()
  else:
    ans[-(i+1)]=bmax

if n%2==1:  
  i=n//2
  if a[i]>amax:
    amax=a[i]
    ans[i]=1
    ans2[i]=a[i]
  elif a[i]<amax:
    print(0)
    quit()
  else:
    ans[i]=amax
    
for i in range(n//2, n):
  if a[i]>amax:
    amax=a[i]
    if ans[i]==1 and a[i]!=ans2[i]:
        print(0)
        quit()
    ans[i]=1
  elif a[i]<amax:
    print(0)
    quit()
  else:
    ans[i]=min(amax, ans[i])
  if b[-(i+1)]>bmax:
    bmax=b[-(i+1)]
    if ans[-(i+1)]==1 and b[-(i+1)]!=ans2[-(i+1)]:
        print(0)
        quit()
    ans[-(i+1)]=1
  elif b[-i-1]<bmax:
    print(0)
    quit()
  else:
    ans[-(i+1)]=min(bmax, ans[-(i+1)])
    
kotae=1
for i in range(n):
  kotae*=ans[i]
  kotae%=(10**9+7)
print(kotae)

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = [0]*n
ans2 = [0]*n
a=list(map(int, input().split()))
b=list(map(int, input().split()))
amax=0
bmax=0

for i in range(n//2):
  if a[i]>amax:
    amax=a[i]
    ans[i]=1
    ans2[i]=a[i]
  else:
    ans[i]=amax
  if b[-(i+1)]>bmax:
    bmax=b[-(i+1)]
    ans[-(i+1)]=1
    ans2[-(i+1)]=b[-(i+1)]
  else:
    ans[-(i+1)]=bmax

if n%2==1:  
  i=n//2
  if a[i]>amax:
    amax=a[i]
    ans[i]=1
    ans2[i]=a[i]
  else:
    ans[i]=amax
#print(ans)

for i in range(n//2, n):
  if a[i]>amax:
    amax=a[i]
    if ans[i]==1 and a[i]!=ans2[i]:
        print(0)
        quit()
    ans[i]=1
  else:
    ans[i]=min(amax, ans[i])
  if b[-(i+1)]>bmax:
    bmax=b[-(i+1)]
    if ans[-(i+1)]==1 and b[-(i+1)]!=ans2[-(i+1)]:
        print(0)
        quit()
    ans[-(i+1)]=1
  else:
    ans[-(i+1)]=min(bmax, ans[-(i+1)])
    
#print(ans)
#print(ans2)




kotae=1
for i in range(n):
  kotae*=ans[i]
  kotae%=(10**9+7)
print(kotae)

Submission status: Wrong Answer

Solution code:
def print0():
    print(0)
    exit()

n = int(input())
t = list(map(int, input().split()))
a = list(map(int, input().split()))
mod = pow(10, 9) + 7
h = [0] * n
unknown = [1] * n
h[0], unknown[0] = t[0], 1
for i in range(1, n):
    if t[i] == t[i - 1]:
        unknown[i] = t[i]
    else:
        h[i] = t[i]
if h[-1] == 0:
    h[-1] = a[-1]
    unknown[-1] = 1
else:
    if h[-1] < a[-1]:
        print0()
    else:
        h[-1] = a[-1]
        unknown[-1] = 1
for i in range(-2, -n - 1, -1):
    if a[i] == a[i + 1]:
        if h[i] == 0:
            unknown[i] = min(unknown[i], a[i])
    else:
        if h[i] == 0:
            h[i] = a[i]
            unknown[i] = 1
        else:
            if not a[i] == h[i]:
                print0()
ans = 1
for i in unknown:
    ans *= i
    ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
Ts = list(map(int, input().split()))
As = list(map(int, input().split()))

prev = 0
inds = []
for t in Ts:
    if prev < t:
        inds.append((t,1))
    else:
        inds.append((t,0))
    prev = t
indsA = []
prev = 0
for a in As[::-1]:
    if prev < a:
        indsA.append((a,1))
    else:
        indsA.append((a,0))
    prev = a
# print(inds, indsA[::-1])
# if Ts[-1] != As[0]:
#     print(0)
#     exit()
ans = 1
MOD = 10**9 + 7
for it, ia in zip(inds, indsA[::-1]):
    # print(it,ia)
    if it[1] == 1 and ia[1] == 1:
        if it[0] != ia[0]:
            # 矛盾
            print(0)
            exit()
        else:
            continue
    elif it[1] == 1 or ia[1] == 1:continue
    mn = min(it[0],ia[0])
    ans *= mn
    ans %= MOD
    # print(it,ia,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
Ts = list(map(int, input().split()))
As = list(map(int, input().split()))

prev = 0
inds = []
for t in Ts:
    if prev < t:
        inds.append((t,1))
    else:
        inds.append((t,0))
    prev = t
indsA = []
prev = 0
for a in As[::-1]:
    if prev < a:
        indsA.append((a,1))
    else:
        indsA.append((a,0))
    prev = a
ans = 1
MOD = 10**9 + 7
for it, ia in zip(inds, indsA[::-1]):
    # print(it,ia)
    if it[1] == 1 and ia[1] == 1:
        if it[0] != ia[0]:
            # 矛盾
            print(0)
            exit()
        else:
            continue
    elif it[1] == 1 or ia[1] == 1:continue
    mn = min(it[0],ia[0])
    ans *= mn
    ans %= MOD
    print(it,ia,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
Ts = list(map(int, input().split()))
As = list(map(int, input().split()))

prev = 0
inds = []
for t in Ts:
    if prev < t:
        inds.append((t,1))
    else:
        inds.append((t,0))
    prev = t
indsA = []
prev = 0
for a in As[::-1]:
    if prev < a:
        indsA.append((a,1))
    else:
        indsA.append((a,0))
    prev = a
# print(inds, indsA[::-1])
if Ts[-1] != As[0]:
    print(0)
    exit()
ans = 1
MOD = 10**9 + 7
for it, ia in zip(inds, indsA[::-1]):
    if it[1] == 1 and ia[1] == 1:
        if it[0] != ia[0]:
            # 矛盾
            print(0)
            exit()
        else:
            continue
    elif it[1] == 1 or ia[1] == 1:continue
    mn = min(it[0],ia[0])
    ans *= mn
    ans %= MOD
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
Ts = list(map(int, input().split()))
As = list(map(int, input().split()))

prev = 0
inds = []
for t in Ts:
    if prev < t:
        inds.append((t,1))
    else:
        inds.append((t,0))
    prev = t
indsA = []
prev = 0
for a in As[::-1]:
    if prev < a:
        indsA.append((a,1))
    else:
        indsA.append((a,0))
    prev = a
# print(inds, indsA[::-1])
if Ts[-1] != As[0]:
    print(0)
    exit()
ans = 1
MOD = 10**9 + 7
for it, ia in zip(inds, indsA[::-1]):
    if it[1] == 1 or ia[1] == 1:continue
    mn = min(it[0],ia[0])
    ans *= mn
    ans %= MOD
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
T = tuple(map(int, input().split()))
A = tuple(map(int, input().split()))
mod = 10**9+7

mt = max(T)
ma = max(A)
if mt != ma:
    print(0)
else:
    ans = 1
    for i in range(1, n-1):
        if T[i-1] == T[i] and A[i] == A[i+1]:
            ans *= min(T[i], A[i])
            ans %= mod
        if T[i] == A[i] and (T[i] != mt or A[i] != ma):
            print(0)
            break
    else:
        print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
T = tuple(map(int, input().split()))
A = tuple(map(int, input().split()))
mod = 10**9+7

if max(T) != max(A):
    print(0)
else:
    ans = 1
    for i in range(1, n-1):
        if T[i-1] == T[i] and A[i] == A[i+1]:
            ans *= min(T[i], A[i])
            ans %= mod
    print(ans)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/code-festival-2016-qualc/tasks/codefestival_2016_qualC_c
# C - 二人のアルピニスト

N=int(input())
T=list(map(int,input().split()))
A=list(map(int,input().split()))
p=[0]*N
q=[0]*N
kari=[0]*N
check=[0]*N
MOD=10**9+7

for i in range(N):
    if i==0:
        p[i]=1
        max_T=T[i]
        kari[i]=T[i]
    else:
        if T[i]<=T[i-1]:
            p[i]=max_T
            if p[i]==1:
                kari[i]=1
        else:
            p[i]=1
            max_T = T[i]    
            kari[i]= T[i]
            
for i in range(N-1,-1,-1):
    if i==N-1:
        q[i]=1
        max_A=A[i]
        check[i]=A[i]
    else:
        if A[i]<=A[i+1]:
            q[i]=max_A
            if q[i]==1:
                check[i]=1
        else:
            q[i]=1
            max_A=A[i]
            check[i]= A[i]

r=[min(p[i],q[i]) for i in range(N)]
#print(p)
#print("kari:",kari)
#print(q)
#print("check:",check)
#print(r)

for j in range(N):
    if kari[j]>0 and check[j]>0:
        if kari[j]!=check[j]:
            print(0)
            break
else:
    ans=1
    for num in r:
        ans = (ans*num)%MOD
    print(ans)

Submission status: Wrong Answer

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

mod = 10**9+7
if max(t) != max(s):
    print(0)
    exit()
ans = 1
for i in range(1,n+1):
    d = False
    if t[i] != t[i-1]:
        d = True
        x = t[i]
    if s[i] != s[i+1]:
        if d:
            if x != s[i]:
                print(0)
                exit()
        else:
            d = True
    if not d:
        ans *= min(s[i],t[i])
        ans %= mod
print(ans%mod)