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:
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]:
        if t[i] < t[i-1]:
            print(0)
            exit()
        else:
            d = True
            x = t[i]
    if s[i] != s[i+1]:
        if s[i] < s[i+1]:
            print(0)
            exit()
        else:
            if d:
                if x != s[i]:
                    print(0)
                    exit()
            else:
                d = True
                x = s[i]
    if not d:
        ans *= min(s[i],t[i])
        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()))
H = [0]*N
flag=0

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

H[N-1]=A[N-1]

for i in range(1,N):
    if A[N-i-1]!=A[N-i]:
        if (H[N-i-1]<0 and A[N-i-1] > -1*H[N-i-1]) or (H[N-i-1]>0 and A[N-i-1] > H[N-i-1]):
            flag=1
            break
        H[N-i-1]=A[N-i-1]
    else:
        H[N-i-1]=max(-1*A[N-i-1],H[N-i-1])

if flag==0:
    p = 10**9+7
    out = 1
    for i in range(N):
        if H[i]<0:
            out = out * (-1*H[i]) % p
    print(out)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
n=int(input())

t=list(map(int,input().split()))
a=list(map(int,input().split()))
#nは10**5以下

print(0)
exit()

kakute=[0]*n
kakute[0]=1

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

a=a[::-1]
ans=1

for i in range(1,n):
    index=n-i-1
    #青木が確定したとき
    if a[i]>a[i-1]:
        #矛盾ないか
        if a[i]>t[index]:
            print(0)
            exit()
        elif kakute[index]==1:
            if a[i] != t[index]:
                print(0)
                exit()
    #青木は確定じゃない
    else:
        #高橋確定
        if kakute[index]==1:
            if t[index]>a[i]:
                print(0)
                exit()
        else:
            ans=ans*min(a[i],t[index])%(10**9+7)

print(ans%(10**9+7))

Submission status: Wrong Answer

Solution code:
n=int(input())

t=list(map(int,input().split()))
a=list(map(int,input().split()))
#nは10**5以下

kakute=[0]*n
kakute[0]=1

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

a=a[::-1]
ans=1

for i in range(1,n):
    index=n-i-1
    #青木が確定したとき
    if a[i]>a[i-1]:
        #矛盾ないか
        if a[i]>t[index]:
            print(0)
            exit()
        elif kakute[index]==1:
            if a[i] != t[index]:
                print(0)
                exit()
    #青木は確定じゃない
    else:
        #高橋確定
        if kakute[index]==1:
            if t[index]>a[i]:
                print(0)
                exit()
        else:
            ans=ans*min(a[i],t[index])%(10**9+7)

print(ans%(10**9+7))

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

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

fix = [False for i in range(N)]
fix[0] = True
for i in range(1, N):
    if T[i] > T[i-1]:
        fix[i] = True

# print(fix)

if fix[N-1] is True and T[N-1] != A[N-1]:
    print(0)
    exit()
else:
    fix[N-1] = True
    T[N-1] = A[N-1]

for j in range(N-2, 0, -1):
    if A[j] > A[j+1]:
        if fix[j] is True and A[j] != T[j]:
            print(0)
            exit()
        else:
            fix[j] = True
            T[j] = A[j]
    elif fix[j] is not True:
        T[j] = min(T[j], A[j])


MOD = 10**9 + 7
ans = 1
for i in range(N):
    if fix[i] is False:
        ans *= T[i]
        ans %= MOD
print(ans)

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

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

fix = [False for i in range(N)]
fix[0] = True
for i in range(1, N):
    if T[i] > T[i-1]:
        fix[i] = True


fix[N-1] = True
T[N-1] = A[N-1]

for j in range(N-2, 0, -1):
    if A[j] > A[j+1]:
        if fix[j] is True and A[j] < T[j]:
            print(0)
            exit()
        else:
            fix[j] = True
            T[j] = A[j]
    elif fix[j] is not True:
        T[j] = min(T[j], A[j])


MOD = 10**9 + 7
ans = 1
for i in range(N):
    if fix[i] is False:
        ans *= T[i]
        ans %= MOD
print(ans)

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

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

fix = [False for i in range(N)]
fix[0] = True
for i in range(1, N):
    if T[i] < T[i-1]:
        print(0)
        exit()
    elif T[i] > T[i-1]:
        fix[i] = True

if fix[N-1] is True and T[N-1] > A[N-1]:
    print(0)
    exit()
else:
    fix[N-1] = True
    T[N-1] = A[N-1]

for j in range(N-2, 0, -1):
    if A[j] < A[j+1]:
        print(0)
        exit()
    elif A[j] > A[j+1]:
        if fix[j] is True and A[j] < T[j]:
            print(0)
            exit()
        else:
            fix[j] = True
            T[j] = A[j]
    elif fix[j] is not True:
        T[j] = min(T[j], A[j])


MOD = 10**9 + 7
ans = 1
for i in range(N):
    if fix[i] is False:
        ans *= T[i]
        ans %= MOD
print(ans)

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

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

fix = [False for i in range(N)]
fix[0] = True
for i in range(1, N):
    if T[i] != T[i-1]:
        fix[i] = True

if fix[N-1] is True and T[N-1] > A[N-1]:
    print(0)
    exit()
else:
    fix[N-1] = True
    T[N-1] = A[N-1]

for j in range(N-2, 0, -1):
    if A[j] != A[j+1]:
        if fix[j] is True and A[j] < T[j]:
            print(0)
            exit()
        else:
            fix[j] = True
            T[j] = A[j]
    elif fix[j] is not True:
        T[j] = min(T[j], A[j])


MOD = 10**9 + 7
ans = 1
for i in range(N):
    if fix[i] is False:
        ans *= T[i]
        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()))
H = [0]*N
Hmax = [0]*N
out = -1

H[0] = T[0]
for i in range(1,N):
    if T[i]>T[i-1]:
        H[i]=T[i]
    else:
        Hmax[i]=T[i]

H[N-1] = A[N-1]
for i in range(1,N):
    if A[N-i-1]>A[N-i]:
        if H[N-i-1]!=0 and H[N-i-1]!=A[N-i-1]:
            out = 0
            break
        else:
            H[N-i-1]=A[N-i-1]
    else:
        Hmax[N-i-1]=min(Hmax[N-i-1],A[N-i-1])

if out==0:
    print(out)
else:
    out=1
    p = 10**9+7
    for i in range(N):
        if H[i]==0:
            out*=Hmax[i]
            out = out%p
    print(out)

Submission status: Wrong Answer

Solution code:
MOD = 10**9+7
n = int(input())
T = list(map(int, input().split()))
A = list(map(int, input().split()))
n_cases = [0]*n
n_cases[0] = n_cases[-1] = 1
height = [0]*n
if T[-1] != A[0]:
    print(0)
    exit()
height[0] = T[0]
height[-1] = A[-1]
for i, (t1, t2) in enumerate(zip(T, T[1:]), 1):
    if t1 != t2:
        n_cases[i] = 1
        height[i] = t2


for i, (a1, a2) in enumerate(zip(A[::-1], A[::-1][1:]), 2):
    if a1 != a2:
        n_cases[-i] = 1
        if height[-i] == a2 or height[-i] == 0:
            continue
        print(0)
        exit()

for i, (t, a) in enumerate(zip(T, A)):
    if n_cases[i]:
        continue
    n_cases[i] = min(t, a)


ans = 1
for x in n_cases:
    ans *= x
    ans %= MOD
print(ans)

Submission status: Wrong Answer

Solution code:
MOD = 10**9+7
n = int(input())
T = list(map(int, input().split()))
A = list(map(int, input().split()))
n_cases = [0]*n
n_cases[0] = n_cases[-1] = 1
height = [0]*n
if n == 1 and T[0] != A[-1]:
    print(0)
    exit()
height[0] = T[0]
height[-1] = A[-1]
for i, (t1, t2) in enumerate(zip(T, T[1:]), 1):
    if t1 != t2:
        n_cases[i] = 1
        height[i] = t2
        

for i, (a1, a2) in enumerate(zip(A[::-1], A[::-1][1:]), 2):
    if a1 != a2:
        n_cases[-i] = 1
        if height[-i] == a2 or height[-i] == 0:
            continue
        print(0)
        exit()

for i, (t, a) in enumerate(zip(T, A)):
    if n_cases[i]:
        continue
    n_cases[i] = min(t, a)


ans = 1
for x in n_cases:
    ans *= x
    ans %= MOD
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    T = list(map(int, input().split()))
    A = list(map(int, input().split()))[::-1]

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

    H = [0] * n
    H[0] = T[0]
    ma = [1] * n
    for i in range(1, n):
        if T[i] == 1 or T[i - 1] < T[i]:
            H[i] = T[i]
        else:
            ma[i] = T[i]

    H = H[::-1]
    ma = ma[::-1]
    if H[0] != 0 and H[0] != A[0]:
        print(0)
        exit()

    H[0] = A[0]
    ma[0] = 1
    for i in range(1, n):
        if A[i] == 1 or A[i - 1] < A[i]:
            if H[i] != 0 and H[i] != A[i]:
                print(0)
                exit()
            else:
                if H[i] != 0 and H[i] > A[i]:
                    print(0)
                    exit()
                H[i] = A[i]
                ma[i] = 1
        else:
            ma[i] = min(ma[i], A[i])

    res = 1
    for i in ma:
        res = res * i % mod
    print(res % mod)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    T = list(map(int, input().split()))
    A = list(map(int, input().split()))[::-1]

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

    H = [0] * n
    H[0] = T[0]
    ma = [1] * n
    for i in range(1, n):
        if T[i] == 1 or T[i - 1] < T[i]:
            H[i] = T[i]
        else:
            ma[i] = T[i]

    H = H[::-1]
    ma = ma[::-1]
    # 今までの記録と矛盾してたら0を返す
    if H[0] != 0 and H[0] != A[0]:
        print(0)
        exit()

    H[0] = A[0]
    ma[0] = 1
    for i in range(1, n):
        if A[i] == 1 or A[i - 1] < A[i]:
            # 今までの記録と矛盾してたら0を返す
            if H[i] != 0 and H[i] != A[i]:
                print(0)
                exit()
            else:
                H[i] = A[i]
                ma[i] = 1
        else:
            ma[i] = min(ma[i], A[i])

    res = 1
    for i in ma:
        res = res * i % mod
    print(res % mod)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    T = list(map(int, input().split()))
    A = list(map(int, input().split()))[::-1]

    H = [0] * n
    H[0] = T[0]
    ma = [1] * n
    for i in range(1, n):
        if T[i] == 1 or T[i - 1] < T[i]:
            H[i] = T[i]
        else:
            ma[i] = T[i]

    H = H[::-1]
    ma = ma[::-1]
    if H[0] != 0 and H[0] != A[0]:
        print(0)
        exit()

    H[0] = A[0]
    ma[0] = 1
    for i in range(1, n):
        if A[i] == 1 or A[i - 1] < A[i]:
            if H[i] != 0 and H[i] != A[i]:
                print(0)
                exit()
            else:
                H[i] = A[i]
                ma[i] = 1
        else:
            ma[i] = min(ma[i], A[i])

    res = 1
    for i in ma:
        res = res * i % mod
    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


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

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

    # 左からみていく
    ans = 1
    for i in range(1, N - 1):
        if T[i - 1] < T[i] and A[i] > A[i + 1]:
            if T[i] == A[i]:
                continue
            else:
                print(0)
                return
        elif A[i] > A[i + 1]:
            if A[i] > T[i]:
                print(0)
                continue
        elif T[i - 1] < T[i]:
            if T[i] > A[i]:
                print(0)
                continue
        else:
            m = min(A[i], T[i])
            ans *= m
            ans %= MOD

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


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

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

    # 左からみていく
    ans = 1
    for i in range(1, N + 1):
        if T[i - 1] < T[i] and A[i] > A[i + 1]:
            if T[i] == A[i]:
                continue
            else:
                print(0)
                return
        elif A[i] > A[i + 1]:
            if A[i] > T[i]:
                print(0)
                continue
        elif T[i - 1] < T[i]:
            if T[i] > A[i]:
                print(0)
                continue
        else:
            m = min(A[i], T[i])
            ans *= m
            ans %= MOD

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
MOD = 10**9 + 7
def main():
    n = int(input())
    t = list(map(int, input().split()))
    a = list(map(int, input().split()))
    h = [None]*n
    h[0] = t[0]
    for i in range(1, n):
        if t[i-1] < t[i]:
            h[i] = t[i]
    f = True
    if h[n-1] is not None and h[n-1] != a[n-1]:
        f = False
    if h[n-1] is None:
        h[n-1] = a[n-1]
    for i in reversed(range(n-1)):
        if a[i] > a[i+1]:
            if h[i] is None:
                h[i] = a[i]
            else:
                if h[i] != a[i]:
                    f = False
    if f:
        ans = 1
        for i in range(n):
            if h[i] is None:
                ans *= min(t[i], a[i])
                ans %= MOD
        print(ans % MOD)
    else:
        print(0)

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()))
H1,H2=[0]*N,[0]*N
h=0
for i in range(N):
  if h<T[i]:
    H1[i]=T[i]
    h=T[i]
h=0
for i in range(N):
  if h<A[-1-i]:
    H2[-1-i]=A[-1-i]
    h=A[-1-i]

H=[0]*N
for i in range(N):
  if H1[i]!=0 and H2[i]!=0 and H1[i]!=H2[i]:
    print(0)
    exit()
  else:
    H[i]=max(H1[i],H2[i])

ans=1
left=H[0]
right=0
cnt=0
for i in range(1,N):
  if H[i]!=0 and cnt==0:
    left=H[i]
  elif H[i]!=0:
    right=H[i]
    d=min(left,right)
    ans*=pow(d,cnt,10**9+7)
    ans%=10**9+7
    cnt=0
    left=H[i]
  elif H[i]==0:
    cnt+=1
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
T=list(map(int, input().split()))
A=list(map(int, input().split()))
H1,H2=[0]*N,[0]*N
h=0
for i in range(N):
  if h<T[i]:
    H1[i]=T[i]
    h=T[i]
h=0
for i in range(N):
  if h<A[-1-i]:
    H2[-1-i]=A[-1-i]
    h=A[-1-i]

H=[0]*N
for i in range(N):
  if H1[i]!=0 and H2[i]!=0 and H1[i]!=H2[i]:
    print(-1)
    exit()
  else:
    H[i]=max(H1[i],H2[i])

ans=1
left=H[0]
right=0
cnt=0
for i in range(1,N):
  if H[i]!=0 and cnt==0:
    left=H[i]
  elif H[i]!=0:
    right=H[i]
    d=min(left,right)
    ans*=pow(d,cnt,10**9+7)
    ans%=10**9+7
    cnt=0
    left=H[i]
  elif H[i]==0:
    cnt+=1
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
mod=10**9+7
INF=10**9+1 
u=[INF]*n
d=[1]*n
u[0]=a[0]
d[0]=a[0]
for i in range(1,n):
    if a[i]-a[i-1]:
        d[i]=a[i]
    u[i]=a[i]
u[-1]=b[-1]
d[-1]=b[-1]
for i in range(n-2,-1,-1):
    if b[i]-b[i+1]:
        if b[i]<d[i] or b[i]>u[i]:
            print(0)
            exit()
        d[i]=b[i]
    u[i]=min(b[i],u[i])
ans=1
for i in range(n):
    ans=ans*(u[i]-d[i]+1)%mod
print(ans)

Submission status: Wrong Answer

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

arr = [[1, INF] for _ in range(n)]
cur_max = -1
for i in range(n):
    cur = t[i]
    if cur > cur_max:
        arr[i][0] = cur
        arr[i][1] = cur
        cur_max = cur
    else:
        arr[i][1] = cur

cur_max = -1
for i in reversed(range(n)):
    cur = a[i]
    if cur > cur_max:
        arr[i][0] = cur
        arr[i][1] = min(cur, arr[i][1])
        cur_max = cur
    else:
        arr[i][1] = min(cur, arr[i][1])

for i in range(n):
    if arr[i][0] > arr[i][1]:
        print(0)
        exit()

ans = 1
for i in range(n):
    ans *= arr[i][1] - arr[i][0] + 1
    ans %= MOD
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
t = list(map(int, input().split()))
a = list(map(int, input().split()))
MOD = 10**9 + 7
if t[0] != a[-1]:
  print(0)
else:
  idx = []
  before = 0
  for i in range(n):
    if before == t[i]:
      idx.append(i)
    before = t[i]
  new_index = []
  if len(idx) == 0:
    print(1)
  else:
    for j in idx:
      if j < n-1:
        if a[j] == a[j+1]:
          new_index.append(j)
    if len(new_index) == 0:
      print(1)
    else:
      ans = 1
      for k in new_index:
        ans *= min(t[k], a[k])
        ans %= MOD
      print(ans)

Submission status: Wrong Answer

Solution code:
mod = 1000000007
n= int(input())
t= list(map(int,input().split()))
a= list(map(int,input().split()))
tb = [-1]*n
pre=-1
for i in range(n):
    if i==0:tb[i]=1
    else:
        if pre!=t[i]:tb[i]=1
    pre=t[i]
ab = [-1]*n
post=-1
for i in range(n-1,-1,-1):
    if i==n-1:ab[i]=1
    else:
        if post!=a[i]:
            ab[i]=1
    post=a[i]
er = False
b=1
for i in range(n):
    if tb[i]==1 or ab[i]==1:
        if tb[i]==1 and ab[i]==1:
            if t[i]==a[i]:pass
            else:
                er=True
                break
        else:pass
    else:
        m = min(a[i],t[i])
        b = ((b% mod) * (m % mod)) % mod
if er:print(0)
else:print(b)

Submission status: Wrong Answer

Solution code:
mod = 1000000007
n= int(input())
t= list(map(int,input().split()))
a= list(map(int,input().split()))
tb = [-1]*n
pre=-1
for i in range(n):
    if i==0:tb[i]=1
    else:
        if pre!=t[i]:tb[i]=1
    pre=t[i]
ab = [-1]*n
post=-1
for i in range(n-1,-1,-1):
    if i==n-1:post=a[i]
    else:
        if post!=a[i]:
            ab[i]=1
    post=a[i]
er = False
b=1
for i in range(n):
    if tb[i]==1 or ab[i]==1:
        if tb[i]==1 and ab[i]==1:
            if t[i]==a[i]:pass
            else:
                er=True
                break
        else:pass
    else:
        m = min(a[i],t[i])
        b = ((b% mod) * (m % mod)) % mod
if er:print(0)
else:print(b)

Submission status: Wrong Answer

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

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

h = [0]*N
ma = -1
for i, t in enumerate(T):
	if t > ma:
		h[i] = t
		ma = t

ma = -1
for i, a in enumerate(A[::-1]):
	if a > ma:
		if h[-i-1] and a != h[-i-1]:
			print(0)
			exit()
		h[-i-1] = a
		ma = a

acc_t = list(accumulate(h, max))
acc_a = list(accumulate(h[::-1], max))[::-1]

mi = [min(t, a) for t, a in zip(T, A)]

ans = 1
for i in range(N):
	if h[i] == 0:
		ans *= mi[i]
		ans %= mod
print(ans)

Submission status: Wrong Answer

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

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

h = [0]*N
ma = -1
for i, t in enumerate(T):
	if t > ma:
		h[i] = t
		ma = t

ma = -1
for i, a in enumerate(A[::-1]):
	if a > ma:
		if h[-i-1] and a != h[-i-1]:
			print(0)
			exit()
		h[-i-1] = a
		ma = a

zero = [1 if x == 0 else 0 for x in h]
for i in range(1, N):
	if h[i] == 0:
		h[i] = max(h[i], h[i-1])

for i in range(1, N):
	if zero[-i-1]:
		h[-i-1] = min(h[-i-1], h[-i])

ans = 1
for i in range(N):
	if zero[i]:
		ans *= h[i]
		ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
t = na()
a = na()
mod = 10**9+7

if max(t) != max(a):
    print(0)
    exit()

dp = [0]*n

for i in range(n):
    if i == 0:
        dp[i] = -1
        dp[-i-1] = -1
        continue
    if t[i] > t[i-1]: dp[i] = -1
    if a[-i-1] > a[-i]: dp[-i-1] = -1
    if t[i] == t[i-1] and dp[i] != -1:
        dp[i] = t[i] if dp[i] == 0 else min(dp[i],t[i])
    if a[-i-1] == a[-i] and dp[-i-1] != -1:
        dp[-i-1] = a[-i-1] if dp[-i-1] == 0 else min(dp[-i-1],a[-i-1])

ans = 1
for d in dp:
    if d > 0: ans = ans*d%mod

print(ans)

Submission status: Wrong Answer

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

ans = 1
mod = 10**9 + 7
inf = float('inf')
M = [inf]*n
M[0] = -T[0]
M[n-1] = -A[n-1]
for i in range(n-1):
    if T[i] == T[i+1]:
        M[i+1] = min(M[i+1],T[i])
    elif M[i+1] == inf or M[i+1] == -T[i+1]:
        M[i+1] = -T[i+1]
    else:
        print(0)
        exit(0)
    if A[-1-i] == A[-2-i]:
        M[-2-i] = min(M[-2-i],A[-1-i])
    elif M[-2-i] == inf or M[-2-i] == -A[-2-i]:
        M[-2-i] = -A[-2-i]
    else:
        print(0)
        exit(0)
for m,t,a in zip(M,T,A):
    if m > 0:
        if m <= t and m <= a:
            ans *= m
            ans %= mod
        else:
            print(0)
            exit(0)
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
T=list(map(int,input().split()))
A=list(map(int,input().split()))
cand=[-1]*N
flag=1
ans=1
p=10**9+7
for i in range(N):
    if i==0:
        if (T[N-1-i]!=A[i]):
            flag=0
        cand[i]=T[i]
        cand[N-1-i]=A[N-1-i]
    else:
        #print(i,cand[i],T[i],N-1-i,cand[N-1-i],A[N-1-i])
        if T[i]!=T[i-1]:
            if cand[i]==-1:
                cand[i]=T[i]
            elif cand[i]!=T[i]:
                flag=0
        if A[N-1-i]!=A[N-i]:
            if cand[N-1-i]==-1:
                cand[N-1-i]=A[N-1-i]
            elif cand[N-1-i]!=A[N-1-i]:
                flag=0
    #print(cand)
    #input()

#print(flag,cand)
lmax=[0]*N
rmax=[0]*N
for i in range(N):
    if i==0:
        lmax[i]=cand[i]
        rmax[N-1-i]=cand[N-1-i]
    else:
        if cand[i]==-1:
            lmax[i]=lmax[i-1]
        else:
            lmax[i]=cand[i]

        if cand[N-i-1]==-1:
            rmax[N-i-1]=rmax[N-i]
        else:
            rmax[N-i-1]=cand[N-i-1]
#print(lmax,rmax)
for i in range(N):
    if cand[i]==-1:
        ans=(ans*min(A[i],T[i]))%p
if flag==0:
    print("0")
else:
    print(ans%p)

Submission status: Wrong Answer

Solution code:
N=int(input())
T=list(map(int,input().split()))
A=list(map(int,input().split()))
cand=[-1]*N
flag=1
ans=1
p=10**9+7
for i in range(N):
    if i==0:
        if (T[i]!=A[N-1-i]):
            flag=0
        cand[i]=T[i]
        cand[N-1-i]=A[N-1-i]
    else:
        #print(i,cand[i],T[i],N-1-i,cand[N-1-i],A[N-1-i])
        if T[i]!=T[i-1]:
            if cand[i]==-1:
                cand[i]=T[i]
            elif cand[i]!=T[i]:
                flag=0
        if A[N-1-i]!=A[N-i]:
            if cand[N-1-i]==-1:
                cand[N-1-i]=A[N-1-i]
            elif cand[N-1-i]!=A[N-1-i]:
                flag=0
    #print(cand)
    #input()

#print(flag,cand)
lmax=[0]*N
rmax=[0]*N
for i in range(N):
    if i==0:
        lmax[i]=cand[i]
        rmax[N-1-i]=cand[N-1-i]
    else:
        if cand[i]==-1:
            lmax[i]=lmax[i-1]
        else:
            lmax[i]=cand[i]

        if cand[N-i-1]==-1:
            rmax[N-i-1]=rmax[N-i]
        else:
            rmax[N-i-1]=cand[N-i-1]
#print(lmax,rmax)
for i in range(N):
    if cand[i]==-1:
        ans=(ans*min(A[i],T[i]))%p
if flag==0:
    print("0")
else:
    print(ans%p)

Submission status: Wrong Answer

Solution code:
N=int(input())
T=list(map(int,input().split()))
A=list(map(int,input().split()))
cand=[-1]*N
flag=1
ans=1
p=10**9+7
for i in range(N):
    if i==0:
        if (i==N-i-1)and(T[i]!=A[N-1-i]):
            flag=0
        cand[i]=T[i]
        cand[N-1-i]=A[N-1-i]
    else:
        if T[i]!=T[i-1]:
            if cand[i]==-1:
                cand[i]=T[i]
            elif cand[i]!=T[i]:
                flag=0
        if A[N-1-i]!=A[N-i]:
            if cand[N-1-i]==-1:
                cand[N-1-i]=A[N-1-i]
            elif cand[N-1-i]!=A[N-1-i]:
                flag=0
    #print(cand)
    #input()
#print(flag,cand)
lmax=[0]*N
rmax=[0]*N
for i in range(N):
    if i==0:
        lmax[i]=cand[i]
        rmax[N-1-i]=cand[N-1-i]
    else:
        if cand[i]==-1:
            lmax[i]=lmax[i-1]
        else:
            lmax[i]=cand[i]

        if cand[N-i-1]==-1:
            rmax[N-i-1]=rmax[N-i]
        else:
            rmax[N-i-1]=cand[N-i-1]
#print(lmax,rmax)
for i in range(N):
    if cand[i]==-1:
        ans=(ans*min(A[i],T[i]))%p
if flag==0:
    print("0")
else:
    print(ans%p)

Submission status: Wrong Answer

Solution code:
N=int(input())
T=list(map(int,input().split()))
A=list(map(int,input().split()))
cand=[-1]*N
flag=1
ans=1
p=10**9+7
for i in range(N):
    if i==0:
        if (i==N-i-1)and(T[i]!=A[N-1-i]):
            flag=0
        cand[i]=T[i]
        cand[N-1-i]=A[N-1-i]
    else:
        if T[i]!=T[i-1]:
            if cand[i]==-1:
                cand[i]=T[i]
            elif cand[i]!=T[i]:
                flag=0
        if A[N-1-i]!=A[N-i]:
            if cand[N-1-i]==-1:
                cand[N-1-i]=A[N-1-i]
            elif cand[N-1-i]!=A[N-1-i]:
                flag=0
    #print(cand)
    #input()
#print(flag,cand)
lmax=[0]*N
rmax=[0]*N
for i in range(N):
    if i==0:
        lmax[i]=cand[i]
        rmax[N-1-i]=cand[N-1-i]
    else:
        if cand[i]==-1:
            lmax[i]=lmax[i-1]
        else:
            lmax[i]=cand[i]

        if cand[N-i-1]==-1:
            rmax[N-i-1]=rmax[N-i]
        else:
            rmax[N-i-1]=cand[N-i-1]
#print(lmax,rmax)
for i in range(N):
    if cand[i]==-1:
        ans=(ans*min(lmax[i],rmax[i]))%p
if flag==0:
    print("0")
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
N,*L = map(int, open(0).read().split())
MOD = 10**9+7
T = L[:N]
A = L[N:]
h = [0]*N
p = -1
for i in range(N):
  if p != T[i]:
    p = T[i]
    h[i] = T[i]
p = -1
for i in range(N-1,-1,-1):
  if p!=A[i]:
    if h[i]!=0 and h[i]!=A[i]:
      print(0)
      sys.exit()
    p = A[i]
    h[i] = A[i]
B = [min(t,a) for t,a in zip(T,A)]
ans = 1
for i in range(N):
  if h[i]==0:
    ans *= B[i]
    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()))
exact = [0] * N # 高さが確定した場所
seem = [0] * N # 高さが確定しない場所のあり得る最大値

cur = 0
for i in range(len(T)):
  if cur < T[i]:
    exact[i] = T[i]
    cur = T[i]
  else:
    seem[i] = cur
    
cur = 0
for i in range(len(A)-1,-1,-1):
  if cur < A[i]:
    if exact[i] != 0 and exact[i] != A[i]: # 既に記録された確定数値が今回の値と異なる場合
      print(0)
      exit(0)
    if exact[i] == 0 and seem[i] < exact[i]:# ここで確定させる数値が高橋くん的にあり得るかチェック
      print(0)
      exit(0)
    exact[i] = A[i]
    cur = A[i]
  else:
    seem[i] = min(seem[i],cur)
    
DIV = 10 ** 9 + 7
ans = 1
for i in range(N):
  if exact[i] != 0: # 確定数値があれば場合の数は変わらない
    continue
  else:
    ans *= seem[i]
    ans %= DIV

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
T = list(map(int,input().split()))
A = list(map(int,input().split()))
exact = [0] * N # 高さが確定した場所
seem = [0] * N # 高さが確定しない場所のあり得る最大値

cur = 0
for i in range(len(T)):
  if cur < T[i]:
    exact[i] = T[i]
    cur = T[i]
  else:
    seem[i] = cur
    
cur = 0
for i in range(len(A)-1,-1,-1):
  if cur < A[i]:
    if exact[i] != 0 and exact[i] != A[i]: # 既に記録された確定数値が今回の値と異なる場合
      print(0)
      exit(0)
    exact[i] = A[i]
    # ここで確定した数値が高橋くん的にあり得るかチェック
    if seem[i] < exact[i]:
      print(0)
      exit(0)
    cur = A[i]
  else:
    seem[i] = min(seem[i],cur)
    
DIV = 10 ** 9 + 7
ans = 1
for i in range(N):
  if exact[i] != 0: # 確定数値があれば場合の数は変わらない
    continue
  else:
    ans *= seem[i]
    ans %= DIV

print(ans)