import java.util.*;
public class Main {
    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        // 整数の入力
        int N=sc.nextInt();
        int[] A=new int[N];
        for(int i=0;i<N;i++){
            A[i]=sc.nextInt()-1;
        }
        int frag=0;
        for(int i=0;i<N;i++){
            if(A[i]!=i){

                if(frag>=2||A[A[i]]!=i){
                    System.out.println("NO");
                    return;

                }else{
                    frag++;
                }
            }
        }
        System.out.println("YES");







    }
    static int gcd (int a, int b) {
        int temp;
        while((temp = a%b)!=0) {
            a = b;
            b = temp;
        }
        return b;
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        // 整数の入力
        int N=sc.nextInt();
        int[] A=new int[N];
        for(int i=0;i<N;i++){
            A[i]=sc.nextInt()-1;
        }
        int frag=0;
        for(int i=0;i<N;i++){
            if(A[i]!=i){

                if(frag>=2||A[A[i]]!=i){
                    System.out.println("NO");
                    return;

                }else{
                    frag++;
                }
            }
        }
        System.out.println("YES");







    }
    static int gcd (int a, int b) {
        int temp;
        while((temp = a%b)!=0) {
            a = b;
            b = temp;
        }
        return b;
    }
} Main public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        // 整数の入力
        int N=sc.nextInt();
        int[] A=new int[N];
        for(int i=0;i<N;i++){
            A[i]=sc.nextInt()-1;
        }
        int frag=0;
        for(int i=0;i<N;i++){
            if(A[i]!=i){

                if(frag>=2||A[A[i]]!=i){
                    System.out.println("NO");
                    return;

                }else{
                    frag++;
                }
            }
        }
        System.out.println("YES");







    } main {

        Scanner sc = new Scanner(System.in);
        // 整数の入力
        int N=sc.nextInt();
        int[] A=new int[N];
        for(int i=0;i<N;i++){
            A[i]=sc.nextInt()-1;
        }
        int frag=0;
        for(int i=0;i<N;i++){
            if(A[i]!=i){

                if(frag>=2||A[A[i]]!=i){
                    System.out.println("NO");
                    return;

                }else{
                    frag++;
                }
            }
        }
        System.out.println("YES");







    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N=sc.nextInt(); N sc.nextInt() sc.nextInt sc int[] A=new int[N]; A new int[N] N for(int i=0;i<N;i++){
            A[i]=sc.nextInt()-1;
        } int i=0; int i=0; i 0 i<N i N i++ i++ i {
            A[i]=sc.nextInt()-1;
        } {
            A[i]=sc.nextInt()-1;
        } A[i]=sc.nextInt()-1 A[i] A i sc.nextInt()-1 sc.nextInt() sc.nextInt sc 1 int frag=0; frag 0 for(int i=0;i<N;i++){
            if(A[i]!=i){

                if(frag>=2||A[A[i]]!=i){
                    System.out.println("NO");
                    return;

                }else{
                    frag++;
                }
            }
        } int i=0; int i=0; i 0 i<N i N i++ i++ i {
            if(A[i]!=i){

                if(frag>=2||A[A[i]]!=i){
                    System.out.println("NO");
                    return;

                }else{
                    frag++;
                }
            }
        } {
            if(A[i]!=i){

                if(frag>=2||A[A[i]]!=i){
                    System.out.println("NO");
                    return;

                }else{
                    frag++;
                }
            }
        } if(A[i]!=i){

                if(frag>=2||A[A[i]]!=i){
                    System.out.println("NO");
                    return;

                }else{
                    frag++;
                }
            } A[i]!=i A[i] A i i {

                if(frag>=2||A[A[i]]!=i){
                    System.out.println("NO");
                    return;

                }else{
                    frag++;
                }
            } if(frag>=2||A[A[i]]!=i){
                    System.out.println("NO");
                    return;

                }else{
                    frag++;
                } frag>=2||A[A[i]]!=i frag>=2 frag 2 A[A[i]]!=i A[A[i]] A A[i] A i i {
                    System.out.println("NO");
                    return;

                } System.out.println("NO") System.out.println System.out System System.out "NO" return; {
                    frag++;
                } frag++ frag System.out.println("YES") System.out.println System.out System System.out "YES" String[] args args static int gcd (int a, int b) {
        int temp;
        while((temp = a%b)!=0) {
            a = b;
            b = temp;
        }
        return b;
    } gcd {
        int temp;
        while((temp = a%b)!=0) {
            a = b;
            b = temp;
        }
        return b;
    } int temp; temp while((temp = a%b)!=0) {
            a = b;
            b = temp;
        } (temp = a%b)!=0 (temp = a%b) temp a%b a b 0 {
            a = b;
            b = temp;
        } a = b a b b = temp b temp return b; b int a a int b b public class Main {
    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        // 整数の入力
        int N=sc.nextInt();
        int[] A=new int[N];
        for(int i=0;i<N;i++){
            A[i]=sc.nextInt()-1;
        }
        int frag=0;
        for(int i=0;i<N;i++){
            if(A[i]!=i){

                if(frag>=2||A[A[i]]!=i){
                    System.out.println("NO");
                    return;

                }else{
                    frag++;
                }
            }
        }
        System.out.println("YES");







    }
    static int gcd (int a, int b) {
        int temp;
        while((temp = a%b)!=0) {
            a = b;
            b = temp;
        }
        return b;
    }
} public class Main {
    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        // 整数の入力
        int N=sc.nextInt();
        int[] A=new int[N];
        for(int i=0;i<N;i++){
            A[i]=sc.nextInt()-1;
        }
        int frag=0;
        for(int i=0;i<N;i++){
            if(A[i]!=i){

                if(frag>=2||A[A[i]]!=i){
                    System.out.println("NO");
                    return;

                }else{
                    frag++;
                }
            }
        }
        System.out.println("YES");







    }
    static int gcd (int a, int b) {
        int temp;
        while((temp = a%b)!=0) {
            a = b;
            b = temp;
        }
        return b;
    }
} Main
import java.util.*;
public class Main {
    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        // 整数の入力
        int N=sc.nextInt();
        int[] A=new int[N];
        for(int i=0;i<N;i++){
            A[i]=sc.nextInt()-1;
        }
        int frag=0;
        for(int i=0;i<N;i++){
            if(A[i]!=i){

                if(frag>=2||A[A[i]]!=i){
                    System.out.println("NO");
                    return;

                }else{
                    frag++;
                }
            }
        }
        System.out.println("YES");







    }
    static int gcd (int a, int b) {
        int temp;
        while((temp = a%b)!=0) {
            a = b;
            b = temp;
        }
        return b;
    }
}
