n = gets.to_i
ary = gets.split(" ").map(&:to_i)

h = Hash.new(0)
num = 1
ary.each do |a|
    h[num] = a unless num == a
    num = num + 1
end

def hikaku(hash)
    a = hash.to_a
    a[0][1] == a[1][0]
end


if h.length >= 3 #ハッシュが３つ以上
    puts "NO"
elsif h.length == 1
    puts "NO"
elsif h.empty? #ハッシュなし
    puts "YES"
elsif hikaku(h) == true
    puts "YES"
elsif hikaku(h) == false
    puts "NO"
else
    puts "NO"
end