from collections import deque import sys input = sys.stdin.readline class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def size(self, x): return -self.parents[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) def members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] def isCheck(a,b,c): if a != b and b != c and c != a: if max(a,b,c) == b or min(a,b,c) == b: return True return False def main(): N, M = map(int, input().split()) A = list(map(int, input().split())) edge = [[] for _ in range(N)] uni = UnionFind(N) for _ in range(M): u, v = map(int, input().split()) u -= 1 v -= 1 edge[u].append(v) edge[v].append(u) uni.union(u,v) Q = deque([]) for i in uni.roots(): Q.append((i,-1)) flag = False visited = [False] * N while Q: s, p = Q.popleft() visited[s] = True for n in edge[s]: if p == n: continue for m in edge[s]: if n == m: continue if isCheck(A[m], A[s], A[n]): flag = True break if not visited[n]: Q.append((n,s)) if flag: print('YES') else: print('NO') if __name__ == "__main__": main()