We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}.
You can perform the following operation at most once: choose integers i and j (1 \leq i < j \leq N), and swap p_i and p_j. Note that you can also choose not to perform it.
Print YES if you can sort p in ascending order in this way, and NO otherwise.

-----Constraints-----
 - All values in input are integers.
 - 2 \leq N \leq 50
 - p is a permutation of {1,\ 2,\ ...,\ N}.

-----Input-----
Input is given from Standard Input in the following format:
N
p_1 p_2 ... p_N

-----Output-----
Print YES if you can sort p in ascending order in the way stated in the problem statement, and NO otherwise.

-----Sample Input-----
5
5 2 3 4 1

-----Sample Output-----
YES

You can sort p in ascending order by swapping p_1 and p_5.

Difficulty: introductory

Starter code:

Solutions:
n = int(input())
p = [int(e) for e in input().split()]
z = 0
for i in range(n):
  if p[i] != i + 1:
    z += 1
if z <= 2:
  print('YES')
else:
  print('NO')

N = int(input())
P = list(map(int, input().split()))
A = list(range(1, N+1))
count = 0
for i in range(N):
  if P[i] != A[i]:
    count += 1
if count <= 2:
  print("YES")
else:
  print("NO")

n = int(input())
s = list(map(int,input().split()))
t = s.copy()
t.sort()
cnt = 0
for i in range(n):
    if s[i] != t[i]:
        cnt += 1
if cnt<3:
    print("YES")
else:
    print("NO")


import copy
N = int(input())
p = list(map(int, input().split()))
pcopy = copy.deepcopy(p)
ans = sorted(p)

is_ascend = False
if p == ans:
    is_ascend = True

for i in range(N):
    for j in range(i+1,N):
        p = copy.copy(pcopy)
        p[i], p[j] = p[j], p[i]
        if p == ans:
            is_ascend = True

if is_ascend:
    print("YES")
else:
    print("NO")


n = int(input())
p = list(map(int, input().split()))

cnt = 0
for i in range(n):
  if p[i] == i+1:
    continue
  p[p[i]-1] = p[i]
  p[i] = i+1
  cnt += 1
print('YES' if cnt <= 1 else 'NO')

N = int(input())
p = list(map(int,input().split()))

if p == sorted(p):
    print('YES')
else:
    ret = 'NO'
    is_sorted = False
    # 全通りの組み合わせをみて、昇順かどうか調べる
    for i in range(N-1):
        for j in range(i+1, N):
            # p[i]とp[j]より小さければ抜ける
            if p[i] < p[j]:
                continue
            else:
                k = p.copy()
                k[i], k[j] = k[j], k[i]

#             for l in range(N-1):
#                 if k[l+1] < k[l]:
#                     is_sorted = False
#                     break

            if k == sorted(p):
                ret = 'YES'
                is_sorted = True
                break
        if is_sorted:
            break

    print(ret)

n = int(input())
p = list(map(int,input().split()))
ans = [i for i in range(1,n+1)]
cnt = 0
 
for i in range(n):
  if p[i] != ans[i]:
    cnt += 1
  if cnt > 2:
    print('NO')
    return
    
print('YES')

n = int(input())
a = list(map(int, input().split()))
b = []
for i in range(n):
  b.append(a[i])
b.sort()
c = 0
for i in range(n):
  if a[i] != b[i]:
    c = c + 1
if c <= 2:
  print("YES")
else:
  print("NO")

import copy
n = int(input())
li = list(map(int,input().split()))
li1 = copy.copy(li)
li1.sort()
cnt = 0
for i in range(n):
    if li[i] != li1[i]:
        cnt += 1

if cnt <= 2:
    print('YES')
else:
    print('NO')

n = int(input())
lst = list(map(int,input().split()))
c = 0
for i in range(n):
  if (lst[i] != i + 1):
    c = c + 1
if (c <= 2):
  print("YES")
else:
  print("NO")

N=int(input())
p=list(map(int,input().split()))
cnt=0
for i in range(N):
    if i+1!=p[i]:
        cnt+=1
if cnt>2:
    print("NO")
else:
    print("YES")


import numpy as np
 
N = int(input())
p = list(map(int, input().split()))
 
q = sorted(p)
p, q = np.array(p), np.array(q)

if np.sum(p != q) <= 2: print('YES')
else: print('NO')

N = int(input())
p = list(map(int, input().split()))
check = 0
for i in range(N):
  if i+1 != p[i]:
    check += 1
print("YES" if check <= 2 else "NO")

n = int(input())
p = list(map(int, input().split()))

cnt = 0
for i in range(n):
    if(p[i] != i + 1):
        cnt = cnt + 1
if(cnt <= 2):
    print("YES")
else:
    print("NO")        

n = int(input())
s = list(map(int,input().split()))
t = s.copy()
t.sort()
cnt = 0
for i in range(n):
    if s[i] != t[i]:
        cnt += 1
if cnt<3:
    print("YES")
else:
    print("NO")

N=int(input())
P=list(map(int,input().split()))
cnt=0
for n in range(N):
  if (n+1)!=P[n]:
    cnt+=1

if cnt<=2:
  print("YES")
else:
  print("NO")

N,*P=map(int,open(0).read().split())
S='YES' if sum([P[i]==i+1 for i in range(N)])>=N-2 else 'NO'
print(S)

N = int(input())
P = list(map(int, input().split()))

sp = sorted(P)
cnt = 0
for a, b in zip(sp, P):
    if a != b:
        cnt += 1
if cnt == 2 or cnt == 0:
    print("YES")
else:
    print("NO")


N = int(input())
p = list(map(int, input().split()))
q = list(range(1, N+1))

ans = 0
for i in range(N):
  if p[i] != q[i]: ans += 1
    
if ans <= 2: print('YES')
else: print('NO')

input();a=[*map(int,input().split())];print('YNEOS'[sum(i!=j for i,j in zip(a,sorted(a)))>2::2])

n = int(input())
p = list(map(int,input().split()))
count = 0
for i in range(n):
    if p[i] != i + 1:
        count += 1
if count == 0 or count == 2:
    print('YES')
else:
    print('NO')

a = input()
N = int(a)
p = list(map(int,input().split()))

count = 0
for i in range(N):
    if p[i] != i+1:
        count = count + 1
    if count == 3:
        break
if count == 3:
    print('NO')
else:
    print('YES')


n = int(input())
a = list(map(int, input().split(' ')))

def swap(a, b):
    return b, a

ok = 0
f = 1
for i in range(n):
    if a[i] - 1 != i:
        f = 0
if f:
    ok = 1
for i in range(n):
    for j in range(i + 1, n):
        a[i], a[j] = swap(a[i], a[j])
        f = 1
        for k in range(n):
            if a[k] - 1 != k:
                f = 0
        if f:
            ok = 1
        a[i], a[j] = swap(a[i], a[j])
print('YES' if ok else 'NO')

n=int(input())
a=list(map(int, input().split()))
count=0
for i in range(n-1,-1,-1):
    for j in range(i+1):
        if(a[j]>a[i]):
            t=a[j]
            a[j]=a[i]
            a[i]=t
            count+=1
if(count>1):
    print("NO")
else:
    print("YES")


n = int(input())
p = list(map(int, input().split()))
a = sorted(p)
b = 0
for i in range(n):
    if p[i] != a[i]:
        b += 1
if b == 2 or b == 0:
    print('YES')
else:
    print('NO')

N=int(input())
P=list(map(int,input().split()))
M=[i for i in range(1,N+1)]
ans=0

for i,j in zip(P,M):
    if i!=j:
        ans+=1

if ans==2 or ans==0:
    print("YES")
else:
    print("NO")

n = int(input())
list_p = [int(i) for i in input().split()]
count = 0
for i in range(0, n):
    if i + 1 != list_p[i]: count += 1
    if count > 2:
        print("NO")
        return
print("YES")

N = int(input())
lsp = list(map(int,input().split()))
ii = 0
for i in range(N):
    if i+1 == lsp[i]:
        continue
    ii += 1
if ii >2:
    print('NO')
else:
    print('YES')

n = int(input())

p = list(map(int,input().split()))

ideal = list(range(1,n+1))

count = 0
for x in range(n):
  if p[x] != ideal[x]:
    count += 1

if count == 0 or count == 2:
  print("YES")
  
else:
  print("NO")


import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 6)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")
al = "abcdefghijklmnopqrstuvwxyz"
AL = al.upper()

n = k()
p = l()

for i in range(n):
    if p[i] != i+1:
        cnt += 1

print("YES" if cnt <= 2  else "NO")

def inpl(): return list(map(int, input().split()))

N = int(input())
P = inpl()
Q = sorted(P)

for i in range(N):
    for j in range(i, N):
        R = [p for p in P]
        R[i], R[j] = R[j], R[i]
        for k in range(N):
            if R[k] != Q[k]:
                break
        else:
            print("YES")
            return
else:
    print("NO")

N = int(input())
p = list(map(int,input().split()))

s_p = sorted(p)

cnt = 0
for i in range(N):
    if p[i] != s_p[i]:
        cnt += 1

if cnt > 2:
    print("NO")
else:
    print("YES")

n = int(input())
a = list(map(int, input().split(' ')))

def swap(a, b):
    return b, a

ok = 0
f = 1
for i in range(n):
    if a[i] - 1 != i:
        f = 0
if f:
    ok = 1
for i in range(n):
    for j in range(i + 1, n):
        a[i], a[j] = swap(a[i], a[j])
        f = 1
        for k in range(n):
            if a[k] - 1 != k:
                f = 0
        if f:
            ok = 1
        a[i], a[j] = swap(a[i], a[j])
print('YES' if ok else 'NO')

n = int(input())
p = list(map(int,input().split()))
ans = [i for i in range(1,n+1)]
cnt = 0

for i in range(n):
  if p[i] != ans[i]:
    cnt += 1
  if cnt > 2:
    print('NO')
    return
    
print('YES')


def resolve():
    n = int(input())
    p = map(int,input().split())
    cnt = 0
    for i,j in enumerate(p):
        if (i+1) != j:
            cnt += 1
    print('YES' if cnt < 3 else 'NO')
resolve()

n = int(input())
p = list(map(int, input().split()))

p_sorted = sorted(p)

cnt = 0
for i in range(n):
    if p[i] != p_sorted[i]:
        cnt += 1

if cnt <= 2:
    print("YES")
else:
    print("NO")


N = int(input())
p = list(map(int, input().split()))
ans = 0
for i in range(N):
  if p[i] != i+1:
    ans += 1

if ans <= 2:
  print("YES")
else:
  print("NO")


# import math
# import statistics
a=int(input())
#b,c=int(input()),int(input())
# c=[]
# for i in a:
#     c.append(i)
#e1,e2 = map(int,input().split())
f = list(map(int,input().split()))
#g = [input() for _ in range(a)]

count=0
for i in range(a-1):
    if f[i]!=f[i+1]-1:
        f[i+1]=i+2
        count+=1
if count==2 or count==0:
    print("YES")
else:
    print("NO")

def ans135(N:int, p:str):
    p=list(map(int,p.split()))
    psort=sorted(p)
    if p==psort:
        return("YES")
    else:
        count = 0
        for i in range(len(p)):
            if p[i]!=psort[i]:
                count+=1
        if count==2:
            return("YES")
        else:
            return("NO")

N=int(input())
p=input()
print(ans135(N,p))

def check(n, list1): 
    new = [] 
    for i in range(n): 
        new.append(list1[i]) 
          
    new.sort()
    swaps = 0
    for i in range(n): 
        if list1[i] != new[i]: 
            swaps += 1
              
    if swaps == 0 or swaps == 2: 
        return True
    else: 
        return False
  
n = int(input())

numbers =list(map(int, input().split()[:n]))



if check(n, numbers): 
    print("YES")
else:
    print("NO") 

      


n = int(input())
p = [int(i) for i in input().split()]
cnt = 0
ps = sorted(p)
for i in range(n):
  if p[i] != ps[i]:
    cnt += 1
if cnt in [0, 2]:
  print('YES')
else:
  print('NO')

N = int(input())
p = list(map(int, input().split()))
n = [i+1 for i in range(N)]
for i in range(N):
    if n[i] != p[i]:
        for j in range(N -i -1):
            if n[i+j+1] != p[i+j+1]:
                p[i], p[i+j+1] = p[i+j+1], p[i]
                break
if n == p:
    print("YES")
else:
    print("NO")

# Nと数列pを受け取る
N = int(input())
p = list(map(int, input().split()))

# pがすでに昇順だったら、YESにする
result = 'NO'
sorted_p = sorted(p)
# if p == sorted_p:
#   result = 'YES'
# else:
#   # pから2つの要素を入れ替えて昇順になったら、YESにする
#   count = 0
#   for i in range(N):
#     if p[i] != sorted_p[i]:
#       count += 1
#   if count == 2:
#     result = 'YES'

# pから2つの要素を入れ替えて昇順になったら、YESにする
count = 0
for i in range(N):
  if p[i] != sorted_p[i]:
    count += 1
if count <= 2:
  result = 'YES'

# 出力する
print(result)

N = int(input())
P = [int(s) for s in input().split()]
index = []
for i in range(N):
    index.append(P.index(i+1))
count = 0
for i, j in enumerate(index):
    if i != j:
        count += 1
if count > 2:
    print("NO")
else:
    print("YES")


N = int(input())
P = list(map(int, input().split()))

cnt = 0
for n in range(N):
    if (n+1) != P[n]:
        cnt += 1

if cnt <= 2:
    print("YES")
else:
    print("NO")

N=int(input())
ans=0
d=[int(s) for s in input().split()]
for i in range(N):
    if d[i]!=i+1:
        ans+=1
if ans>=3:
    print("NO")
else:
    print("YES")

import sys

def solve():
    input = sys.stdin.readline
    N = int(input())
    P = [int(p) for p in input().split()]
    diff = 0
    for i in range(N):
        if P[i] != i + 1: diff += 1
    if diff <= 2: print("YES")
    else: print("NO")

    return 0

def __starting_point():
    solve()
__starting_point()

n = int(input())
a = list(map(int, input().split(' ')))

ok = 0
f = 1
for i in range(n):
    if a[i] - 1 != i:
        f = 0
if f:
    ok = 1
for i in range(n):
    for j in range(i + 1, n):
        a[i], a[j] = a[j], a[i]
        f = 1
        for k in range(n):
            if a[k] - 1 != k:
                f = 0
        if f:
            ok = 1
        a[i], a[j] = a[j], a[i]
print('YES' if ok else 'NO')

n = int(input())
p = list(map(int, input().split()))
error = 0
for i in range(n):
    if p[i] != i+1:
        error += 1
    if error > 2:
        print('NO')
        return
print('YES')


N = int(input())
p = [int(i) for i in input().split()]
r = sorted(p)
for i in range(N):
    for j in range(i+1,N):
        q = []
        for k in range(N):
            if(k == i):
                q.append(p[j])
            elif(k == j):
                q.append(p[i])
            else:
                q.append(p[k])
        if(p == r or q == r):
            print("YES")
            return
print("NO")

N = int(input())
p = list(map(int, input().split()))

res = False
for i in range(N):
    for j in range(i, N):
        flg = True
        p[i], p[j] = p[j], p[i]
        for k in range(N - 1):
            if p[k] > p[k + 1]:
                flg = False
        if flg:
            res = True
            break
        p[i], p[j] = p[j], p[i]

print(['NO', 'YES'][res])

n = int(input())
p = list(map(int, input().split()))
cnt = 0

for i in range(n):
    if p[i] != i+1:
        cnt += 1
print(('YES' if cnt == 0 or cnt == 2 else 'NO'))



n=int(input())
p_list=[int(i) for i in input().split()]

np_list=sorted(p_list)

count=0
for i in range(n):
    if p_list[i]!=np_list[i]:
        count+=1

if count==2 or count==0:
    ans="YES"
else:
    ans="NO"

print(ans)

import sys
import itertools
tokens = itertools.chain.from_iterable(list(map(str.split, sys.stdin)))

yesno = ['NO', 'YES']
n = int(next(tokens))
p = [int(next(tokens)) for _ in range(n)]


s = sum(1 if a != b else 0 for a, b in zip(p, sorted(p)))
print((yesno[s == 2 or s == 0]))


x = int(input())
p = list(map(int, input().strip().split()))
k = sorted(p)
count = 0
for i in range(0,x):
    if p[i] == k[i]:
        count += 1

if (x - count) == 2 or (x - count) == 0:
    print('YES')
else:
    print('NO')


N=int(input())
List=list(input().split())
List=list(map(int,List))
List_sorted=sorted(List)

i=0
while i<len(List)-1:
    j=i+1
    ListTest=list(List)
    count=0
    while j<len(List):
        if ListTest==List_sorted:
            print('YES')
            return
        ListTest[i],ListTest[j]=ListTest[j],ListTest[i]
        if ListTest==List_sorted:
            print('YES')
            return
        else:
            ListTest=list(List)
        j+=1
    i+=1
print('NO')


n=int(input())
p=list(map(int,input().split()))
for i in range(n):
    for j in range(n):
        a=p[:]
        a[i],a[j]=a[j],a[i]
        if a==sorted(a):
            print('YES')
            return
print('NO')

n = int(input())
p = list(map(int, input().split()))

q = p.copy()
q.sort()

counter = 0
for i in range(n):
    if p[i] != q[i]:
        counter += 1

if counter == 0 or counter == 2:
    print('YES')
else:
    print('NO')


# ABC135

N = int(input())
P = list(map(int, input().split()))
sorted_P = sorted(P)
cnt = 0
for i in range(len(P)):
    if P[i] != sorted_P[i]:
        cnt += 1

    if cnt > 2:
        print("NO")
        return
print("YES")

n=int(input())
P=list(map(int,input().split()))
Psort=sorted(P)
cnt = 0
for i in range(n):
  if P[i] != Psort[i]:
    cnt += 1
if cnt <= 2:
  print("YES")
else:
  print("NO")

N = int(input())
p = list(map(int, input().split()))

cnt = 0
for i in range(N):
  if p[i] != i+1:
    cnt += 1
if cnt==0 or cnt==2:
  print("YES")
else:
  print("NO")

N = int(input())
p = list(map(int, input().split()))

# p_i != i なる要素p_iの数をkとするとき、
# k<=2 であるときに限り1回以下のスワップでpを昇順にできる
# k=0 のとき、最初からpは昇順
# k=1 にはならない
# k=2 のとき p_i != i である2つの要素を1回スワップすることでpを昇順にできる

q = list(range(1, N+1))

k = 0
for i in range(N):
  if p[i] != q[i]: k += 1
    
if k <= 2: print('YES')
else: print('NO')

N = int(input())
p = list(map(int,input().split()))

if p == sorted(p):
    print('YES')
else:
    ret = 'NO'
    # 全通りの組み合わせをみて、昇順かどうか調べる
    for i in range(N-1):
        for j in range(i+1, N):

            # p[i]とp[j]より小さければ抜ける
            if p[i] < p[j]:
                is_sorted = False
                continue
            else:
                k = p.copy()
                k[i], k[j] = k[j], k[i]

            is_sorted = True
            for l in range(N-1):
                if k[l+1] < k[l]:
                    is_sorted = False
                    break

            if is_sorted:
                ret = 'YES'
                break
        if is_sorted:
            break

    print(ret)

n = int(input())
p = list(map(int,input().rstrip().split()))
pp = sorted(p)
cnt = 0
for i in range(n):
    if p[i]!=pp[i]:
        cnt+=1
if cnt>2:
    print("NO")
else:
    print("YES")

import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    p = LI()

    cnt = len([j for i, j in zip(p, sorted(p)) if i != j])
    if cnt <= 2:
        print('YES')
    else:
        print('NO')

def __starting_point():
    resolve()

__starting_point()

n = int(input())
p = list(map(int,input().split()))

ps = sorted(p)
cnt = 0
for i,j in zip(p,ps):
    if i!=j:
        cnt += 1
print("YES" if cnt<=2 else "NO")

N = int(input())
p = list(map(int, input().split()))
count = 0
for n in range(N):
  if n+1 != p[n]:
    count += 1
if count in [0,2]:
  print("YES")
else:
  print("NO")

b = int(input())
a = list(map(int,input().split()))
count = 0
for i in range(b):
    if a[i] != i + 1 :
        count += 1
if count <= 2:
    print('YES')
else:
    print('NO')

n = int(input())
p = list(map(int, input().split()))
ans = False
for i in range(n):
    for j in range(i, n):
        p[i], p[j] = p[j], p[i]
        if all(p[k] < p[k + 1] for k in range(n - 1)):
            ans = True
        p[i], p[j] = p[j], p[i]
print((["NO", "YES"][ans]))


n = int(input())
a = list(map(int, input().split()))
print("YES" if [i == j for i, j in zip(a, sorted(a))].count(False) <= 2 else "NO")

n=int(input())
p=list(map(int,input().split()))
cnt=0
for i in range(1,n+1):
  if p[i-1]!=i:
    cnt+=1
if cnt==0 or cnt==2:
  print("YES")
else:
  print("NO")

n = int(input())
nums = list(map(int, input().split()))
sorted_nums = sorted(nums)

not_equal = 0
for p, q in zip(nums, sorted_nums):
    if p != q:
        not_equal += 1

if not_equal <= 2:
    print('YES')
else:
    print('NO')


# B
n = int(input())
ps = list(map(int, input().split()))
count=0
c_ps=sorted(ps)
for i in range(n):
    if ps[i]!=c_ps[i]:
        count+=1

if count in [0,2]:
    print('YES')
else:
    print('NO')

n = int(input())
C = list(i for i in range(1,n+1))
L = list(map(int,input().split()))
cnt = 0
for c,l in zip(C,L):
  if c != l:
    cnt += 1
print('YES' if cnt == 0 or cnt == 2 else 'NO')

n=int(input())
c=list(range(1,n+1))
p=list(map(int,input().split()))
cnt=0

for i in range(n):
  cnt += 1 if c[i]!=p[i] else 0
  
print('YES' if cnt==2 or cnt==0 else 'NO')

n = int(input())
plist = list(map(int, input().split()))
nlist = sorted(plist)

ng_cnt = 0
for p, n in zip(plist, nlist):
    if p != n:
        ng_cnt += 1
if ng_cnt >2:
    print('NO')
else:
    print('YES')

N = int(input())
lst = list(map(int, input().split()))
k = 0

for i in range(N):
    if i+1 != lst[i]:
        k += 1

if k <= 2:
    print('YES')

else:
    print('NO')


n = int(input())
p = list(map(int,input().split()))
count = 0
for i in range(n):
  if i+1 != p[i]:
    count += 1

if count <= 2:
  print("YES")
else:
  print("NO")

N = int(input())
p = list(map(int,input().split()))

count = 0
for i in range(N):
    j = i + 1
    if p[i] != j:
        count += 1
if count > 2:
    ans = 'NO'
else:
    ans = 'YES'

print(ans)

# coding: utf-8

num = int(input())
str = input().split()
table = list(str)
table5 = [int(i) for i in table]
count = 0
table2 = sorted(table5)
table3 = [int(i) for i in table2]
for i in range(num):
    if table5[i] == table3[i]:
        continue
    else:
        count += 1

if count == 2 or count == 0:
    print("YES")
else:
    print("NO")

N = int(input())
P = list(map(int, input().split()))

cnt = 0

for i in range(N):
    if i+1 != P[i]:
        cnt += 1
    if cnt > 2:
        print('NO')
        return

print('YES')

n = int(input())
p = list(map(int, input().split()))

cnt = 0
for i in range(n):
    if i+1 != p[i]:
        cnt += 1
print('NO' if cnt >= 3 else 'YES')

import copy
n = int(input())
p = list(map(int, input().split()))

A = copy.copy(p)
A.sort()
cnt = 0

for i in range(n):
    if A[i] != p[i]:
        cnt += 1

if cnt == 2 or cnt == 0:
    print('YES')
else:
    print('NO')


N = int(input())
p = list(map(int,input().split()))
sp = sorted(p)
count = 0

for i in range(N):
    if p[i] != sp[i]:
        count += 1

if count == 2 or count == 0:
    print('YES')
else:
    print('NO')

N = int(input())
vec = list(map(int,input().split()))
vec2 = [i for i in vec]
vec2.sort()
cnt = 0
for i in range(N):
    cnt += vec[i] != vec2[i]
if cnt <= 2:
    print("YES")
else:
    print("NO")

N=int(input())
P=list(map(int, input().split()))
ido=[]

for i in range(N):
	if P[i]!=i+1:
		ido.append(P[i])

if len(ido)==2 or len(ido)==0:
	print("YES")
else:
	print("NO")

N = int(input())
N_List = list(map(int,input().split()))
cnt = 0
for i in range(N):
    if i + 1 != N_List[i]:
        cnt += 1
print(("NO","YES")[cnt<=2])

from typing import List


def answer(n: int, p: List[int]) -> str:
    count = 0
    for i, num in enumerate(p, start=1):
        if i != num:
            count += 1

    return 'YES' if count <= 2 else 'NO'


def main():
    n = int(input())
    p = list(map(int, input().split()))
    print(answer(n, p))


def __starting_point():
    main()
__starting_point()

n = int(input())
p = list(map(int, input().split()))
flg_cnt = 0
for i in range(n):
    if i+1 != p[i]:
        flg_cnt += 1
    if flg_cnt > 2:
        print('NO')
        return
print('YES')

n=int(input())
l=list(map(int,input().split()))
l2=sorted(l)
c=0
for i in range(len(l)):
    if l[i]!=l2[i]:
        c+=1
print("YES" if c<=2 else "NO")

N = int(input())
lst1 = list(map(int, input().split()))
lst2 = sorted(lst1)
c = 0
for i, j in zip(lst1, lst2):
    if i != j:
        c += 1

if c == 2 or c == 0:
    print('YES')

else:
    print('NO')


import math
import collections
import itertools
import copy

def YesNo(Bool):
    if(Bool):
        print("YES")
    else:
        print("NO")
    return

def resolve():

    N=int(input())
    p=list(map(int,input().split()))
    ans=False

    tmp=copy.deepcopy(p)
    isOK=True
    for k in range(N-1):
        if(tmp[k]>=tmp[k+1]):
            isOK=False
    if(isOK):
        ans=True

    for i in range(N):
        for j in range(i+1,N):
            tmp=[]
            tmp=copy.deepcopy(p)
            tmp[i],tmp[j]=tmp[j],tmp[i]
            isOK=True
            for k in range(N-1):
                if(tmp[k]>=tmp[k+1]):
                    isOK=False
            if(isOK):
                ans=True
    YesNo(ans)

resolve()


n = int(input())
A = list(map(int, input().split( )))
cnt = 0
for i in range(n):
  if A[i] != i+1:
    cnt += 1
if cnt <= 2:
  print('YES')
else:
  print('NO')


N = int(input())
P = list(map(int, input().split()))
#answer = [int(x) for x in range(1, N+1)]
count = 0
for i in range(1, N+1):
  if P[i-1] != i:
    count += 1
if count <= 2:
  print("YES")
else:
  print("NO")

n = int(input())
p = list(map(int,input().split()))
ans = [i for i in range(1,n+1)]
cha = 0
for i in range(n):
  if p[i] != ans[i]:
    cha += 1
    if cha > 2:
      print("NO")
      return
print("YES")

N = int(input())
p = list(map(int, input().split()))

count = 0
for i, j in enumerate(p, 1):
    if i != j:
        count += 1
if count <= 2:
    print('YES')
else:
    print('NO')

n = int(input())
l = list(range(1,n+1))
p = list(map(int,input().split()))
m = 0
for i in range(n):
    if p[i] != l[i]:
        m += 1
if m >= 3:
    print('NO')
else:
    print('YES')

n=int(input())
p=list(map(int,input().split()))
new_p=sorted(p)
cnt=0

for i in range(n):
    if p[i] != new_p[i]:
        cnt += 1
        
if cnt > 2:
    print('NO')
else:
    print('YES')

a = int(input())
b= list(map(int,input().split()))
c = sorted(b)
d = 0
for i in range(a):
  if b[i] != c[i]:
    d += 1
if d <=2:
  print("YES")
else:
  print("NO")



N = int(input())
pi = list(map(int,input().split()))
count = 0
for i in range(N):
  if pi[i] != i + 1:
    count += 1
if count == 0 or count == 2:
  print("YES")
else:
  print("NO")