

import java.util.InputMismatchException;
import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		try {
			Scanner scanner = new Scanner(System.in);

			int n = scanner.nextInt();
			scanner.nextLine(); //次の行へ

			int[] p = new int[n+1];
			int i = 1;
			while (scanner.hasNext()) {
				p[i] = scanner.nextInt();
				i++;
			}

			Main swap = new Main();
			if(swap.swap(p)) {
				System.out.println("YES");
			}else{
				System.out.println("NO");
			}

			scanner.close();

		} catch (InputMismatchException e) {
			// TODO: handle exception
			System.out.println("整数を入力してください");
		}
	}


	public Boolean swap(int[] p) {
		int count = 0;

		for(int i = 1; i < p.length; i++) {
			//i番目の数字がiでは無い
			if(p[i] != i) {
				//iと（i番目の数字）番目の数字が一致
				if(i == p[p[i]]) {
					if(count < 2) {		//2回目まで
						 count++;
					}else{				//2回目以降
						return false;
					}
				}else{
					return false;
				}
			}
		}

		return true;
	}

}
 import java.util.InputMismatchException; InputMismatchException java.util import java.util.Scanner; Scanner java.util public class Main{
	public static void main(String[] args) {
		try {
			Scanner scanner = new Scanner(System.in);

			int n = scanner.nextInt();
			scanner.nextLine(); //次の行へ

			int[] p = new int[n+1];
			int i = 1;
			while (scanner.hasNext()) {
				p[i] = scanner.nextInt();
				i++;
			}

			Main swap = new Main();
			if(swap.swap(p)) {
				System.out.println("YES");
			}else{
				System.out.println("NO");
			}

			scanner.close();

		} catch (InputMismatchException e) {
			// TODO: handle exception
			System.out.println("整数を入力してください");
		}
	}


	public Boolean swap(int[] p) {
		int count = 0;

		for(int i = 1; i < p.length; i++) {
			//i番目の数字がiでは無い
			if(p[i] != i) {
				//iと（i番目の数字）番目の数字が一致
				if(i == p[p[i]]) {
					if(count < 2) {		//2回目まで
						 count++;
					}else{				//2回目以降
						return false;
					}
				}else{
					return false;
				}
			}
		}

		return true;
	}

} Main public static void main(String[] args) {
		try {
			Scanner scanner = new Scanner(System.in);

			int n = scanner.nextInt();
			scanner.nextLine(); //次の行へ

			int[] p = new int[n+1];
			int i = 1;
			while (scanner.hasNext()) {
				p[i] = scanner.nextInt();
				i++;
			}

			Main swap = new Main();
			if(swap.swap(p)) {
				System.out.println("YES");
			}else{
				System.out.println("NO");
			}

			scanner.close();

		} catch (InputMismatchException e) {
			// TODO: handle exception
			System.out.println("整数を入力してください");
		}
	} main {
		try {
			Scanner scanner = new Scanner(System.in);

			int n = scanner.nextInt();
			scanner.nextLine(); //次の行へ

			int[] p = new int[n+1];
			int i = 1;
			while (scanner.hasNext()) {
				p[i] = scanner.nextInt();
				i++;
			}

			Main swap = new Main();
			if(swap.swap(p)) {
				System.out.println("YES");
			}else{
				System.out.println("NO");
			}

			scanner.close();

		} catch (InputMismatchException e) {
			// TODO: handle exception
			System.out.println("整数を入力してください");
		}
	} try {
			Scanner scanner = new Scanner(System.in);

			int n = scanner.nextInt();
			scanner.nextLine(); //次の行へ

			int[] p = new int[n+1];
			int i = 1;
			while (scanner.hasNext()) {
				p[i] = scanner.nextInt();
				i++;
			}

			Main swap = new Main();
			if(swap.swap(p)) {
				System.out.println("YES");
			}else{
				System.out.println("NO");
			}

			scanner.close();

		} catch (InputMismatchException e) {
			// TODO: handle exception
			System.out.println("整数を入力してください");
		} catch (InputMismatchException e) {
			// TODO: handle exception
			System.out.println("整数を入力してください");
		} InputMismatchException e {
			// TODO: handle exception
			System.out.println("整数を入力してください");
		} System.out.println("整数を入力してください") System.out.println System.out System System.out "整数を入力してください" {
			Scanner scanner = new Scanner(System.in);

			int n = scanner.nextInt();
			scanner.nextLine(); //次の行へ

			int[] p = new int[n+1];
			int i = 1;
			while (scanner.hasNext()) {
				p[i] = scanner.nextInt();
				i++;
			}

			Main swap = new Main();
			if(swap.swap(p)) {
				System.out.println("YES");
			}else{
				System.out.println("NO");
			}

			scanner.close();

		} Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int n = scanner.nextInt(); n scanner.nextInt() scanner.nextInt scanner scanner.nextLine() scanner.nextLine scanner int[] p = new int[n+1]; p new int[n+1] n+1 n 1 int i = 1; i 1 while (scanner.hasNext()) {
				p[i] = scanner.nextInt();
				i++;
			} scanner.hasNext() scanner.hasNext scanner {
				p[i] = scanner.nextInt();
				i++;
			} p[i] = scanner.nextInt() p[i] p i scanner.nextInt() scanner.nextInt scanner i++ i Main swap = new Main(); swap new Main() if(swap.swap(p)) {
				System.out.println("YES");
			}else{
				System.out.println("NO");
			} swap.swap(p) swap.swap swap p {
				System.out.println("YES");
			} System.out.println("YES") System.out.println System.out System System.out "YES" {
				System.out.println("NO");
			} System.out.println("NO") System.out.println System.out System System.out "NO" scanner.close() scanner.close scanner String[] args args public Boolean swap(int[] p) {
		int count = 0;

		for(int i = 1; i < p.length; i++) {
			//i番目の数字がiでは無い
			if(p[i] != i) {
				//iと（i番目の数字）番目の数字が一致
				if(i == p[p[i]]) {
					if(count < 2) {		//2回目まで
						 count++;
					}else{				//2回目以降
						return false;
					}
				}else{
					return false;
				}
			}
		}

		return true;
	} swap {
		int count = 0;

		for(int i = 1; i < p.length; i++) {
			//i番目の数字がiでは無い
			if(p[i] != i) {
				//iと（i番目の数字）番目の数字が一致
				if(i == p[p[i]]) {
					if(count < 2) {		//2回目まで
						 count++;
					}else{				//2回目以降
						return false;
					}
				}else{
					return false;
				}
			}
		}

		return true;
	} int count = 0; count 0 for(int i = 1; i < p.length; i++) {
			//i番目の数字がiでは無い
			if(p[i] != i) {
				//iと（i番目の数字）番目の数字が一致
				if(i == p[p[i]]) {
					if(count < 2) {		//2回目まで
						 count++;
					}else{				//2回目以降
						return false;
					}
				}else{
					return false;
				}
			}
		} int i = 1; int i = 1; i 1 i < p.length i p.length p p.length i++ i++ i {
			//i番目の数字がiでは無い
			if(p[i] != i) {
				//iと（i番目の数字）番目の数字が一致
				if(i == p[p[i]]) {
					if(count < 2) {		//2回目まで
						 count++;
					}else{				//2回目以降
						return false;
					}
				}else{
					return false;
				}
			}
		} {
			//i番目の数字がiでは無い
			if(p[i] != i) {
				//iと（i番目の数字）番目の数字が一致
				if(i == p[p[i]]) {
					if(count < 2) {		//2回目まで
						 count++;
					}else{				//2回目以降
						return false;
					}
				}else{
					return false;
				}
			}
		} if(p[i] != i) {
				//iと（i番目の数字）番目の数字が一致
				if(i == p[p[i]]) {
					if(count < 2) {		//2回目まで
						 count++;
					}else{				//2回目以降
						return false;
					}
				}else{
					return false;
				}
			} p[i] != i p[i] p i i {
				//iと（i番目の数字）番目の数字が一致
				if(i == p[p[i]]) {
					if(count < 2) {		//2回目まで
						 count++;
					}else{				//2回目以降
						return false;
					}
				}else{
					return false;
				}
			} if(i == p[p[i]]) {
					if(count < 2) {		//2回目まで
						 count++;
					}else{				//2回目以降
						return false;
					}
				}else{
					return false;
				} i == p[p[i]] i p[p[i]] p p[i] p i {
					if(count < 2) {		//2回目まで
						 count++;
					}else{				//2回目以降
						return false;
					}
				} if(count < 2) {		//2回目まで
						 count++;
					}else{				//2回目以降
						return false;
					} count < 2 count 2 {		//2回目まで
						 count++;
					} count++ count {				//2回目以降
						return false;
					} return false; false {
					return false;
				} return false; false return true; true int[] p p public class Main{
	public static void main(String[] args) {
		try {
			Scanner scanner = new Scanner(System.in);

			int n = scanner.nextInt();
			scanner.nextLine(); //次の行へ

			int[] p = new int[n+1];
			int i = 1;
			while (scanner.hasNext()) {
				p[i] = scanner.nextInt();
				i++;
			}

			Main swap = new Main();
			if(swap.swap(p)) {
				System.out.println("YES");
			}else{
				System.out.println("NO");
			}

			scanner.close();

		} catch (InputMismatchException e) {
			// TODO: handle exception
			System.out.println("整数を入力してください");
		}
	}


	public Boolean swap(int[] p) {
		int count = 0;

		for(int i = 1; i < p.length; i++) {
			//i番目の数字がiでは無い
			if(p[i] != i) {
				//iと（i番目の数字）番目の数字が一致
				if(i == p[p[i]]) {
					if(count < 2) {		//2回目まで
						 count++;
					}else{				//2回目以降
						return false;
					}
				}else{
					return false;
				}
			}
		}

		return true;
	}

} public class Main{
	public static void main(String[] args) {
		try {
			Scanner scanner = new Scanner(System.in);

			int n = scanner.nextInt();
			scanner.nextLine(); //次の行へ

			int[] p = new int[n+1];
			int i = 1;
			while (scanner.hasNext()) {
				p[i] = scanner.nextInt();
				i++;
			}

			Main swap = new Main();
			if(swap.swap(p)) {
				System.out.println("YES");
			}else{
				System.out.println("NO");
			}

			scanner.close();

		} catch (InputMismatchException e) {
			// TODO: handle exception
			System.out.println("整数を入力してください");
		}
	}


	public Boolean swap(int[] p) {
		int count = 0;

		for(int i = 1; i < p.length; i++) {
			//i番目の数字がiでは無い
			if(p[i] != i) {
				//iと（i番目の数字）番目の数字が一致
				if(i == p[p[i]]) {
					if(count < 2) {		//2回目まで
						 count++;
					}else{				//2回目以降
						return false;
					}
				}else{
					return false;
				}
			}
		}

		return true;
	}

} Main


import java.util.InputMismatchException;
import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		try {
			Scanner scanner = new Scanner(System.in);

			int n = scanner.nextInt();
			scanner.nextLine(); //次の行へ

			int[] p = new int[n+1];
			int i = 1;
			while (scanner.hasNext()) {
				p[i] = scanner.nextInt();
				i++;
			}

			Main swap = new Main();
			if(swap.swap(p)) {
				System.out.println("YES");
			}else{
				System.out.println("NO");
			}

			scanner.close();

		} catch (InputMismatchException e) {
			// TODO: handle exception
			System.out.println("整数を入力してください");
		}
	}


	public Boolean swap(int[] p) {
		int count = 0;

		for(int i = 1; i < p.length; i++) {
			//i番目の数字がiでは無い
			if(p[i] != i) {
				//iと（i番目の数字）番目の数字が一致
				if(i == p[p[i]]) {
					if(count < 2) {		//2回目まで
						 count++;
					}else{				//2回目以降
						return false;
					}
				}else{
					return false;
				}
			}
		}

		return true;
	}

}

