def examA():

    S = SI()

    ans = "No"

    flag = False

    for s in S:

        if flag:

            if s=="F":

                ans = "Yes"

                break

        else:

            if s=="C":

                flag = True

    print(ans)

    return



def examB():

    K, T = LI()

    A = LI(); A.sort()

    ans = max(0,A[-1]*2-sum(A)-1)

    print(ans)

    return



def examC(mod):

    N = I()

    T = LI(); A = LI()

    H = [0]*N

    ans = 1

    if T[-1] != A[0]:

        ans = 0

    H[0] = T[0]; H[N-1] = A[N-1]

    for i in range(N-1):

        if T[i+1]>T[i]:

            if H[i+1]==0:

                H[i+1] = T[i+1]

            elif H[i+1]!=T[i+1]:

                ans = 0

        else:

            if H[i+1]==0:

                H[i+1] = -T[i+1]

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

        if A[i-1]>A[i]:

            if H[i-1]==0:

                H[i-1] = A[i-1]

            elif H[i-1]<0:

                if -H[i-1]<A[i-1]:

                    ans = 0

                else:

                    H[i-1] = A[i-1]

            elif H[i-1]!=A[i-1]:

                ans = 0

#    print(H)

    flag = False; cur = 0

    for i in H:

        if flag:

            if i<0:

                cur +=1

            else:

                ans *=(min(now,i)**cur)%mod

                ans %=mod

                flag = False

                cur = 0; now = i

        else:

            if i<0:

                cur = 1

                flag = True

            else:

                now = i

    print((ans%mod))

    return



import sys,copy,bisect,itertools,heapq,math

from heapq import heappop,heappush,heapify

from collections import Counter,defaultdict,deque

def I(): return int(sys.stdin.readline())

def LI(): return list(map(int,sys.stdin.readline().split()))

def LSI(): return list(map(str,sys.stdin.readline().split()))

def LS(): return sys.stdin.readline().split()

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

mod = 10**9 + 7

inf = float('inf')



if __name__ == '__main__':

    examC(mod)

def examA():

    N = I()

    T = LI()

    A = LI()

    S = [-1]*N

    S[0] = T[0]; S[-1]=A[-1]

    ans = 1

    for i in range(1,N):

        if T[i]==T[i-1]:

            continue

        S[i] = T[i]

    flag = -1

    for i in range(N):

        if S[i]==A[i]:

            flag = i

            break

    if not (A[flag]==A[0] and A[flag]==T[-1]):

        print((0))

        return

    for i in range(N-1)[::-1]:

        if A[i]==A[i+1]:

            continue

        if S[i]==-1:

            S[i] = A[i]

        else:

            S[i] = max(S[i],A[i])

    #print(S)

    P = []

    pindex = []

    for i in range(N):

        if S[i]==-1:

            continue

        P.append(S[i])

        pindex.append(i)

    flag_U = True

    for i in range(len(P)-1):

        if P[i+1]>P[i]:

            if not flag_U:

                print((0))

                return

        elif P[i+1]<P[i]:

            flag_U = False

    ans = 1

    p = -1

    for i in range(N):

        if S[i]==-1:

            cur = min(P[p + 1], P[p])

            ans *= cur

            ans %= mod

        else:

            p += 1



    print(ans)

    return



def examB():

    ans = 0

    print(ans)

    return



def examC():

    ans = 0

    print(ans)

    return



def examD():

    ans = 0

    print(ans)

    return



import sys,copy,bisect,itertools,heapq,math,random

from heapq import heappop,heappush,heapify

from collections import Counter,defaultdict,deque

def I(): return int(sys.stdin.readline())

def LI(): return list(map(int,sys.stdin.readline().split()))

def LSI(): return list(map(str,sys.stdin.readline().split()))

def LS(): return sys.stdin.readline().split()

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

global mod,mod2,inf,alphabet,_ep

mod = 10**9 + 7

mod2 = 998244353

inf = 10**18

_ep = 10**(-12)

alphabet = [chr(ord('a') + i) for i in range(26)]



if __name__ == '__main__':

    examA()



"""



"""
def examA():
    S = SI()
    ans = "No"
    flag = False
    for s in S:
        if flag:
            if s == "F":
                ans = "Yes"
                break
        else:
            if s == "C":
                flag = True
    print(ans)
    return


def examB():
    K, T = LI()
    A = LI()
    A.sort()
    ans = max(0, A[-1] * 2 - sum(A) - 1)
    print(ans)
    return


def examC(mod):
    N = I()
    T = LI()
    A = LI()
    H = [0] * N
    ans = 1
    if T[-1] != A[0]:
        ans = 0
    H[0] = T[0]
    H[N - 1] = A[N - 1]
    for i in range(N - 1):
        if T[i + 1] > T[i]:
            if H[i + 1] == 0:
                H[i + 1] = T[i + 1]
            elif H[i + 1] != T[i + 1]:
                ans = 0
        else:
            if H[i + 1] == 0:
                H[i + 1] = -T[i + 1]
    for i in range(N - 1, 0, -1):
        if A[i - 1] > A[i]:
            if H[i - 1] == 0:
                H[i - 1] = A[i - 1]
            elif H[i - 1] < 0:
                if -H[i - 1] < A[i - 1]:
                    ans = 0
                else:
                    H[i - 1] = A[i - 1]
            elif H[i - 1] != A[i - 1]:
                ans = 0
    #    print(H)
    flag = False
    cur = 0
    for i in H:
        if flag:
            if i < 0:
                cur += 1
            else:
                ans *= (min(now, i) ** cur) % mod
                ans %= mod
                flag = False
                cur = 0
                now = i
        else:
            if i < 0:
                cur = 1
                flag = True
            else:
                now = i
    print((ans % mod))
    return


import sys, copy, bisect, itertools, heapq, math
from heapq import heappop, heappush, heapify
from collections import Counter, defaultdict, deque


def I():
    return int(sys.stdin.readline())


def LI():
    return list(map(int, sys.stdin.readline().split()))


def LSI():
    return list(map(str, sys.stdin.readline().split()))


def LS():
    return sys.stdin.readline().split()


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


mod = 10**9 + 7
inf = float("inf")
if __name__ == "__main__":
    examC(mod)

def examA():
    N = I()
    T = LI()
    A = LI()
    S = [-1] * N
    S[0] = T[0]
    S[-1] = A[-1]
    ans = 1
    for i in range(1, N):
        if T[i] == T[i - 1]:
            continue
        S[i] = T[i]
    flag = -1
    for i in range(N):
        if S[i] == A[i]:
            flag = i
            break
    if not (A[flag] == A[0] and A[flag] == T[-1]):
        print((0))
        return
    for i in range(N - 1)[::-1]:
        if A[i] == A[i + 1]:
            continue
        if S[i] == -1:
            S[i] = A[i]
        else:
            S[i] = max(S[i], A[i])
    # print(S)
    P = []
    pindex = []
    for i in range(N):
        if S[i] == -1:
            continue
        P.append(S[i])
        pindex.append(i)
    flag_U = True
    for i in range(len(P) - 1):
        if P[i + 1] > P[i]:
            if not flag_U:
                print((0))
                return
        elif P[i + 1] < P[i]:
            flag_U = False
    ans = 1
    p = -1
    for i in range(N):
        if S[i] == -1:
            cur = min(P[p + 1], P[p])
            ans *= cur
            ans %= mod
        else:
            p += 1
    print(ans)
    return


def examB():
    ans = 0
    print(ans)
    return


def examC():
    ans = 0
    print(ans)
    return


def examD():
    ans = 0
    print(ans)
    return


import sys, copy, bisect, itertools, heapq, math, random
from heapq import heappop, heappush, heapify
from collections import Counter, defaultdict, deque


def I():
    return int(sys.stdin.readline())


def LI():
    return list(map(int, sys.stdin.readline().split()))


def LSI():
    return list(map(str, sys.stdin.readline().split()))


def LS():
    return sys.stdin.readline().split()


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


global mod, mod2, inf, alphabet, _ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10 ** (-12)
alphabet = [chr(ord("a") + i) for i in range(26)]
if __name__ == "__main__":
    examA()
"""
"""

-    S = SI() -    ans = "No" -    flag = False -    for s in S: -        if flag: -            if s == "F": -                ans = "Yes" -                break +    N = I() +    T = LI() +    A = LI() +    S = [-1] * N +    S[0] = T[0] +    S[-1] = A[-1] +    ans = 1 +    for i in range(1, N): +        if T[i] == T[i - 1]: +            continue +        S[i] = T[i] +    flag = -1 +    for i in range(N): +        if S[i] == A[i]: +            flag = i +            break +    if not (A[flag] == A[0] and A[flag] == T[-1]): +        print((0)) +        return +    for i in range(N - 1)[::-1]: +        if A[i] == A[i + 1]: +            continue +        if S[i] == -1: +            S[i] = A[i] -            if s == "C": -                flag = True +            S[i] = max(S[i], A[i]) +    # print(S) +    P = [] +    pindex = [] +    for i in range(N): +        if S[i] == -1: +            continue +        P.append(S[i]) +        pindex.append(i) +    flag_U = True +    for i in range(len(P) - 1): +        if P[i + 1] > P[i]: +            if not flag_U: +                print((0)) +                return +        elif P[i + 1] < P[i]: +            flag_U = False +    ans = 1 +    p = -1 +    for i in range(N): +        if S[i] == -1: +            cur = min(P[p + 1], P[p]) +            ans *= cur +            ans %= mod +        else: +            p += 1 -    K, T = LI() -    A = LI() -    A.sort() -    ans = max(0, A[-1] * 2 - sum(A) - 1) +    ans = 0 -def examC(mod): -    N = I() -    T = LI() -    A = LI() -    H = [0] * N -    ans = 1 -    if T[-1] != A[0]: -        ans = 0 -    H[0] = T[0] -    H[N - 1] = A[N - 1] -    for i in range(N - 1): -        if T[i + 1] > T[i]: -            if H[i + 1] == 0: -                H[i + 1] = T[i + 1] -            elif H[i + 1] != T[i + 1]: -                ans = 0 -        else: -            if H[i + 1] == 0: -                H[i + 1] = -T[i + 1] -    for i in range(N - 1, 0, -1): -        if A[i - 1] > A[i]: -            if H[i - 1] == 0: -                H[i - 1] = A[i - 1] -            elif H[i - 1] < 0: -                if -H[i - 1] < A[i - 1]: -                    ans = 0 -                else: -                    H[i - 1] = A[i - 1] -            elif H[i - 1] != A[i - 1]: -                ans = 0 -    #    print(H) -    flag = False -    cur = 0 -    for i in H: -        if flag: -            if i < 0: -                cur += 1 -            else: -                ans *= (min(now, i) ** cur) % mod -                ans %= mod -                flag = False -                cur = 0 -                now = i -        else: -            if i < 0: -                cur = 1 -                flag = True -            else: -                now = i -    print((ans % mod)) +def examC(): +    ans = 0 +    print(ans) -import sys, copy, bisect, itertools, heapq, math +def examD(): +    ans = 0 +    print(ans) +    return + + +import sys, copy, bisect, itertools, heapq, math, random +global mod, mod2, inf, alphabet, _ep -inf = float("inf") +mod2 = 998244353 +inf = 10**18 +_ep = 10 ** (-12) +alphabet = [chr(ord("a") + i) for i in range(26)] -    examC(mod) +    examA() +""" +"""