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:
# -*- coding: utf-8 -*-

import sys

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

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

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

for i in range(N):
    # 両者の確定箇所について矛盾がないか確認
    if Hs1[i]!=0 and Hs2[i]!=0 and Hs1[i]!=Hs2[i]:
        print(0)
        exit()

ans=1
for i in range(N):
    if not fixed[i]:
        # 未確定の場所について、低い方の数だけ可能性がある
        ans=(ans*min(T[i], A[i]))%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
h = [0 for _ in range(n)]
last = 0
for i in range(n):
    if t[i] != last:
        h[i] = t[i]
        last = t[i]
last = 0
for i in range(n):
    j = n-i-1
    if a[j] != last:
        if h[j] > 0 and h[j] != a[j]:
            ans = 0
            break
        h[j] = a[j]
        last = a[j]
for i in range(n):
    if h[i] == 0:
        ans = ans * min(t[i], a[i]) % mod
print(ans)

Submission status: Wrong Answer

Solution code:
def powmod(x, n):
  ret = 1
  while n > 0:
    if n & 1:
      ret *= x; ret %= mod; n -= 1
    else:
      x *= x; x %= mod; n >>= 1
  return ret

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

mod = 10**9 + 7

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

print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
N=int(input())
a=list(map(int,input().split()))
a.append(a[len(a)-1])
A=[0]
A=A+a
#print(A)
b=list(map(int,input().split()))
b.append(0)
b=b[::-1]
b.append(b[len(b)-1])
B=b[::-1]
#print(B)

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

L=[]
x=A[0]
y=B[0]
for i in range(1,N+1):
    if x>A[i]:
        print(0)
        exit()
    else:
        x=A[i]
    if y<B[i]:
        print(0)
        exit()
    else:
        y=B[i]
    if A[i]>A[i-1]:
        L.append(1)
    elif B[i]>B[i+1]:
        L.append(1)
    else:
        L.append(min(A[i],B[i]))
    

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

Submission status: Wrong Answer

Solution code:
N=int(input())
a=list(map(int,input().split()))
a.append(a[len(a)-1])
A=[0]
A=A+a
#print(A)
b=list(map(int,input().split()))
b.append(0)
b=b[::-1]
b.append(b[len(b)-1])
B=b[::-1]
#print(B)

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

L=[]
for i in range(1,N+1):
    if A[i]>A[i-1]:
        L.append(1)
    elif B[i]>B[i+1]:
        L.append(1)
    else:
        L.append(min(A[i],B[i]))

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

Submission status: Wrong Answer

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


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

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

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

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

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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


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

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

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

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

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N=int(input())

T=list(map(int,input().split()))
A=list(map(int,input().split()))
for i in range(1,N):
    if T[i]<T[i-1]:
        print(0)
        exit()
    if A[N-1-i]<A[N-i]:
        print(0)
        exit()

if T[N-1]!=A[0]:
    print(0)
else:
    L1=[]
    for i in range(N):
        L1.append(min(T[i],A[i]))
        
    L2=[1]
    Num=L1[0]
    for i in range(1,N):
        if L1[i]==Num:
            L2.append(Num)
        elif L1[i]>Num:
            L2.append(1)
            Num=L1[i]
        else:
            L2.append(L1[i])
    L3=[0 for i in range(N)]
    Num=L1[N-1]
    L3[N-1]=1
    for i in range(1,N):
        if L1[N-1-i]==Num:
            L3[N-1-i]=Num
        elif L1[i]>Num:
            L3[N-1-i]=1
            Num=L1[N-1-i]
        else:
            L3[N-1-i]=L1[N-1-i]
    #print(L2)
    #print(L3)
    L4=[]
    for i in range(N):
        L4.append(min(L2[i],L3[i]))
    #print(L4)
    
    ans=1
    for i in range(N):
        ans*=L4[i]
        ans%=(10**9+7)
    print(ans)

Submission status: Wrong Answer

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

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

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

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

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

	print(ans)

Submission status: Wrong Answer

Solution code:
def inpl(): return [int(i) for i in input().split()]
mod = 10**9+7
N = int(input())
T = inpl()
A = inpl()
summit =  T[-1]
S = sum([max(i, j) - summit for i, j in zip(A, T)])
ans, pre = 1, 0
if A[0] != summit:
    ans = 0
if S:
    ans = 0
k = sum([min(i, j) == summit for i, j in zip(A, T)]) 
for i in T:
    if i == summit:
        break
    if i == pre:
        ans = ans*pre %mod
    else:
        pre = i
pre = 0
for i in reversed(A):
    if i == summit:
        break
    if i == pre:
        ans = ans*pre %mod
    else:
        pre = i
print(ans*summit**max(k-2, 0)%mod)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N=int(input())
T=[int(i) for i in input().split()]
A=[int(i) for i in input().split()]
H=[0 for i in range(N)]
P=10**9+7
for i in range(N):
    if i==0:
        H[i]=T[i]
    else:
        if T[i-1]<T[i]:
            H[i]=T[i]
for i in range(N)[::-1]:
    if i==N-1:
        if H[i]==0:
            H[i]=A[i]
        else:
            if H[i]!=A[i]:
                print(0)
                exit()
    else:
        if A[i+1]<A[i]:
            if H[i]==0:
                H[i]=A[i]
            else:
                if H[i]!=A[i]:
                    print(0)
                    exit()
C=[0 for i in range(N)]
for i in range(N):
    if H[i]!=0:
        C[i]=1
sup=[0 for i in range(N)]
for i in range(N):
    if C[i]==0:
        sup[i]=min(A[i],T[i])
ans=1
for i in range(N):
    if C[i]==1:
        continue
    ans=ans*sup[i]
    ans%=P
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 max(t) != max(a):
    print(0)
    exit()

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


m = [max(t) for _ in range(n)]
mt = t[0]
m[0] = 1
ma = a[n - 1]
m[n - 1] = 1
for i in range(1, n):
    if t[i] != t[i - 1]:
        mt = t[i]
        m[i] = 1
    m[i] = min(mt, m[i])
    if a[n - i] != a[n - i - 1]:
        ma = a[n - i - 1]
        m[n - i - 1] = 1
    m[n - i - 1] = min(ma, m[n - i - 1])

ans = m[0]
for i in range(1, n):
    ans *= m[i]
    ans %= MOD

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
*l,=map(int,input().split())
*m,=map(int,input().split())
o=[[0,0] for _ in range(n)]
mod=10**9+7

if l[-1]!=m[0]:
  print(0)
for i in range(n):
  if not i:
    o[0][0]=1
    o[-1][1]=1
  else:
    if l[i]>l[i-1]:
      o[i][0]=1
    else:
      o[i][0]=l[i]
    if m[n-i-1]>m[n-i]:
      o[n-i-1][1]=1
    else:
      o[n-i-1][1]=m[n-i]

v=1
for i in o:
  v*=min(i)
  v%=mod
v%=mod
print(v)

Submission status: Wrong Answer

Solution code:
n=int(input())
*l,=map(int,input().split())
*m,=map(int,input().split())
o=[[0,0] for _ in range(n)]

if l[-1]!=m[0]:
  print(0)
for i in range(n):
  if not i:
    o[0][0]=1
    o[-1][1]=1
  else:
    if l[i]>l[i-1]:
      o[i][0]=1
    else:
      o[i][0]=l[i]
    if m[n-i-1]>m[n-i]:
      o[n-i-1][1]=1
    else:
      o[n-i-1][1]=m[n-i]

v=1
for i in o:
  v*=min(i)
  v%=(10**9+7)

print(v)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
T = list(map(int, input().split()))
A = list(map(int, input().split()))
h = [[] for i in range(N)]
for i in range(N):
    if i == 0:
        h[i].append(T[i])
    elif i == N - 1:
        h[i].append(A[i])
    else:
        if T[i] > T[i - 1]:
            h[i].append(T[i])
        elif A[i] > A[i + 1]:
            h[i].append(A[i])
        else:
            h[i] = [j for j in range(1, min([A[i], T[i]]) + 1)]
cnt = 1
for i in range(N):
    cnt *= (len(h[i]) % 1000000007)
if max(A) != max(T):
    cnt = 0
print(cnt % 1000000007)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

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

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

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

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

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

print ans

Submission status: Wrong Answer

Solution code:
n = int(input())

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

combs = [0 for i in range(n)]

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


	else :
		combs[i] = t
t = 0
for i in range(n-1,-1,-1):


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

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

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

print ans if a[n-1] == b[0] else 0

Submission status: Wrong Answer

Solution code:
n = int(input())

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

combs = [0 for i in range(n)]

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


	else :
		combs[i] = t
t = 0
for i in range(n-1,-1,-1):


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

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

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

print ans

Submission status: Wrong Answer

Solution code:
n = int(input())

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

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

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

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

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

print ans

Submission status: Wrong Answer

Solution code:
import sys

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

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

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

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

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

    print count




if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

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

    if  left > right:print(0)
    if  left < right:
        res *= l1**(right-left-1)

        print(res%1000000007)

Submission status: Wrong Answer

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

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

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

    print(res%1000000007)

Submission status: Wrong Answer

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

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

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

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

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

B = [10**9] * n

h = 0

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

h = 0

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

ans = 1

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

print(ans)

Submission status: Wrong Answer

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

B = [10**9] * n

h = 0

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

h = 0

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

ans = 1

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

print(ans)

Submission status: Wrong Answer

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

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

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

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

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

    if i == 1 or i == N:
        if T[1] != A[N] or T[N] != A[1]:
            ans *= 0
        else:
            ans *= 1
        continue
    
    ans = (ans * min(T[i], A[i])) % mod

print(ans % mod)

Submission status: Wrong Answer

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

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

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

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

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

    if i == 1 or i == N:
        ans *= 1
        continue

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

print(ans % mod)

Submission status: Wrong Answer

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

T.insert(0, T[0])
T.append(T[-1])
A.insert(0, A[0])
A.append(A[-1])

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

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

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

    if i == 1 or i == N:
        ans *= 1
        continue

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

print(ans % mod)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input().split()
s.insert(0,"0")
s.append("0")
ss=input().split()
ss.insert(0,"0")
s.append("0")
res=1
for i in range(1,n):
 if int(s[i-1])!=int(s[i]):
  if int(ss[i+1])!=int(ss[i]) and s[i]!=ss[i]:
   res*=0
  if int(ss[i])<int(s[i]):
   res*=0
  continue
 elif int(ss[i])!=int(ss[i+1]):
  if int(s[i])<int(ss[i]):
   res*=0
 else:
  res*=min(int(s[i]),int(ss[i]))
print(res%1000000007)

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()]
x,flag,ans,mod = [[t[0],t[0]]]+[[1,1] for _ in range(n-1)],True,1,10**9+7
for i in range(1,n):
	if t[i-1]<t[i]: x[i] = [t[i],t[i]]
	else: x[i][0] = t[i]
if x[-1][0]<a[-1]: flag = False
else:
	x[-1] = [a[-1],a[-1]]
	for i in range(n-2,0,-1):
		if a[i]>a[i+1]:
			if x[i][0]>=a[i]: x[i]=[a[i],a[i]]
			else: flag = False
		else:
			if x[i][0]>=a[i]: x[i][0]=a[i]
			else: pass
if flag:
	for i,j in x: ans = (ans*(i-j+1))%mod
else: ans = 0
print(ans)

Submission status: Wrong Answer

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

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

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

print(x)

Submission status: Wrong Answer

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

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

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

print(x)

Submission status: Wrong Answer

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

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

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

print(x)