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 = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
result = 1
for i in range(n):
    h_max, h_min = min(t[i], a[i])+1, 1
    if i == 0:
        h_min = t[0]
    else:
        if t[i-1] < t[i]:
            h_min = t[i]

    if i == n-1:
        h_min = a[n-1] 
    else:
        if a[i] > a[i+1]:
            h_min = a[i]
    result *= max(h_max - h_min, 0)%(10**9+7)
print(result%(10**9+7))

Submission status: Wrong Answer

Solution code:
n = int(input())
t = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
result = 1
for i in range(n):
    h_max, h_min = min(t[i], a[i])+1, 1
    if i == 0:
        h_min = t[0]
    else:
        if t[i-1] < t[i]:
            h_min = t[i]

    if i == n-1:
        h_min = a[n-1] 
    else:
        if a[i] > a[i+1]:
            h_min = a[i]
    result *= max(h_max - h_min, 0)
print(result%(10**9+7))

Submission status: Wrong Answer

Solution code:
n = int(input())
t = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
result = 1
for i in range(n):
    h_max, h_min = min(t[i], a[i])+1, 1
    if i == 0:
        h_min = t[0]
    else:
        if t[i-1] < t[i]:
            h_min = t[i]

    if i == n-1:
        h_min = a[n-1] 
    else:
        if a[i] > a[i+1]:
            h_min = a[i]
    result *= h_max - h_min
print(result%(10**9+7))

Submission status: Wrong Answer

Solution code:

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


    maxh = t[:]
    minflag = [0 for _ in range(n)]

    if t[-1] != a[0]:
        return 0
    else:
        maxflag = t[-1]
    maxh[0] = t[0]
    minflag[0] = 1

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

    maxh[-1] = a[-1]
    minflag[-1] = 1

    for i in range(n-2,-1,-1):
        if a[i] > t[i]:
            if i == n-2:
                break
            return 0
        elif a[i] == t[i]:
            if a[i] != maxflag:
                return 0
            else:
                minflag[i] = 1
                break
        maxh[i] = a[i]
        if a[i] > a[i+1]:
            minflag[i] = 1

    res = 1
    for i in range(n):
        if not minflag[i]:
            res *= maxh[i]
            res %= (10**9+7)
    return res % (10**9+7)

print(main())

Submission status: Wrong Answer

Solution code:

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


    maxh = t[:]
    minh = [0 for _ in range(n)]

    maxflag = max(t)
    maxh[0] = t[0]
    minh[0] = 1

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

    if t[-1] != a[0]:
        return 0
    maxh[-1] = a[-1]
    minh[-1] = 1

    for i in range(n-2,-1,-1):
        if a[i] > t[i]:
            return 0
        elif t[i] < maxflag:
            return 0
        elif a[i] == t[i]:
            if a[i] != maxflag:
                return 0
            else:
                minh[i] = 1
                break
        if a[i] > a[i+1]:
            maxh[i] = a[i]
            minh[i] = 1
        else:
            maxh[i] = min(maxh[i],a[i])
    res = 1
    for i in range(n):
        if not minh[i]:
            res *= maxh[i]
            res %= (10**9+7)
    return res % (10**9+7)

print(main())

Submission status: Wrong Answer

Solution code:

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


    maxh = t[:]
    minh = [0 for _ in range(n)]

    maxflag = max(t)
    maxh[0] = t[0]
    minh[0] = 1

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

    if t[-1] != a[0]:
        return 0
    maxh[-1] = a[-1]
    minh[-1] = 1

    for i in range(n-2,-1,-1):
        if a[i] > t[i]:
            return 0
        elif a[i] == t[i]:
            if a[i] != maxflag:
                return 0
            else:
                minh[i] = 1
                break
        if a[i] > a[i+1]:
            maxh[i] = a[i]
            minh[i] = 1
        else:
            maxh[i] = min(maxh[i],a[i])
    res = 1
    for i in range(n):
        if not minh[i]:
            res *= maxh[i]
            res %= (10**9+7)
    return res % (10**9+7)

print(main())

Submission status: Wrong Answer

Solution code:

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


    maxh = t[:]
    minh = [0 for _ in range(n)]

    maxflag = max(t)
    maxh[0] = t[0]
    minh[0] = 1

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

    if t[-1] != a[0]:
        return 0
    maxh[-1] = a[-1]
    minh[-1] = 1

    for i in range(n-2,-1,-1):
        if a[i] >= t[i]:
            if a[i] != maxflag:
                return 0
            else:
                minh[i] = 1
                break
        if a[i] > a[i+1]:
            maxh[i] = a[i]
            minh[i] = 1
        else:
            maxh[i] = min(maxh[i],a[i])
    res = 1
    for i in range(n):
        if not minh[i]:
            res *= maxh[i]
            res %= (10**9+7)
    return res % (10**9+7)

print(main())

Submission status: Wrong Answer

Solution code:

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


    if t[-1] != a[0]:
        return 0

    maxh = [0 for _ in range(n)]
    minh = [0 for _ in range(n)]

    maxh[0] = t[0]
    minh[0] = t[0]

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

    maxh[-1] = a[-1]
    minh[-1] = a[-1]

    for i in range(n-2,-1,-1):
        if a[i] > a[i+1]:
            maxh[i] = a[i]
            minh[i] = a[i]
        else:
            maxh[i] = min(maxh[i],a[i])
    res = 1
    for i in range(n):
        if maxh[i] != minh[i]:
            res *= maxh[i]
    return res % (10**9+7)

print(main())

Submission status: Wrong Answer

Solution code:

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

    maxh = [0 for _ in range(n)]
    minh = [0 for _ in range(n)]

    maxh[0] = t[0]
    minh[0] = t[0]

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

    if maxh[-1] < a[-1]:
        return 0
    else:
        maxh[-1] = a[-1]
        minh[-1] = a[-1]

    for i in range(n-2,-1,-1):
        if a[i] > a[i+1]:
            maxh[i] = a[i]
            minh[i] = a[i]
        else:
            maxh[i] = min(maxh[i],a[i])
    res = 1
    for i in range(n):
        if maxh[i] != minh[i]:
            res *= maxh[i]
    return res % (10**9+7)

print(main())

Submission status: Wrong Answer

Solution code:
import sys
n = int(input())
t = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
a.append(0)
result = 1
for i in range(1, n):
    if (t[i-1] < t[i] and t[i] > a[i]) or (a[i+1] < a[i] and t[i] < a[i]):
        print(0)
        sys.exit()
    elif t[i-1] < t[i] or a[i+1] < a[i]:
        continue
    else:
        result *= min(t[i], a[i])%(10**9 + 7)
print(result)

Submission status: Wrong Answer

Solution code:
import sys
n = int(input())
t = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
a.append(0)
result = 1
for i in range(1, n):
    if (t[i-1] < t[i] and t[i] > a[i]) or (a[i+1] < a[i] and t[i] < a[i]):
        print(0)
        sys.exit()
    elif t[i-1] < t[i] or a[i+1] < a[i]:
        continue
    else:
        result *= min(t[i], a[i])
print(result%(10**9 + 7))

Submission status: Wrong Answer

Solution code:
import sys
n = int(input())
t = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
a.append(a[-1])
for i in range(1, n):
    print(t[i], a[i])
    if t[i] > t[i-1]:
        if t[i] <= a[i]:
            a[i] = 1
            continue
        else:
            print(0)
            sys.exit()
    elif a[i] > a[i+1]:
        if t[i] >= a[i]:
            a[i] = 1
            continue
        else:
            print(0)
            sys.exit()

result = []
for i in range(n):
    result.append(min(t[i], a[i]))
counter = 1
for i in range(1, n-1):
    counter *= result[i]
print(counter%(10**9+7))

Submission status: Wrong Answer

Solution code:
n=int(input())
s=list(map(int,input().split()))
a=list(map(int,input().split()))
uselist=[0 for _ in range(n)]
cnt=0
for i in range(n):
    if cnt!=s[i]:
        cnt=s[i]
        uselist[i]=cnt
    else:
        continue

###
uselist1=[0 for _ in range(n)]
count=0
for i in range(n):
    if count!=a[n-1-i]:
        count=a[n-1-i]
        uselist1[n-1-i]=count
    else:
        continue

###
flag=True
anslist=[0 for _ in range(n)]
for i in range(n):
    if uselist[i]!=0 and uselist1[i]!=0:
        if uselist[i]!=uselist1[i]:
            flag=False
            break
        else:
            anslist[i]=uselist[i]
    elif uselist[i]!=0 and uselist1[i]==0:
        anslist[i]=uselist[i]
    elif uselist[i]==0 and uselist1[i]!=0:
        anslist[i]=uselist1[i]
    else:
        continue
for i in range(n):
    if anslist[n-1-i]>uselist1[n-1-i]:
        flag=False
        break
        
        
ans=1
checker=0
index=0
i=1
if flag:
    anslist.append(0)
    anslist.insert(0,0)
    while i<=n:
        if index==0 and anslist[i]!=0:
            checker=anslist[i]
            i+=1
            
        elif index==0 and anslist[i]==0:
            index+=1
            i+=1
            
        elif index!=0 and anslist[i]!=0:
            checker=min(checker,anslist[i])
            use=1
            for _ in range(index):
                use=(use*checker)%(10**9+7)
            ans=(ans*(use))%(10**9+7)
            checker=anslist[i]
            index=0
            i+=1
        elif index!=0 and anslist[i]==0:
            index+=1
            i+=1
    print(ans%(10**9+7))
if not flag:
    print(0)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=list(map(int,input().split()))
a=list(map(int,input().split()))
uselist=[0 for _ in range(n)]
cnt=0
for i in range(n):
    if cnt!=s[i]:
        cnt=s[i]
        uselist[i]=cnt
    else:
        continue

###
uselist1=[0 for _ in range(n)]
count=0
for i in range(n):
    if count!=a[n-1-i]:
        count=a[n-1-i]
        uselist1[n-1-i]=count
    else:
        continue

###
flag=True
anslist=[0 for _ in range(n)]
for i in range(n):
    if uselist[i]!=0 and uselist1[i]!=0:
        if uselist[i]!=uselist1[i]:
            flag=False
            break
        else:
            anslist[i]=uselist[i]
    elif uselist[i]!=0 and uselist1[i]==0:
        anslist[i]=uselist[i]
    elif uselist[i]==0 and uselist1[i]!=0:
        anslist[i]=uselist1[i]
    else:
        continue

if flag:

    anslist.append(0)
    anslist.insert(0,0)
    ans=1
    checker=0
    index=0
    i=1
    while i<=n:
        if index==0 and anslist[i]!=0:
            checker=anslist[i]
            i+=1
            
        elif index==0 and anslist[i]==0:
            index+=1
            i+=1
            
        elif index!=0 and anslist[i]!=0:
            checker=min(checker,anslist[i])
            use=1
            for _ in range(index):
                use=(use*checker)%(10**9+7)
            ans=(ans*(use))%(10**9+7)
            checker=anslist[i]
            index=0
            i+=1
        elif index!=0 and anslist[i]==0:
            index+=1
            i+=1
    print(ans%(10**9+7))
else:
    print(0)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=list(map(int,input().split()))
a=list(map(int,input().split()))
uselist=[0 for _ in range(n)]
cnt=0
for i in range(n):
    if cnt!=s[i]:
        cnt=s[i]
        uselist[i]=cnt
    else:
        continue

###
uselist1=[0 for _ in range(n)]
count=0
for i in range(n):
    if count!=a[n-1-i]:
        count=a[n-1-i]
        uselist1[n-1-i]=count
    else:
        continue

###
flag=True
anslist=[0 for _ in range(n)]
for i in range(n):
    if uselist[i]!=0 and uselist1[i]!=0:
        if uselist[i]!=uselist1[i]:
            flag=False
            break
        else:
            anslist[i]=uselist[i]
    elif uselist[i]!=0 and uselist1[i]==0:
        anslist[i]=uselist[i]
    elif uselist[i]==0 and uselist1[i]!=0:
        anslist[i]=uselist1[i]
    else:
        continue

if flag:

    anslist.append(0)
    anslist.insert(0,0)
    print(anslist)

    ans=1
    checker=0
    index=0
    i=1
    while i<=n:
        if index==0 and anslist[i]!=0:
            checker=anslist[i]
            i+=1
            
        elif index==0 and anslist[i]==0:
            index+=1
            i+=1
            
        elif index!=0 and anslist[i]!=0:
            checker=min(checker,anslist[i])
            use=1
            for _ in range(index):
                use=(use*checker)%(10**9+7)
            ans=(ans*(use))%(10**9+7)
            checker=anslist[i]
            index=0
            i+=1
        elif index!=0 and anslist[i]==0:
            index+=1
            i+=1
    print(ans%(10**9+7))
else:
    print(0)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=list(map(int,input().split()))
a=list(map(int,input().split()))
uselist=[0 for _ in range(n)]
cnt=0
for i in range(n):
    if cnt!=s[i]:
        cnt=s[i]
        uselist[i]=cnt
    else:
        continue

###
uselist1=[0 for _ in range(n)]
count=0
for i in range(n):
    if count!=a[n-1-i]:
        count=a[n-1-i]
        uselist1[n-1-i]=count
    else:
        continue

###
flag=True
anslist=[0 for _ in range(n)]
for i in range(n):
    if uselist[i]!=0 and uselist1[i]!=0:
        if uselist[i]!=uselist1[i]:
            flag=False
            break
        else:
            anslist[i]=uselist[i]
    elif uselist[i]!=0 and uselist1[i]==0:
        anslist[i]=uselist[i]
    elif uselist[i]==0 and uselist1[i]!=0:
        anslist[i]=uselist1[i]
    else:
        continue
if flag:

    anslist.append(0)
    anslist.insert(0,0)

    ans=1
    checker=0
    index=0
    i=1
    while i<=n:
        if index==0 and anslist[i]!=0:
            checker=anslist[i]
            i+=1
            
        elif index==0 and anslist[i]==0:
            index+=1
            i+=1
            
        elif index!=0 and anslist[i]!=0:
            checker=min(checker,anslist[i])
            use=1
            for _ in range(index):
                use=(use*checker)%(10**9+7)
            ans=(ans*(use))%(10**9+7)
            checker=anslist[i]
            index=0
            i+=1
            
        elif index!=0 and anslist[i]==0:
            index+=1
            i+=1
    print(ans)
            
else:
    print(0)

Submission status: Wrong Answer

Solution code:
n = int(input())
tn = [int(i) for i in input().split()]
an = [int(i) for i in input().split()]
atn = [0]+[tn[i] if tn[i] <= an[i] else an[i] for i in range(n)]+[0]
matn = max(atn)
if matn != max(an) | matn != max(tn):
  print(0)
  exit()

ans = 1
mod = 10**9+7

for i in range(1,n+1):
  if atn[i-1] == atn[i] <= atn[i+1]:
    ans *= atn[i]
  elif atn[i-1] >= atn[i] == atn[i+1]:
    ans *= atn[i]
  ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n=int(input())
t=list(map(int,input().split()))
a=list(map(int,input().split()))
h=[0]*n
h[0]=t[0]
h[-1]=a[-1]
for i in range(n):
    if t[i]==1 or a[i]==1:
        h[i]=1
ans=1
for i in range(n-1):
    if t[i]<t[i+1]:
        h[i+1]=t[i+1]
    if a[i]>a[i+1]:
        h[i]=a[i]
#print(h)
for i in range(n):
    if h[i]!=0:
        if t[i]<h[i] or a[i]<h[i]:
            ans=0
    else:
        ans*=min(a[i],t[i])%(10**9+7)
print(ans%(10**9+7))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n=int(input())
t=list(map(int,input().split()))
a=list(map(int,input().split()))
#print(t,a)
l=[0]*n
l[0]=t[0]
l[-1]=a[-1]
if t[-1]!=a[0]:
    print(0)
else:
    for i in range(n-1):
        if t[i]<t[i+1]:
            l[i+1]=t[i+1]
        if a[i]>a[i+1]:
            l[i]=a[i]
    #print(l)
    ans=1
    for i in range(1,n-1):
        if l[i]==0:
            ans*=min(t[i],a[i])%(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()))
#print(t,a)
l=[0]*n
l[0]=t[0]
l[-1]=a[-1]
if t[-1]!=a[0]:
    print(0)
else:
    for i in range(n-1):
        if t[i]<t[i+1]:
            l[i+1]=t[i+1]
        if a[i]>a[i+1]:
            l[i]=a[i]
    #print(l)
    ans=1
    for i in range(1,n-1):
        if l[i]==0:
            ans*=min(t[i],a[i])%(10**9+7)
    print(ans%(10**9+7))

Submission status: Wrong Answer

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

h = [min(t[i],a[i]) for i in range(n)]
fixed = [False] * n
fixed[0] = True; fixed[n-1] = True
# takahashi check
for i in range(1,n):
    if t[i] > t[i-1]: 
        fixed[i] = True
        if t[i] > a[i]:
            print(0)
            sys.exit(0)
# aoki check
for i in range(n-2, -1, -1):
    if a[i] > a[i+1]:
        fixed[i] = True
        if a[i] > t[i]:
            print(0)
            sys.exit(0)

# calc ans
ans = 1
for i in range(n):
    if not fixed[i]:
        ans = ans * h[i] % (10**9+7)
print(ans)

Submission status: Wrong Answer

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

if max(a) != max(t):
    print(0)
else:
    h = [min(t[i],a[i]) for i in range(n)]
    fixed = [False] * n
    fixed[0] = True; fixed[n-1] = True
    for i in range(1,n):
        if t[i] > t[i-1]: fixed[i] = True
    for i in range(n-2, 0, -1):
        if a[i] > a[i+1]: fixed[i] = True

    ans = 1
    for i in range(n):
        if not fixed[i]:
            ans = ans * h[i] % (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()))


x = [1 for _ in range(N)]
for i in range(1,N):
    if i == 0:
        x[i] = 1
    elif T[i - 1] < T[i]:
        x[i] = 1
    else:
        x[i] = T[i]

y = [1 for _ in range(N)]
for i in range(N - 2, -1, -1):
    if i == N - 1:
        y[i] == 1
    if A[i] > A[i + 1]:
        y[i] = 1
    else:
        y[i] = A[i]

ans = 1

for i in range(N):
    ans = ans * min(x[i], y[i]) % int(1e9 + 7)

print(ans)

11333
33121

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
#import sys
#input=sys.stdin.readline

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

ans=1

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

for i in range(1,N-1):
    if T[i]!=T[i-1]:
        if A[i]!=A[i+1] and T[i]!=A[i]:
            print(0)
            exit()
        else:
            continue
    else:
        if A[i]!=A[i+1]:
            continue
        else:
            ans*=min(A[i],T[i])

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
import sys
input=sys.stdin.readline

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

ans=1

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

for i in range(1,N-1):
    if T[i]!=T[i-1]:
        if A[i]!=A[i+1] and T[i]!=A[i]:
            print(0)
            exit()
        else:
            continue
    else:
        if A[i]!=A[i+1]:
            continue
        else:
            ans*=min(A[i],T[i])

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()))
T_min = [0]*N
T_min[0] = T[0]
for i in range(1,N):
  if T[i-1]<T[i]:
    T_min[i] = T[i]
A_min = [0]*N
A_min[N-1] = A[N-1]
for i in range(N-2,-1,-1):
  if A[i+1]<A[i]:
    A_min[i] = A[i]
M = [0]*N
M_min = [0]*N
for i in range(N):
  M[i] = min(T[i],A[i])
  M_min[i] = max(T_min[i],A_min[i])
ans = 1
mod = 10**9+7
for i in range(N):
  if M[i]<M_min[i]:
    ans = 0
  else:
    ans*=M[i]+1-M_min[i]
    ans%=mod
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
import functools
import operator
import sys
input=sys.stdin.readline

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

dp=[0]*N

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

for i in range(1,N-1):
    if T[i-1]!=T[i] and A[i]!=A[i+1] and A[i]!=T[i]:
        print("0")
        exit()
    if T[i-1]!=T[i]:
        dp[i]=1
    elif A[i]!=A[i+1]:
        dp[i]=1
    else:
        if dp[i]==0:
            dp[i]=min(T[i],A[i])
dp[0]=1#端っこは絶対確定モードなので
dp[N-1]=1
print(functools.reduce(operator.mul, dp)%(10**9+7))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
import functools
import operator
import sys
input=sys.stdin.readline

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

dp=[0]*N

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

for i in range(1,N-1):
    if T[i-1]!=T[i]:
        dp[i]=1
    elif A[i]!=A[i+1]:
        dp[i]=1
    else:
        if dp[i]==0:
            dp[i]=min(T[i],A[i])
dp[0]=1#端っこは絶対確定モードなので
dp[N-1]=1
print(functools.reduce(operator.mul, dp)%(10**9+7))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
import functools
import operator
import sys
input=sys.stdin.readline

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

dp=[0]*N

for i in range(1,N-1):
    if T[i-1]!=T[i]:
        dp[i]=1
    elif A[i]!=A[i+1]:
        dp[i]=1
    else:
        if dp[i]==0:
            dp[i]=min(T[i],A[i])
dp[0]=1
dp[N-1]=1
print(functools.reduce(operator.mul, dp)%(10**9+7))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
import functools
import operator

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

dp=[0]*N

for i in range(1,N-1):
    if T[i-1]!=T[i]:
        dp[i]=1
    elif A[i]!=A[i+1]:
        dp[i]=1
    else:
        if dp[i]==0:
            dp[i]=min(T[i],A[i])
dp[0]=1
dp[N-1]=1
print(functools.reduce(operator.mul, dp)%(10**9+7))

Submission status: Wrong Answer

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

H = [[1,10 ** 9] for _ in range(N)]

for i in range(N):
  if i == 0:
    hmin = T[i]
    hmax = T[i]
  elif T[i-1] < T[i]:
    hmin = T[i]
    hmax = T[i]
  else:
    hmin = 1
  H[i][0] = hmin
  H[i][1] = hmax
  #print(H)
for i in range(N-1,-1,-1):
  if i == N-1:
    hmin = A[i]
    hmax = A[i]
  elif A[i] > A[i+1]:
    hmax = A[i]
    hmin = A[i]
  else:
    hmin = 1
  H[i][0] = max(hmin,H[i][0])
  H[i][1] = min(hmax,H[i][1])
  #print(H)

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

Submission status: Wrong Answer

Solution code:
n = int(input())

t_list = [ int(v) for v in input().split() ]
a_list = [ int(v) for v in input().split() ]
a_list_reverse = list(reversed(a_list))

t_confirm_list = [t_list[0]] + [None for i in range(n-1)]
a_confirm_list = [a_list_reverse[0]] + [None for i in range(n-1)]


for i in range(n-1):
    if t_list[i]  < t_list[i+1]:
        t_confirm_list[i+1] = t_list[i+1]
    if a_list_reverse[i]  < a_list_reverse[i+1]:
        a_confirm_list[i+1] = a_list_reverse[i+1]

a_confirm_list.reverse()


mod = 10**9+7
ans = 1
for i in range(n):
    if a_confirm_list[i] != None and t_confirm_list[i] != None:
        if a_confirm_list[i] != t_confirm_list[i]:
            ans = 0
            break
    elif a_confirm_list[i] == None and t_confirm_list[i] == None:
        ans *= min(a_list[i],t_list[i])
        ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#CODEFESTIVAL2016 qualC C

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
t = LI()
a = LI()
x = [0]*n
y = [0]*n
for i in range(1,n):
    if t[i] > t[i-1]:
        x[i] = 1
for i in range(1,n)[::-1]:
    if a[i] < a[i-1]:
        y[i-1] = 1
ans = 1
for i in range(1,n-1):
    if x[i] == 1 and y[i] == 1:
        if t[i] != a[i]:
            print(0)
            quit()
        continue
    elif x[i] == 1:
        if a[i] >= t[i]:
            continue
        else:
            print(0)
            quit()
    elif y[i] == 1:
        if a[i] <= t[i]:
            continue
        else:
            print(0)
            quit()
    ans *= min(t[i],a[i])
    ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#CODEFESTIVAL2016 qualC C

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
t = LI()
a = LI()
if t[-1] != a[0]:
    print(0)
    quit()
x = [0]*n
y = [0]*n
for i in range(1,n):
    if t[i] > t[i-1]:
        x[i] = 1
for i in range(1,n)[::-1]:
    if a[i] < a[i-1]:
        y[i-1] = 1

ans = 1
for i in range(1,n-1):
    if x[i] == 1 or y[i] == 1:
        continue
    ans *= min(t[i],a[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()))

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

max_h = [1] * N

#max_h[0] = T[0]
#max_h[-1] = A[-1]

h = [None] * N
#Tから見ていく
for i in range(1,N-1):

        ht = ha = None
        if T[i] > T[i-1]:
            ht = T[i]
        if A[i] > A[i+1]:
            ha = A[i]

        if ht != None and ha != None:
            if ht == ha:
                h[i] = ht
            else:
                print(0) # 矛盾するので終わり
                exit()
        elif ht != None:
            h[i] = ht
        elif ha != None:
            h[i] = ha
        elif ht == None and ha == None:
            max_h[i] = min(T[i], A[i])

ans = 1
for i in range(N):
    
    ans *= max_h[i]

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()))
Tr,Ar=[0]*N,[0]*N

ct,ca=0,0
for i in range(N):
    if T[i] > ct:
        ct=T[i]
        Tr[i]=[ct,ct]
    else:
        Tr[i]=[1,ct]

    if A[N-1-i] > ca:
        ca=A[N-1-i]
        Ar[N-1-i]=[ca,ca]
    else:
        Ar[N-1-i]=[1,ca]

MOD=1000000007
ans=1
for tr,ar in zip(Tr,Ar):
    l = max(tr[0],ar[0])
    r = min(tr[1],ar[1])
    ans = (ans*(r-l+1))%MOD
print(ans)

Submission status: Wrong Answer

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


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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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


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


if __name__ == "__main__":
    main()