import java.io.InputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Scanner;

public class Codecomp {

  final PrintStream out;
  final Scanner sc;

  public Codecomp(InputStream is, PrintStream ps) {
    this.sc = new Scanner(is);
    this.out = ps;
  }
  long mod = 1000000007;
  long exp(long base, long n) {
    long val = 1;
    while (n != 0) {
      if ((n & 1) == 1)val = (base * val) % mod;
      base = base * base % mod;
      n /= 2;
    }
    return val;
  }

  int n;
  int[] v;
  int[] u;
  void solve() {
    n = sc.nextInt();
    v = new int[n];
    u = new int[n];
    for (int i = 0; i < n; ++i) {
      v[i] = sc.nextInt() - 1;
    }
    ArrayList<Integer> list = new ArrayList<Integer>();
    for (int i = 0; i < n; ++i) {
      int cur = i, s = 0;
      while (v[cur] >= 0) {
        int next = v[cur];
        u[cur] = s;
        v[cur] = -1 - i;
        cur = next;
        s++;
      }
      if (v[cur] == -1 - i) {
        list.add(s - u[cur]);
      }
    }
    list.sort(Integer::compareTo);
    int e = list.size() - 1;

    long nans = 0;
    for (int a : list) {
      nans += exp(2, n - a) * 2;
      nans = nans % mod;
    }
    long ans = exp(2, n);
    ans -= nans;
    if (ans < 0)ans += mod;
    System.out.println(ans);
  }

  public static void main(String[] args) {
    new Codecomp(System.in, System.out).solve();
  }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.Collection;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Objects;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.Queue;
import java.util.LinkedList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        DDirectedRoads solver = new DDirectedRoads();
        solver.solve(1, in, out);
        out.close();
    }

    static class DDirectedRoads {
        private int n;
        private ArrayList<Integer>[] graph;
        private int[] dis;

        private int bfs(int start, int des) {
            dis = new int[n + 1];
            Arrays.fill(dis, -1);
            dis[start] = 0;
            Queue<Integer> queue = new LinkedList<>();
            queue.offer(start);
            while (!queue.isEmpty()) {
                int v = queue.poll();
                for (int w : graph[v]) {
                    if (dis[w] == -1) {
                        queue.offer(w);
                        dis[w] = dis[v] + 1;
                        if (w == des) return dis[w];
                    }
                }
            }
            return dis[des];
        }

        private long modPow(long base, long exp, long mod) {
            long res = 1;
            while (exp > 0) {
                if ((exp & 1) != 0) res = ((res * base) % mod + mod) % mod;
                exp >>= 1;
                base = ((base * base) % mod + mod) % mod;
            }
            return res;
        }

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            n = in.nextInt();
            graph = new ArrayList[n + 1];
            for (int i = 0; i <= n; i++) graph[i] = new ArrayList<>();
            DisjointSet ds = new DisjointSet(n);
            int cycleSize = -1;
            ArrayList<Util.Pair<Integer>> al = new ArrayList<>();
            for (int i = 1; i <= n; i++) {
                int a = i, b = in.nextInt();
                if (ds.isInSameSet(a, b)) {
                    cycleSize = bfs(a, b) + 1;
                    al.add(new Util.Pair<>(cycleSize, a));
                } else ds.union(a, b);
                graph[a].add(b);
                graph[b].add(a);
            }
            long res = 1;
            for (Util.Pair<Integer> pair : al) {
                int cycleLength = pair.x, setsize = ds.setSize[ds.root(pair.y)];
                res *= (modPow(2, setsize, 1_000_000_007) - modPow(2, setsize - cycleLength + 1, 1_000_000_007));
                res = ((res % 1_000_000_007) + 1_000_000_007) % 1_000_000_007;
            }
            out.println(res);
            out.flush();
        }

        class DisjointSet {
            int[] parent;
            int[] setSize;

            private DisjointSet(int n) {
                parent = new int[n + 1];
                setSize = new int[n + 1];
                for (int i = 1; i <= n; i++) {
                    parent[i] = i;
                    setSize[i] = 1;
                }
            }

            private int root(int x) {
                if (x == parent[x]) return x;
                return parent[x] = (root(parent[x]));
            }

            private boolean _union(int n1, int n2) {
                if (n1 == n2) return false;
                if (setSize[n1] > setSize[n2]) {
                    parent[n2] = parent[n1];
                    setSize[n1] += setSize[n2];
                } else {
                    parent[n1] = parent[n2];
                    setSize[n2] += setSize[n1];
                }
                return true;
            }

            private boolean union(int n1, int n2) {
                return _union(root(n1), root(n2));
            }

            private boolean isInSameSet(int n1, int n2) {
                return root(n1) == root(n2);
            }

        }

    }

    static class Util {
        public static class Pair<T> {
            public T x;
            public T y;

            public Pair(T x, T y) {
                this.x = x;
                this.y = y;
            }

            public boolean equals(Object obj) {
                if (!(obj instanceof Util.Pair)) return false;
                Util.Pair<T> pair = (Util.Pair<T>) obj;
                return this.x.equals(pair.x) && this.y.equals(pair.y);
            }

            public String toString() {
                return ("(" + this.x + "," + this.y + ")");
            }

            public int hashCode() {
                return Objects.hash(x, y);
            }

        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[8192];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public InputReader(FileInputStream file) {
            this.stream = file;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res = (res << 3) + (res << 1) + c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;
        private ArrayList<String> res = new ArrayList<>();
        private StringBuilder sb = new StringBuilder("");

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void println(Object... objects) {
            for (int i = 0; i < objects.length; i++) {
                sb.append(objects[i]);
            }
            res.add(sb.toString());
            sb = new StringBuilder("");
        }

        public void close() {
            writer.close();
        }

        public void flush() {
            for (String str : res) writer.printf("%s\n", str);
            res.clear();
            sb = new StringBuilder("");
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.Collection;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.Queue;
import java.util.LinkedList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        DDirectedRoads solver = new DDirectedRoads();
        solver.solve(1, in, out);
        out.close();
    }

    static class DDirectedRoads {
        private int n;
        private ArrayList<Integer>[] graph;
        private int[] dis;

        private int bfs(int start, int des) {
            dis = new int[n + 1];
            Arrays.fill(dis, -1);
            dis[start] = 0;
            Queue<Integer> queue = new LinkedList<>();
            queue.offer(start);
            while (!queue.isEmpty()) {
                int v = queue.poll();
                for (int w : graph[v]) {
                    if (dis[w] == -1) {
                        queue.offer(w);
                        dis[w] = dis[v] + 1;
                        if (w == des) return dis[w];
                    }
                }
            }
            return dis[des];
        }

        private long modPow(long base, long exp, long mod) {
            long res = 1;
            while (exp > 0) {
                if ((exp & 1) != 0) res = ((res * base) % mod + mod) % mod;
                exp >>= 1;
                base = ((base * base) % mod + mod) % mod;
            }
            return res;
        }

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            n = in.nextInt();
            graph = new ArrayList[n + 1];
            for (int i = 0; i <= n; i++) graph[i] = new ArrayList<>();
            DisjointSet ds = new DisjointSet(n);
            int cycleSize = -1;
            for (int i = 1; i <= n; i++) {
                int a = i, b = in.nextInt();
                if (ds.isInSameSet(a, b)) {
                    cycleSize = bfs(a, b) + 1;
                } else ds.union(a, b);
                graph[a].add(b);
                graph[b].add(a);
            }
            out.println(modPow(2, n, 1_000_000_007) - modPow(2, n - cycleSize + 1, 1_000_000_007));
            out.flush();
        }

        class DisjointSet {
            int[] parent;
            int[] setSize;

            private DisjointSet(int n) {
                parent = new int[n + 1];
                setSize = new int[n + 1];
                for (int i = 1; i <= n; i++) {
                    parent[i] = i;
                    setSize[i] = 1;
                }
            }

            private int root(int x) {
                if (x == parent[x]) return x;
                return parent[x] = (root(parent[x]));
            }

            private boolean _union(int n1, int n2) {
                if (n1 == n2) return false;
                if (setSize[n1] > setSize[n2]) {
                    parent[n2] = parent[n1];
                    setSize[n1] += setSize[n2];
                } else {
                    parent[n1] = parent[n2];
                    setSize[n2] += setSize[n1];
                }
                return true;
            }

            private boolean union(int n1, int n2) {
                return _union(root(n1), root(n2));
            }

            private boolean isInSameSet(int n1, int n2) {
                return root(n1) == root(n2);
            }

        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[8192];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public InputReader(FileInputStream file) {
            this.stream = file;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res = (res << 3) + (res << 1) + c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;
        private ArrayList<String> res = new ArrayList<>();
        private StringBuilder sb = new StringBuilder("");

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void println(Object... objects) {
            for (int i = 0; i < objects.length; i++) {
                sb.append(objects[i]);
            }
            res.add(sb.toString());
            sb = new StringBuilder("");
        }

        public void close() {
            writer.close();
        }

        public void flush() {
            for (String str : res) writer.printf("%s\n", str);
            res.clear();
            sb = new StringBuilder("");
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.Collection;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Objects;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.Queue;
import java.util.LinkedList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        DDirectedRoads solver = new DDirectedRoads();
        solver.solve(1, in, out);
        out.close();
    }

    static class DDirectedRoads {
        private int n;
        private ArrayList<Integer>[] graph;
        private int[] dis;

        private int bfs(int start, int des) {
            dis[start] = 0;
            Queue<Integer> queue = new LinkedList<>();
            queue.offer(start);
            while (!queue.isEmpty()) {
                int v = queue.poll();
                for (int w : graph[v]) {
                    if (dis[w] == -1) {
                        queue.offer(w);
                        dis[w] = dis[v] + 1;
                        if (w == des) return dis[w];
                    }
                }
            }
            return dis[des];
        }

        private long modPow(long base, long exp, long mod) {
            long res = 1;
            while (exp > 0) {
                if ((exp & 1) != 0) res = ((res * base) % mod + mod) % mod;
                exp >>= 1;
                base = ((base * base) % mod + mod) % mod;
            }
            return res;
        }

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            n = in.nextInt();
            dis = new int[n + 1];
            Arrays.fill(dis, -1);
            graph = new ArrayList[n + 1];
            for (int i = 0; i <= n; i++) graph[i] = new ArrayList<>();
            DisjointSet ds = new DisjointSet(n);
            int cycleSize;
            ArrayList<Util.Pair<Integer>> al = new ArrayList<>();
            for (int i = 1; i <= n; i++) {
                int a = i, b = in.nextInt();
                if (ds.isInSameSet(a, b)) {
                    cycleSize = bfs(a, b) + 1;
                    al.add(new Util.Pair<>(cycleSize, a));
                } else ds.union(a, b);
                graph[a].add(b);
                graph[b].add(a);
            }
            long res = 1;
            for (Util.Pair<Integer> pair : al) {
                int cycleLength = pair.x, setsize = ds.setSize[ds.root(pair.y)];
                res *= (modPow(2, setsize, 1_000_000_007) - modPow(2, setsize - cycleLength + 1, 1_000_000_007));
                res = ((res % 1_000_000_007) + 1_000_000_007) % 1_000_000_007;
            }
            out.println(res);
            out.flush();
        }

        class DisjointSet {
            int[] parent;
            int[] setSize;

            private DisjointSet(int n) {
                parent = new int[n + 1];
                setSize = new int[n + 1];
                for (int i = 1; i <= n; i++) {
                    parent[i] = i;
                    setSize[i] = 1;
                }
            }

            private int root(int x) {
                if (x == parent[x]) return x;
                return parent[x] = (root(parent[x]));
            }

            private boolean _union(int n1, int n2) {
                if (n1 == n2) return false;
                if (setSize[n1] > setSize[n2]) {
                    parent[n2] = parent[n1];
                    setSize[n1] += setSize[n2];
                } else {
                    parent[n1] = parent[n2];
                    setSize[n2] += setSize[n1];
                }
                return true;
            }

            private boolean union(int n1, int n2) {
                return _union(root(n1), root(n2));
            }

            private boolean isInSameSet(int n1, int n2) {
                return root(n1) == root(n2);
            }

        }

    }

    static class Util {
        public static class Pair<T> {
            public T x;
            public T y;

            public Pair(T x, T y) {
                this.x = x;
                this.y = y;
            }

            public boolean equals(Object obj) {
                if (!(obj instanceof Util.Pair)) return false;
                Util.Pair<T> pair = (Util.Pair<T>) obj;
                return this.x.equals(pair.x) && this.y.equals(pair.y);
            }

            public String toString() {
                return ("(" + this.x + "," + this.y + ")");
            }

            public int hashCode() {
                return Objects.hash(x, y);
            }

        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[8192];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public InputReader(FileInputStream file) {
            this.stream = file;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res = (res << 3) + (res << 1) + c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;
        private ArrayList<String> res = new ArrayList<>();
        private StringBuilder sb = new StringBuilder("");

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void println(Object... objects) {
            for (int i = 0; i < objects.length; i++) {
                sb.append(objects[i]);
            }
            res.add(sb.toString());
            sb = new StringBuilder("");
        }

        public void close() {
            writer.close();
        }

        public void flush() {
            for (String str : res) writer.printf("%s\n", str);
            res.clear();
            sb = new StringBuilder("");
        }

    }
}


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.NoSuchElementException;

/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            pass System Test!
*/

public class D {
  private static class Task {
    final long MOD = (int) 1e9 + 7;
    long modPow(long x, long e) {
      long ret = 1;
      long cur = x;
      while (e > 0) {
        if ((e & 1) != 0) ret = (ret * cur) % MOD;
        cur = (cur * cur) % MOD;
        e /= 2;
      }
      return ret;
    }

    void solve(FastScanner in, PrintWriter out) {
      int N = in.nextInt();
      int[] to = new int[N];
      for (int i = 0; i < N; i++) {
        to[i] = in.nextInt() - 1;
      }

      int[] vis = new int[N];
      Arrays.fill(vis, -1);
      int cur = 0;
      int d;
      for (d = 0; ; d++) {
        if (vis[cur] >= 0) {
          d -= vis[cur];
          break;
        }
        vis[cur] = d;
        cur = to[cur];
      }

      long ans = modPow(2, N);
      ans -= modPow(2, N - d + 1);
      if (ans < 0) ans += MOD;
      out.println(ans);
    }
  }

  /**
   * ここから下はテンプレートです。
   */
  public static void main(String[] args) {
    OutputStream outputStream = System.out;
    FastScanner in = new FastScanner();
    PrintWriter out = new PrintWriter(outputStream);
    Task solver = new Task();
    solver.solve(in, out);
    out.close();
  }
  private static class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int bufferLength = 0;

    private boolean hasNextByte() {
      if (ptr < bufferLength) {
        return true;
      } else {
        ptr = 0;
        try {
          bufferLength = in.read(buffer);
        } catch (IOException e) {
          e.printStackTrace();
        }
        if (bufferLength <= 0) {
          return false;
        }
      }
      return true;
    }

    private int readByte() {
      if (hasNextByte()) return buffer[ptr++];
      else return -1;
    }

    private static boolean isPrintableChar(int c) {
      return 33 <= c && c <= 126;
    }

    private void skipUnprintable() {
      while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
    }

    boolean hasNext() {
      skipUnprintable();
      return hasNextByte();
    }

    public String next() {
      if (!hasNext()) throw new NoSuchElementException();
      StringBuilder sb = new StringBuilder();
      int b = readByte();
      while (isPrintableChar(b)) {
        sb.appendCodePoint(b);
        b = readByte();
      }
      return sb.toString();
    }

    long nextLong() {
      if (!hasNext()) throw new NoSuchElementException();
      long n = 0;
      boolean minus = false;
      int b = readByte();
      if (b == '-') {
        minus = true;
        b = readByte();
      }
      if (b < '0' || '9' < b) {
        throw new NumberFormatException();
      }
      while (true) {
        if ('0' <= b && b <= '9') {
          n *= 10;
          n += b - '0';
        } else if (b == -1 || !isPrintableChar(b)) {
          return minus ? -n : n;
        } else {
          throw new NumberFormatException();
        }
        b = readByte();
      }
    }

    double nextDouble() {
      return Double.parseDouble(next());
    }

    double[] nextDoubleArray(int n) {
      double[] array = new double[n];
      for (int i = 0; i < n; i++) {
        array[i] = nextDouble();
      }
      return array;
    }

    double[][] nextDoubleMap(int n, int m) {
      double[][] map = new double[n][];
      for (int i = 0; i < n; i++) {
        map[i] = nextDoubleArray(m);
      }
      return map;
    }

    public int nextInt() {
      return (int) nextLong();
    }

    public int[] nextIntArray(int n) {
      int[] array = new int[n];
      for (int i = 0; i < n; i++) array[i] = nextInt();
      return array;
    }

    public long[] nextLongArray(int n) {
      long[] array = new long[n];
      for (int i = 0; i < n; i++) array[i] = nextLong();
      return array;
    }

    public String[] nextStringArray(int n) {
      String[] array = new String[n];
      for (int i = 0; i < n; i++) array[i] = next();
      return array;
    }

    public char[][] nextCharMap(int n) {
      char[][] array = new char[n][];
      for (int i = 0; i < n; i++) array[i] = next().toCharArray();
      return array;
    }
  }
}


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.NoSuchElementException;

/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            pass System Test!
*/

public class D {
  private static class Task {
    final long MOD = (int) 1e9 + 7;
    long modPow(long x, long e) {
      long ret = 1;
      long cur = x;
      while (e > 0) {
        if ((e & 1) != 0) ret = (ret * cur) % MOD;
        cur = (cur * cur) % MOD;
        e /= 2;
      }
      return ret;
    }

    void solve(FastScanner in, PrintWriter out) throws Exception {
      int N = in.nextInt();
      int[] to = new int[N];
      for (int i = 0; i < N; i++) {
        to[i] = in.nextInt() - 1;
      }
      ArrayList<ArrayList<Integer>> G = new ArrayList<>();
      for (int i = 0; i < N; i++) G.add(new ArrayList<>());
      for (int i = 0; i < N; i++) {
        G.get(i).add(to[i]);
      }

      StronglyConnectedComponents scc = new StronglyConnectedComponents();
      int[] cmp = scc.decompose(G);
      int size = scc.k;

      int[] cnt = new int[size];
      for (int c : cmp) cnt[c]++;

      long ans = 1;
      for (int c : cnt) {
        if (c > 1) ans = (ans * (modPow(2, c) - 2 + MOD)) % MOD;
        else ans = (ans * 2) % MOD;
      }
      out.println(ans);
    }
    class StronglyConnectedComponents {
      int k;
      int[] decompose(ArrayList<ArrayList<Integer>> G) {
        ArrayList<Integer> vs = new ArrayList<>();
        int V = G.size();
        int[] cmp = new int[V];

        ArrayList<ArrayList<Integer>> rG = new ArrayList<>(V);
        for (int i = 0; i < V; i++) rG.add(new ArrayList<Integer>());
        for (int i = 0; i < V; i++) for (int v : G.get(i)) rG.get(v).add(i);
        boolean[] used = new boolean[V];

        ArrayDeque<Integer> stack = new ArrayDeque<>();
        boolean[] added = new boolean[V];
        for (int i = 0; i < V; i++)
          if (!used[i]) {
            stack.addFirst(i);
            while (!stack.isEmpty()) {
              int v = stack.peekFirst();
              used[v] = true;
              boolean pushed = false;
              for (int j = G.get(v).size() - 1; j >= 0; j--) {
                int u = G.get(v).get(j);
                if (!used[u]) {
                  stack.addFirst(u);
                  pushed = true;
                }
              }
              if (!pushed) {
                stack.pollFirst();
                if (!added[v]) {
                  vs.add(v);
                  added[v] = true;
                }
              }
            }
          }

        used = new boolean[V];
        k = 0;
        Collections.reverse(vs);
        for (int i : vs)
          if (!used[i]) {
            stack.push(i);
            used[i] = true;
            cmp[i] = k;
            while (!stack.isEmpty()) {
              int v = stack.peek();
              boolean pushed = false;
              for (int u : rG.get(v))
                if (!used[u]) {
                  used[u] = true;
                  cmp[u] = k;
                  stack.push(u);
                  pushed = true;
                }
              if (!pushed) stack.pop();
            }
            k++;
          }
        return cmp;
      }
    }
  }

  /**
   * ここから下はテンプレートです。
   */
  public static void main(String[] args) throws Exception {
    OutputStream outputStream = System.out;
    FastScanner in = new FastScanner();
    PrintWriter out = new PrintWriter(outputStream);
    Task solver = new Task();
    solver.solve(in, out);
    out.close();
  }
  private static class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int bufferLength = 0;

    private boolean hasNextByte() {
      if (ptr < bufferLength) {
        return true;
      } else {
        ptr = 0;
        try {
          bufferLength = in.read(buffer);
        } catch (IOException e) {
          e.printStackTrace();
        }
        if (bufferLength <= 0) {
          return false;
        }
      }
      return true;
    }

    private int readByte() {
      if (hasNextByte()) return buffer[ptr++];
      else return -1;
    }

    private static boolean isPrintableChar(int c) {
      return 33 <= c && c <= 126;
    }

    private void skipUnprintable() {
      while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
    }

    boolean hasNext() {
      skipUnprintable();
      return hasNextByte();
    }

    public String next() {
      if (!hasNext()) throw new NoSuchElementException();
      StringBuilder sb = new StringBuilder();
      int b = readByte();
      while (isPrintableChar(b)) {
        sb.appendCodePoint(b);
        b = readByte();
      }
      return sb.toString();
    }

    long nextLong() {
      if (!hasNext()) throw new NoSuchElementException();
      long n = 0;
      boolean minus = false;
      int b = readByte();
      if (b == '-') {
        minus = true;
        b = readByte();
      }
      if (b < '0' || '9' < b) {
        throw new NumberFormatException();
      }
      while (true) {
        if ('0' <= b && b <= '9') {
          n *= 10;
          n += b - '0';
        } else if (b == -1 || !isPrintableChar(b)) {
          return minus ? -n : n;
        } else {
          throw new NumberFormatException();
        }
        b = readByte();
      }
    }

    double nextDouble() {
      return Double.parseDouble(next());
    }

    double[] nextDoubleArray(int n) {
      double[] array = new double[n];
      for (int i = 0; i < n; i++) {
        array[i] = nextDouble();
      }
      return array;
    }

    double[][] nextDoubleMap(int n, int m) {
      double[][] map = new double[n][];
      for (int i = 0; i < n; i++) {
        map[i] = nextDoubleArray(m);
      }
      return map;
    }

    public int nextInt() {
      return (int) nextLong();
    }

    public int[] nextIntArray(int n) {
      int[] array = new int[n];
      for (int i = 0; i < n; i++) array[i] = nextInt();
      return array;
    }

    public long[] nextLongArray(int n) {
      long[] array = new long[n];
      for (int i = 0; i < n; i++) array[i] = nextLong();
      return array;
    }

    public String[] nextStringArray(int n) {
      String[] array = new String[n];
      for (int i = 0; i < n; i++) array[i] = next();
      return array;
    }

    public char[][] nextCharMap(int n) {
      char[][] array = new char[n][];
      for (int i = 0; i < n; i++) array[i] = next().toCharArray();
      return array;
    }
  }
}


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.NoSuchElementException;

/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            pass System Test!
*/

public class D {
  private static class Task {
    final long MOD = (int) 1e9 + 7;
    long modPow(long x, long e) {
      long ret = 1;
      long cur = x;
      while (e > 0) {
        if ((e & 1) != 0) ret = (ret * cur) % MOD;
        cur = (cur * cur) % MOD;
        e /= 2;
      }
      return ret;
    }

    void solve(FastScanner in, PrintWriter out) throws Exception {
      int N = in.nextInt();
      int[] to = new int[N];
      for (int i = 0; i < N; i++) {
        to[i] = in.nextInt() - 1;
      }

      long[] vis = new long[N];
      Arrays.fill(vis, -1);
      ArrayList<Long> dists = new ArrayList<>();
      for (int i = 0; i < N; i++) {
        if (vis[i] >= 0) continue;
        int cur = i;
        long d;
        for (d = 0; ; d++) {
          if (vis[cur] >= 0) {
            d -= vis[cur];
            if (d > 1) dists.add(d);
            break;
          }
          vis[cur] = d;
          cur = to[cur];
        }
      }
      long ans = 1;
      for (long d : dists) {
        long m = modPow(2, d) - 2;
        if (m < 0) m += MOD;
        ans *= m;
        ans %= MOD;
        N -= d;
      }
      ans *= modPow(2, N);
      ans %= MOD;
      out.println(ans);

    }
  }

  /**
   * ここから下はテンプレートです。
   */
  public static void main(String[] args) throws Exception {
    OutputStream outputStream = System.out;
    FastScanner in = new FastScanner();
    PrintWriter out = new PrintWriter(outputStream);
    Task solver = new Task();
    solver.solve(in, out);
    out.close();
  }
  private static class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int bufferLength = 0;

    private boolean hasNextByte() {
      if (ptr < bufferLength) {
        return true;
      } else {
        ptr = 0;
        try {
          bufferLength = in.read(buffer);
        } catch (IOException e) {
          e.printStackTrace();
        }
        if (bufferLength <= 0) {
          return false;
        }
      }
      return true;
    }

    private int readByte() {
      if (hasNextByte()) return buffer[ptr++];
      else return -1;
    }

    private static boolean isPrintableChar(int c) {
      return 33 <= c && c <= 126;
    }

    private void skipUnprintable() {
      while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
    }

    boolean hasNext() {
      skipUnprintable();
      return hasNextByte();
    }

    public String next() {
      if (!hasNext()) throw new NoSuchElementException();
      StringBuilder sb = new StringBuilder();
      int b = readByte();
      while (isPrintableChar(b)) {
        sb.appendCodePoint(b);
        b = readByte();
      }
      return sb.toString();
    }

    long nextLong() {
      if (!hasNext()) throw new NoSuchElementException();
      long n = 0;
      boolean minus = false;
      int b = readByte();
      if (b == '-') {
        minus = true;
        b = readByte();
      }
      if (b < '0' || '9' < b) {
        throw new NumberFormatException();
      }
      while (true) {
        if ('0' <= b && b <= '9') {
          n *= 10;
          n += b - '0';
        } else if (b == -1 || !isPrintableChar(b)) {
          return minus ? -n : n;
        } else {
          throw new NumberFormatException();
        }
        b = readByte();
      }
    }

    double nextDouble() {
      return Double.parseDouble(next());
    }

    double[] nextDoubleArray(int n) {
      double[] array = new double[n];
      for (int i = 0; i < n; i++) {
        array[i] = nextDouble();
      }
      return array;
    }

    double[][] nextDoubleMap(int n, int m) {
      double[][] map = new double[n][];
      for (int i = 0; i < n; i++) {
        map[i] = nextDoubleArray(m);
      }
      return map;
    }

    public int nextInt() {
      return (int) nextLong();
    }

    public int[] nextIntArray(int n) {
      int[] array = new int[n];
      for (int i = 0; i < n; i++) array[i] = nextInt();
      return array;
    }

    public long[] nextLongArray(int n) {
      long[] array = new long[n];
      for (int i = 0; i < n; i++) array[i] = nextLong();
      return array;
    }

    public String[] nextStringArray(int n) {
      String[] array = new String[n];
      for (int i = 0; i < n; i++) array[i] = next();
      return array;
    }

    public char[][] nextCharMap(int n) {
      char[][] array = new char[n][];
      for (int i = 0; i < n; i++) array[i] = next().toCharArray();
      return array;
    }
  }
}


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.NoSuchElementException;

/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            pass System Test!
*/

public class D {
  private static class Task {
    final int MOD = (int) 1e9 + 7;
    long modpow(long x, long e) {
      long ret = 1;
      long cur = x;
      while (e > 0) {
        if ((e & 1) != 0) ret = (ret * cur) % MOD;
        cur = (cur * cur) % MOD;
        e /= 2;
      }
      return ret;
    }
    ArrayList<Integer>[] undirected;

    ArrayList<Integer> cycle = new ArrayList<>();
    boolean[] vis;
    int dfs(int v, int p) {
      if (vis[v]) {
        cycle.add(v);
        return v;
      }
      vis[v] = true;
      for (int u : undirected[v]) {
        if (u == p) continue;
        int result = dfs(u, v);
        if (result >= 0) {
          cycle.add(v);
          if (result == v) return -1;
          return result;
        }
      }
      return -1;
    }

    void solve(FastScanner in, PrintWriter out) {
      int N = in.nextInt();
      int[] to = new int[N];
      undirected = new ArrayList[N];
      for (int i = 0; i < N; i++) undirected[i] = new ArrayList<>();
      for (int i = 0; i < N; i++) {
        to[i] = in.nextInt() - 1;
        undirected[i].add(to[i]);
        undirected[to[i]].add(i);
      }

      for (int i = 0; i < N; i++) {
        if (to[to[i]] == i) {
          out.println(modpow(2, N - 1) % MOD);
          return;
        }
      }

      vis = new boolean[N];
      dfs(0, -1);

      int cycleSize = cycle.size() - 1;

      long ans = modpow(2, cycleSize);
      ans = (ans - 2 + MOD) % MOD;
      ans *= modpow(2, N - cycleSize);
      ans %= MOD;
      out.println(ans);
    }
  }

  /**
   * ここから下はテンプレートです。
   */
  public static void main(String[] args) {
    OutputStream outputStream = System.out;
    FastScanner in = new FastScanner();
    PrintWriter out = new PrintWriter(outputStream);
    Task solver = new Task();
    solver.solve(in, out);
    out.close();
  }
  private static class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int bufferLength = 0;

    private boolean hasNextByte() {
      if (ptr < bufferLength) {
        return true;
      } else {
        ptr = 0;
        try {
          bufferLength = in.read(buffer);
        } catch (IOException e) {
          e.printStackTrace();
        }
        if (bufferLength <= 0) {
          return false;
        }
      }
      return true;
    }

    private int readByte() {
      if (hasNextByte()) return buffer[ptr++];
      else return -1;
    }

    private static boolean isPrintableChar(int c) {
      return 33 <= c && c <= 126;
    }

    private void skipUnprintable() {
      while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
    }

    boolean hasNext() {
      skipUnprintable();
      return hasNextByte();
    }

    public String next() {
      if (!hasNext()) throw new NoSuchElementException();
      StringBuilder sb = new StringBuilder();
      int b = readByte();
      while (isPrintableChar(b)) {
        sb.appendCodePoint(b);
        b = readByte();
      }
      return sb.toString();
    }

    long nextLong() {
      if (!hasNext()) throw new NoSuchElementException();
      long n = 0;
      boolean minus = false;
      int b = readByte();
      if (b == '-') {
        minus = true;
        b = readByte();
      }
      if (b < '0' || '9' < b) {
        throw new NumberFormatException();
      }
      while (true) {
        if ('0' <= b && b <= '9') {
          n *= 10;
          n += b - '0';
        } else if (b == -1 || !isPrintableChar(b)) {
          return minus ? -n : n;
        } else {
          throw new NumberFormatException();
        }
        b = readByte();
      }
    }

    double nextDouble() {
      return Double.parseDouble(next());
    }

    double[] nextDoubleArray(int n) {
      double[] array = new double[n];
      for (int i = 0; i < n; i++) {
        array[i] = nextDouble();
      }
      return array;
    }

    double[][] nextDoubleMap(int n, int m) {
      double[][] map = new double[n][];
      for (int i = 0; i < n; i++) {
        map[i] = nextDoubleArray(m);
      }
      return map;
    }

    public int nextInt() {
      return (int) nextLong();
    }

    public int[] nextIntArray(int n) {
      int[] array = new int[n];
      for (int i = 0; i < n; i++) array[i] = nextInt();
      return array;
    }

    public long[] nextLongArray(int n) {
      long[] array = new long[n];
      for (int i = 0; i < n; i++) array[i] = nextLong();
      return array;
    }

    public String[] nextStringArray(int n) {
      String[] array = new String[n];
      for (int i = 0; i < n; i++) array[i] = next();
      return array;
    }

    public char[][] nextCharMap(int n) {
      char[][] array = new char[n][];
      for (int i = 0; i < n; i++) array[i] = next().toCharArray();
      return array;
    }
  }
}


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.NoSuchElementException;

/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            pass System Test!
*/

public class D {
  private static class Task {
    final int MOD = (int) 1e9 + 7;
    long modPow(long x, long e) {
      long ret = 1;
      long cur = x;
      while (e > 0) {
        if ((e & 1) != 0) ret = (ret * cur) % MOD;
        cur = (cur * cur) % MOD;
        e /= 2;
      }
      return ret;
    }

    void solve(FastScanner in, PrintWriter out) {
      int N = in.nextInt();
      int[] to = new int[N];
      for (int i = 0; i < N; i++) {
        to[i] = in.nextInt() - 1;
      }

      int[] vis = new int[N];
      Arrays.fill(vis, -1);
      int cur = 0;
      int d;
      for (d = 0; ; d++) {
        if (vis[cur] >= 0) {
          d -= vis[cur];
          break;
        }
        vis[cur] = d;
        cur = to[cur];
      }

      long ans = modPow(2, d) - 2;
      if (ans < 0) ans += MOD;
      ans *= modPow(2, N - d);
      ans %= MOD;
      out.println(ans);
    }
  }

  /**
   * ここから下はテンプレートです。
   */
  public static void main(String[] args) {
    OutputStream outputStream = System.out;
    FastScanner in = new FastScanner();
    PrintWriter out = new PrintWriter(outputStream);
    Task solver = new Task();
    solver.solve(in, out);
    out.close();
  }
  private static class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int bufferLength = 0;

    private boolean hasNextByte() {
      if (ptr < bufferLength) {
        return true;
      } else {
        ptr = 0;
        try {
          bufferLength = in.read(buffer);
        } catch (IOException e) {
          e.printStackTrace();
        }
        if (bufferLength <= 0) {
          return false;
        }
      }
      return true;
    }

    private int readByte() {
      if (hasNextByte()) return buffer[ptr++];
      else return -1;
    }

    private static boolean isPrintableChar(int c) {
      return 33 <= c && c <= 126;
    }

    private void skipUnprintable() {
      while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
    }

    boolean hasNext() {
      skipUnprintable();
      return hasNextByte();
    }

    public String next() {
      if (!hasNext()) throw new NoSuchElementException();
      StringBuilder sb = new StringBuilder();
      int b = readByte();
      while (isPrintableChar(b)) {
        sb.appendCodePoint(b);
        b = readByte();
      }
      return sb.toString();
    }

    long nextLong() {
      if (!hasNext()) throw new NoSuchElementException();
      long n = 0;
      boolean minus = false;
      int b = readByte();
      if (b == '-') {
        minus = true;
        b = readByte();
      }
      if (b < '0' || '9' < b) {
        throw new NumberFormatException();
      }
      while (true) {
        if ('0' <= b && b <= '9') {
          n *= 10;
          n += b - '0';
        } else if (b == -1 || !isPrintableChar(b)) {
          return minus ? -n : n;
        } else {
          throw new NumberFormatException();
        }
        b = readByte();
      }
    }

    double nextDouble() {
      return Double.parseDouble(next());
    }

    double[] nextDoubleArray(int n) {
      double[] array = new double[n];
      for (int i = 0; i < n; i++) {
        array[i] = nextDouble();
      }
      return array;
    }

    double[][] nextDoubleMap(int n, int m) {
      double[][] map = new double[n][];
      for (int i = 0; i < n; i++) {
        map[i] = nextDoubleArray(m);
      }
      return map;
    }

    public int nextInt() {
      return (int) nextLong();
    }

    public int[] nextIntArray(int n) {
      int[] array = new int[n];
      for (int i = 0; i < n; i++) array[i] = nextInt();
      return array;
    }

    public long[] nextLongArray(int n) {
      long[] array = new long[n];
      for (int i = 0; i < n; i++) array[i] = nextLong();
      return array;
    }

    public String[] nextStringArray(int n) {
      String[] array = new String[n];
      for (int i = 0; i < n; i++) array[i] = next();
      return array;
    }

    public char[][] nextCharMap(int n) {
      char[][] array = new char[n][];
      for (int i = 0; i < n; i++) array[i] = next().toCharArray();
      return array;
    }
  }
}


import java.io.*;
import java.util.*;

import static java.lang.Math.*;

public class Main {
    FastScanner in;
    PrintWriter out;

    static final String FILE = "";

    public static final long MOD = (long) 1e9 + 7;

    List<Integer>[] graph;
    boolean[] used;
    int[] time;
    int size = -1;
    int cnt = 0;

    void dfs(int v, int last, int t) {
        if (used[v]) {
            if (size == -1)
                size = t - time[v];
            return;
        }
        cnt++;
        time[v] = t;
        used[v] = true;
        for (Integer it : graph[v]) {
            if (it == last) {
                last = -1;
                continue;
            }
            dfs(it, v, t + 1);
        }
    }

    public void solve() {
        int n = in.nextInt();
        graph = new List[n];
        for (int i = 0; i < n; i++)
            graph[i] = new ArrayList<>();
        used = new boolean[n];
        time = new int[n];
        for (int i = 0; i < n; i++) {
            int to = in.nextInt() - 1;
            graph[i].add(to);
            graph[to].add(i);
        }

        long ans = 1;
        for (int z = 0; z < n; z++) {
            cnt = 0;
            size = -1;
            if (used[z])
                continue;
            dfs(z, z, 0);
            long a = 1, b = 1;
            for (int i = 0; i < cnt - size; i++) {
                a <<= 1;
                a %= MOD;
            }
            for (int i = 0; i < size; i++) {
                b <<= 1;
                b %= MOD;
            }
            b -= 2;
            b += MOD;
            b %= MOD;
            a *= b;
            a %= MOD;
            ans *= a;
            ans %= MOD;
        }
        out.print(ans);
    }

    public void run() {
        if (FILE.equals("")) {
            in = new FastScanner(System.in);
            out = new PrintWriter(System.out);
        } else {
            try {
                in = new FastScanner(new FileInputStream(FILE +
                        ".in"));
                out = new PrintWriter(new FileOutputStream(FILE +
                        ".out"));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        solve();
        out.close();
    }

    public static void main(String[] args) {
        (new Main()).run();
    }

    class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(InputStream is) {
            br = new BufferedReader(new InputStreamReader(is));
        }

        public String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
                return "";
            }
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public float nextFloat() {
            return Float.parseFloat(next());
        }
    }

}


import java.io.*;
import java.util.*;

import static java.lang.Math.*;

public class Main {
    FastScanner in;
    PrintWriter out;

    static final String FILE = "";

    public static final long MOD = (long) 1e9 + 7;

    List<Integer>[] graph;
    boolean[] used;
    int[] time;
    int size = -1;

    void dfs(int v, int last, int t) {
        if (size != -1)
            return;
        if (used[v]) {
            size = t - time[v];
            return;
        }
        time[v] = t;
        used[v] = true;
        for (Integer it : graph[v]) {
            if (it == last) {
                last = -1;
                continue;
            }
            dfs(it, v, t + 1);
        }
    }

    public void solve() {
        int n = in.nextInt();
        graph = new List[n];
        for (int i = 0; i < n; i++)
            graph[i] = new ArrayList<>();
        used = new boolean[n];
        time = new int[n];
        for (int i = 0; i < n; i++) {
            int to = in.nextInt() - 1;
            graph[i].add(to);
            graph[to].add(i);
        }
        dfs(0, 0, 0);
        long a = 1, b = 1;
        for (int i = 0; i < n - size; i++) {
            a <<= 1;
            a %= MOD;
        }
        for (int i = 0; i < size; i++) {
            b <<= 1;
            b %= MOD;
        }
        b -= 2;
        b += MOD;
        b %= MOD;
        a *= b;
        a %= MOD;
        out.print(a);
    }

    public void run() {
        if (FILE.equals("")) {
            in = new FastScanner(System.in);
            out = new PrintWriter(System.out);
        } else {
            try {
                in = new FastScanner(new FileInputStream(FILE +
                        ".in"));
                out = new PrintWriter(new FileOutputStream(FILE +
                        ".out"));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        solve();
        out.close();
    }

    public static void main(String[] args) {
        (new Main()).run();
    }

    class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(InputStream is) {
            br = new BufferedReader(new InputStreamReader(is));
        }

        public String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
                return "";
            }
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public float nextFloat() {
            return Float.parseFloat(next());
        }
    }

}


import java.io.*;
import java.util.*;
import java.math.*;
import static java.lang.Math.*;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Double.parseDouble;
import static java.lang.String.*;

public class Main {

    static int MOD = (int)1e9+7;
    static List<Integer> [] g;
    static int [] dfs_num,dfs_low;
    static boolean [] vis;
    static Stack<Integer> s;
    static boolean [] e;
    static int n,dfs_cnt,num_scc;
    static List<Integer> cycle;

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder out = new StringBuilder();
        StringTokenizer tk;

        n = parseInt(in.readLine());
        e = new boolean[n];
        g = new List[n];
        for(int i=0; i<n; i++)
            g[i] = new ArrayList<>();

        dfs_cnt = num_scc = 0;

        vis = new boolean[n];
        dfs_num = new int[n];
        dfs_low = new int[n];
        Arrays.fill(dfs_num, -1);
        s = new Stack<>();
        cycle = new ArrayList<>();

        tk = new StringTokenizer(in.readLine());
        for(int i=0; i<n; i++) {
            int x = parseInt(tk.nextToken())-1;
            g[i].add(x);
            //System.out.println(i+" , "+x);
        }

        for(int i=0; i<n; i++)
            if(dfs_num[i]==-1)
                dfs(i);

        int ans = 1;

        for(int x : cycle) {
            int y = binPow(2,x)-2+MOD;
            if(y >= MOD) y -= MOD;
            ans = multi(ans, y);
        }

        for(int i=0; i<n; i++)
            if(!e[i])
                ans = multi(ans,2);

        System.out.println(ans);
    }

    static void dfs(int u) {
        dfs_low[u] = dfs_num[u] = dfs_cnt++;
        s.add(u);
        vis[u] = true;

        for(int v : g[u]) {
            if(dfs_num[v]==-1)
                dfs(v);
            if(vis[v])
                dfs_low[u] = min(dfs_low[u],dfs_low[v]);

            if(dfs_low[u]==dfs_num[u]) {
                int cnt = 0;
                while(true) {
                    int x = s.pop();
                    vis[x] = false;
                    cnt++;
                    e[x] = true;
                    if(x==u) break;
                }
                if(cnt>1)
                    cycle.add(cnt);
                else e[u] = false;
            }
        }
    }

    static int binPow(int n,int p) {
        if(p==0)
            return 1;

        if(p%2==1)
            return multi(binPow(n,p-1),n);

        int res = binPow(n,p/2);

        return multi(res,res);
    }

    static int multi(int a,int b) {
        int r = 0;
        for(int i=0; i<b; i++) {
            r = (r+a);
            if(r >= MOD) r -= MOD;
        }

        return r;
    }

}


import java.io.*;
import java.util.*;
import java.math.*;
import static java.lang.Math.*;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Double.parseDouble;
import static java.lang.String.*;

public class Main {

    static int MOD = (int)1e9+7;
    static List<Integer> [] g;
    static int [] dfs_num,dfs_low;
    static boolean [] vis;
    static Stack<Integer> s;
    static boolean [] e;
    static int n,dfs_cnt,num_scc;
    static List<Integer> cycle;

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder out = new StringBuilder();
        StringTokenizer tk;

        n = parseInt(in.readLine());
        e = new boolean[n];
        g = new List[n];
        for(int i=0; i<n; i++)
            g[i] = new ArrayList<>();

        dfs_cnt = num_scc = 0;

        vis = new boolean[n];
        dfs_num = new int[n];
        dfs_low = new int[n];
        Arrays.fill(dfs_num, -1);
        s = new Stack<>();
        cycle = new ArrayList<>();

        tk = new StringTokenizer(in.readLine());
        for(int i=0; i<n; i++) {
            int x = parseInt(tk.nextToken())-1;
            g[i].add(x);
            //System.out.println(i+" , "+x);
        }

        for(int i=0; i<n; i++)
            if(dfs_num[i]==-1)
                dfs(i);

        int ans = 1;

        for(int x : cycle) {
            int y = binPow(2,x)-2+MOD;
            if(y >= MOD) y -= MOD;
            ans = multi(ans, y);
        }

        for(int i=0; i<n; i++)
            if(!e[i])
                ans = multi(ans,2);

        System.out.println(ans);
    }

    static void dfs(int u) {
        dfs_low[u] = dfs_num[u] = dfs_cnt++;
        s.add(u);
        vis[u] = true;

        for(int v : g[u]) {
            if(dfs_num[v]==-1)
                dfs(v);
            if(vis[v])
                dfs_low[u] = min(dfs_low[u],dfs_low[v]);

            if(dfs_low[u]==dfs_num[u]) {
                int cnt = 0;
                while(true) {
                    int x = s.pop();
                    vis[x] = false;
                    cnt++;
                    e[x] = true;
                    if(x==u) break;
                }
                if(cnt>1)
                    cycle.add(cnt);
                else e[u] = false;
            }
        }
    }

    static int binPow(int n,int p) {
        if(p==0)
            return 1;

        if(p%2==1)
            return multi(binPow(n,p-1),n);

        int res = binPow(n,p/2);

        return multi(res,res);
    }

    static int multi(int a,int b) {
        long r = (long)a*(long)b;

        return (int)(r%MOD);
    }

}


import java.io.*;
import java.util.*;
import java.math.*;
import static java.lang.Math.*;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Double.parseDouble;
import static java.lang.String.*;

public class Main {

    static int MOD = (int)1e9+7;
    static List<Integer> [] g;
    static int [] dfs_num,dfs_low;
    static boolean [] vis;
    static Stack<Integer> s;
    static boolean [] e;
    static int n,dfs_cnt,num_scc;
    static List<Integer> cycle;

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder out = new StringBuilder();
        StringTokenizer tk;

        n = parseInt(in.readLine());
        e = new boolean[n];
        g = new List[n];
        for(int i=0; i<n; i++)
            g[i] = new ArrayList<>();

        dfs_cnt = num_scc = 0;

        vis = new boolean[n];
        dfs_num = new int[n];
        dfs_low = new int[n];
        Arrays.fill(dfs_num, -1);
        s = new Stack<>();
        cycle = new ArrayList<>();

        tk = new StringTokenizer(in.readLine());
        for(int i=0; i<n; i++) {
            int x = parseInt(tk.nextToken())-1;
            g[i].add(x);
            //System.out.println(i+" , "+x);
        }

        for(int i=0; i<n; i++)
            if(dfs_num[i]==-1)
                dfs(i);

        int ans = 1;

        for(int x : cycle)
            ans = multi(ans, (binPow(2,x)-2+MOD)%MOD);

        for(int i=0; i<n; i++)
            if(!e[i])
                ans = multi(ans,2);

        System.out.println(ans);
    }

    static void dfs(int u) {
        dfs_low[u] = dfs_num[u] = dfs_cnt++;
        s.add(u);
        vis[u] = true;

        for(int v : g[u]) {
            if(dfs_num[v]==-1)
                dfs(v);
            if(vis[v])
                dfs_low[u] = min(dfs_low[u],dfs_low[v]);

            if(dfs_low[u]==dfs_num[u]) {
                int cnt = 0;
                while(true) {
                    int x = s.pop();
                    vis[x] = false;
                    cnt++;
                    e[x] = true;
                    if(x==u) break;
                }
                if(cnt>1)
                    cycle.add(cnt);
                else e[u] = false;
            }
        }
    }

    static int binPow(int n,int p) {
        if(p==0)
            return 1;

        if(p%2==1)
            return multi(binPow(n,p-1),n);

        int res = binPow(n,p/2);

        return multi(res,res);
    }

    static int multi(int a,int b) {
        int r = 0;
        for(int i=0; i<b; i++)
            r = (r+a)%MOD;

        return r;
    }

}


import java.io.*;
import java.util.*;

public class test {
    int INF = (int)1e9;
    long MOD = 1000000007;

    long pow(long a, long n, long mod) {
        if (n == 0) {
            return 1;
        }
        long rs = 1;
        while (n > 0) {
            if (n % 2 == 1) {
                rs *= a;
            }
            rs %= mod;
            n >>= 1;
            a *= a;
            a %= mod;
        }
        return rs;
    }

    void solve(InputReader in, PrintWriter out) throws IOException {
        int n = in.nextInt();
        int[] a = new int[n];
        for(int i=0; i<n; i++) {
            a[i] = in.nextInt()-1;
        }
        boolean[] vis = new boolean[n];
        int[] dfi = new int[n];
        int dfid = 1;
        long rs = 1;
        for(int i=0; i<n; i++) {
            if(vis[i]) continue;
            int t = 0;
            int v = i;
            while(!vis[v]) {
                t++;
                dfi[v] = dfid++;
                vis[v] = true;
                v = a[v];
            }
            int te = Math.abs(dfi[i]-dfi[v]);
            int ce = t - te;
//            System.out.println("i: "+i+"  "+te+"  "+ce+"  "+t);
            if(te<=t) rs = (rs * pow(2, te, MOD))%MOD;
            else rs = (rs * pow(2, t, MOD))%MOD;
            if(ce>0) rs = (rs * (pow(2, ce, MOD)-2))%MOD;
        }
        out.println(rs);
    }

    public static void main(String[] args) throws IOException {
        if(args.length>0 && args[0].equalsIgnoreCase("d")) {
            DEBUG_FLAG = true;
        }
        InputReader in = new InputReader();
        PrintWriter out = new PrintWriter(System.out);
        int t = 1;//in.nextInt();
        long start = System.nanoTime();
        while(t-- >0) {
            new test().solve(in, out);
        }
        long end = System.nanoTime();
        debug("\nTime: " + (end-start)/1e6 + " \n\n");
        out.close();
    }

    static boolean DEBUG_FLAG = false;
    static void debug(String s) {
        if(DEBUG_FLAG) System.out.print(s);
    }

    static class InputReader {
        static BufferedReader br;
        static StringTokenizer st;

        public InputReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }
    }
}


#include<bits/stdc++.h>
#define up(j,k,i) for(i=j;i<k;i++)
#define down(j,k,i) for(i=j;i>k;i--)
#define M 1000000007
#define pp(n) printf("%lld\n",n)
#define ps(n) printf("%lld ",n)
#define is(n) scanf("%lld",&n)
#define id(n,m) scanf("%lld%lld",&n,&m);
#define it(n,m,k) scanf("%lld%lld%lld",&n,&m,&k);
#define ss(s) scanf("%s",s)
#define cool 0
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define pll pair<ll,ll>
#define db cout<<"######\n"
typedef long double ld;
typedef long long int ll;
using namespace std;
ll i,j,k,z,t,n,m,sum,ans,x,y,maxm=-1,minm=LLONG_MAX;
bool vis[105];
ll a[105];
ll b[105];
bool dfs(ll x)
{ll i;
if(x==y) return 1;
if(vis[x]) return 0;
vis[x]=1;
up(1,j+1,i)
if((a[i]>a[x]&&a[i]<b[x])||(b[i]>a[x]&&b[i]<b[x]))
if(dfs(i))
{vis[x]=0; return 1;}
vis[x]=0; return 0;
}
int main()
{
is(n);
while(n--)
{
it(t,x,y);
if(t==1)
a[++j]=x,b[j]=y;
else
cout<<(dfs(x)?"YES":"NO")<<endl;
}
}


#include<bits/stdc++.h>
#define up(j,k,i) for(i=j;i<k;i++)
#define down(j,k,i) for(i=j;i>k;i--)
#define M 1000000007
#define pp(n) printf("%lld\n",n)
#define ps(n) printf("%lld ",n)
#define is(n) scanf("%lld",&n)
#define id(n,m) scanf("%lld%lld",&n,&m);
#define it(n,m,k) scanf("%lld%lld%lld",&n,&m,&k);
#define ss(s) scanf("%s",s)
#define cool 0
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define pll pair<ll,ll>
#define db cout<<"######\n"
typedef long double ld;
typedef long long int ll;
using namespace std;
ll i,j,k,z,t,n,m,sum,ans,x,y,maxm=-1,minm=LLONG_MAX;
bool vis[105];
ll a[105];
ll b[105];
bool dfs(ll x)
{ll i;
if(x==y) return 1;
if(vis[x]) return 0;
vis[x]=1;
up(1,j+1,i)
if((a[x]>a[i]&&a[x]<b[i])||(b[x]>a[i]&&b[x]<b[i]))
if(dfs(i))
{vis[x]=0; return 1;}
vis[x]=0; return 0;
}
int main()
{
is(n);
while(n--)
{
it(t,x,y);
if(t==1)
a[++j]=x,b[j]=y;
else
cout<<(dfs(x)?"YES":"NO")<<endl;
}
}


#include <bits/stdc++.h>
using namespace std;

using ll = long long int;

vector<bool> vis(200, false);
vector<ll> g[200];
vector<pair<ll, ll>> arr(200);

bool okay(pair<ll, ll> a, pair<ll, ll> b) {
    return ((b.first < a.first && a.first < b.second) || (b.first < a.second && a.second < b.second));
}

void dfs(ll node) {
    vis[node] = true;
    for (auto adj : g[node]) {
        if (!vis[adj]) {
            dfs(adj);
        }
    }
}

int main() {
    ll n, cnt = 0;
    cin >> n;

    for (ll i = 0; i < n; i++) {
        ll op;
        cin >> op;
        if (op == 1) {
            ll x, y;
            cin >> x >> y;
            arr[cnt++] = {x, y};
            for (int j = 0; j < i; j++) {
                if (okay(arr[i], arr[j])) {
                    g[i].push_back(j);
                }
                if (okay(arr[j], arr[i])) {
                    g[j].push_back(i);
                }
            }
        } else {
            ll x, y;
            cin >> x >> y;
            x--, y--;
            for (int i = 0; i <= 105; i++) {
                vis[i] = false;
            }
            dfs(x);
            if (vis[y]) {
                cout << "YES" << endl;
            } else {
                cout << "NO" << endl;
            }
        }
    }
}

/*

20

1 1208 1583

1 -258 729

1 -409 1201

1 194 1938

1 -958 1575

1 -1466 1752
1 -1870 1881

1 -2002 2749

1 -2002 2984

1 -2002 3293
2 9 6
*/


#include <bits/stdc++.h>
using namespace std;

using ll = long long int;

vector<bool> vis(200, false);
vector<ll> g[200];
vector<pair<ll, ll>> arr(200);

bool okay(pair<ll, ll> a, pair<ll, ll> b) {
    return ((b.first < a.first && a.first < b.second) || (b.first < a.second && a.second < b.second));
}

void dfs(ll node) {
    vis[node] = true;
    for (auto adj : g[node]) {
        if (!vis[adj]) {
            dfs(adj);
        }
    }
}

int main() {
    ll n, cnt = 0;
    cin >> n;

    for (ll i = 0; i < n; i++) {
        ll op;
        cin >> op;
        if (op == 1) {
            ll x, y;
            cin >> x >> y;
            arr[cnt++] = {x, y};
            for (int j = 0; j < cnt; j++) {
                if (okay(arr[cnt - 1], arr[j])) {
                    g[cnt - 1].push_back(j);
                }
                if (okay(arr[j], arr[cnt - 1])) {
                    g[j].push_back(cnt - 1);
                }
            }
        } else {
            ll x, y;
            cin >> x >> y;
            x--, y--;
            for (int i = 0; i <= 105; i++) {
                vis[i] = false;
            }
            dfs(x);
            if (vis[y]) {
                cout << "YES" << endl;
            } else {
                cout << "NO" << endl;
            }
        }
    }
}

/*

20

1 1208 1583

1 -258 729

1 -409 1201

1 194 1938

1 -958 1575

1 -1466 1752
1 -1870 1881

1 -2002 2749

1 -2002 2984

1 -2002 3293
2 9 6
*/


#include <bits/stdc++.h>
using namespace std;

using ll = long long int;

vector<bool> vis(200, false);
vector<ll> g[200];
vector<pair<ll, ll>> arr(200);

bool okay(pair<ll, ll> a, pair<ll, ll> b) {
    return ((b.first < a.first && a.first < b.second) || (b.first < a.second && a.second < b.second));
}

void dfs(ll node) {
    vis[node] = true;
    for (auto adj : g[node]) {
        if (!vis[adj]) {
            dfs(adj);
        }
    }
}

int main() {
    ll n, cnt = 0;
    cin >> n;

    for (ll i = 0; i < n; i++) {
        ll op;
        cin >> op;
        if (op == 1) {
            ll x, y;
            cin >> x >> y;
            arr[cnt++] = {x, y};
            for (int j = 0; j <= cnt; j++) {
                if (okay(arr[cnt - 1], arr[j])) {
                    g[cnt - 1].push_back(j);
                }
                if (okay(arr[j], arr[cnt - 1])) {
                    g[j].push_back(cnt - 1);
                }
            }
        } else {
            ll x, y;
            cin >> x >> y;
            x--, y--;
            for (int i = 0; i <= 105; i++) {
                vis[i] = false;
            }
            dfs(x);
            if (vis[y]) {
                cout << "YES" << endl;
            } else {
                cout << "NO" << endl;
            }
        }
    }
}

/*

20

1 1208 1583

1 -258 729

1 -409 1201

1 194 1938

1 -958 1575

1 -1466 1752
1 -1870 1881

1 -2002 2749

1 -2002 2984

1 -2002 3293
2 9 6
*/


#include <bits/stdc++.h>
using namespace std;

using ll = long long int;

vector<bool> vis(200, false);
vector<ll> g[200];
vector<pair<ll, ll>> arr(200);

bool okay(pair<ll, ll> a, pair<ll, ll> b) {
    return ((b.first < a.first && a.first < b.second) || (b.first < a.second && a.second < b.second));
}

void dfs(ll node) {
    vis[node] = true;
    for (auto adj : g[node]) {
        if (!vis[adj]) {
            dfs(adj);
        }
    }
}

int main() {
    ll n;
    cin >> n;

    for (ll i = 0; i < n; i++) {
        ll op;
        cin >> op;
        if (op == 1) {
            ll x, y;
            cin >> x >> y;
            arr[i] = {x, y};
            for (int j = 0; j < i; j++) {
                if (okay(arr[i], arr[j])) {
                    g[i].push_back(j);
                }
                if (okay(arr[j], arr[i])) {
                    g[j].push_back(i);
                }
            }
        } else {
            ll x, y;
            cin >> x >> y;
            x--, y--;
            vis.assign(200, false);
            dfs(x);
            if (vis[y]) {
                cout << "YES" << endl;
            } else {
                cout << "NO" << endl;
            }
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vd vector<double>
#define rep(i,a,b) for(int i=a;i<b;++i)
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long long ll;
const int N=101;
bool reacheable(const ii&a,const ii&b){
    return((a.first>b.first&&a.first<b.second)||(a.second>b.first&&a.second<b.second));
}
ii A[N];
int main()
{ios_base::sync_with_stdio(false);cin.tie(NULL);
int n,k,x,y;
set<ii>s;
cin>>n;
rep(i,1,n+1){
    cin>>k>>x>>y;
  if(k==1){
      s.insert(mp(x,y));
       A[i]={x,y};

  }

  if(k==2){
      /*for(auto vt=s.begin();vt!=s.end();++vt){
         ii tmp=*vt;
            cout<<"("<<tmp.first<<","<<tmp.second<<")\n";
        }*/
     ii a=A[x];ii b=A[y];
     if(reacheable(a,b)) cout<<"YES\n";
     else{
         if(a.first>b.first){ii tmp=b;b=a;a=tmp;}
        auto pos=s.find(a);auto po=s.find(b);
        ii alpha=b;
        for(auto it=po;it!=pos;--it){
            ii tmp=*it;

            //cout<<"("<<tmp.first<<","<<tmp.second<<")\n";
            if(tmp==alpha)continue;
            if(reacheable(tmp,b)){
               b=tmp;
                //cout<<"("<<a.first<<","<<a.second<<")\n";
            }
            else{
                cout<<"NO\n";
                break;
            }
        }
       if(reacheable(a,b)) cout<<"YES\n";
       else cout<<"NO\n";
     }
  }
}
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define eb emplace_back

#define vd vector<double>
#define rep(i,a,b) for(int i=a;i<b;++i)
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef long long ll;
bool visited[101];

vvi adj(101);
ii A[101];

bool reacheable(const ii & a, const ii & b) {
    return((a.first > b.first&& a.first < b.second) || (a.second > b.first&& a.second < b.second));
}

void dfs(int u) {
    visited[u] = true;
    for (auto v : adj[u]) {
        if (!visited[v])dfs(v);
    }
}
int main()
{ios_base::sync_with_stdio(false);cin.tie(NULL);

    rep(kk, 0, 101) {
        visited[kk] = false;
    }
    int n, k, x, y;

    cin >> n;
    int next = 1;
    rep(i, 0, n) {
        cin >> k >> x >> y;
        if (k == 1) {

            A[next] = {x,y}; ++next;

        }
        rep(j, 1, next) {
            if (reacheable(A[j], A[next-1]))adj[j].eb(next-1);
            if(reacheable(A[next-1], A[j]))adj[next-1].eb(j);
        }

        if (k == 2) {

            dfs(x);
            if (visited[y])cout << "YES\n";
            else cout << "NO\n";
        }
        rep(kk, 0, 101) {
            visited[kk] = false;
        }
    }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vd vector<double>
#define rep(i,a,b) for(int i=a;i<b;++i)
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long long ll;
const int N=101;
bool reacheable(const ii&a,const ii&b){
    return((a.first>b.first&&a.first<b.second)||(a.second>b.first&&a.second<b.second));
}
ii A[N];
int main()
{ios_base::sync_with_stdio(false);cin.tie(NULL);
int n,k,x,y;
set<ii>s;
cin>>n;
int next=1;
rep(i,0,n){
    cin>>k>>x>>y;
  if(k==1){
      s.insert(mp(x,y));
       A[next]={x,y};++next;

  }

  if(k==2){
      /*for(auto vt=s.begin();vt!=s.end();++vt){
         ii tmp=*vt;
            cout<<"("<<tmp.first<<","<<tmp.second<<")\n";
        }*/
     ii a=A[x];ii b=A[y];
     if(a.first>b.first){ii tmp=b;b=a;a=tmp;}

     if(reacheable(a,b)||reacheable(b,a)) cout<<"YES\n";
     else{

        auto pos=s.find(a);auto po=s.find(b);
        ii alpha=b;
        for(auto it=po;it!=pos;--it){
            ii tmp=*it;

            //cout<<"("<<tmp.first<<","<<tmp.second<<")\n";
            if(tmp==alpha)continue;
            if(reacheable(tmp,b)||(reacheable(b,tmp))){
               b=tmp;
                //cout<<"("<<a.first<<","<<a.second<<")\n";
            }

        }
       if(reacheable(a,b)) cout<<"YES\n";
       else cout<<"NO\n";
     }
  }
}
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vd vector<double>
#define rep(i,a,b) for(int i=a;i<b;++i)
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long long ll;
const int N=101;
bool reacheable(const ii&a,const ii&b){
    return((a.first>b.first&&a.first<b.second)||(a.second>b.first&&a.second<b.second));
}
ii A[N];
int main()
{ios_base::sync_with_stdio(false);cin.tie(NULL);
int n,k,x,y;
set<ii>s;
cin>>n;
int next=1;
rep(i,0,n){
    cin>>k>>x>>y;
  if(k==1){
      s.insert(mp(x,y));
       A[next]={x,y};++next;

  }

  if(k==2){
      /*for(auto vt=s.begin();vt!=s.end();++vt){
         ii tmp=*vt;
            cout<<"("<<tmp.first<<","<<tmp.second<<")\n";
        }*/
     ii a=A[x];ii b=A[y];
     if(a.first>b.first){ii tmp=b;b=a;a=tmp;}

     if(reacheable(a,b)||reacheable(b,a)) cout<<"YES\n";
     else{

        auto pos=s.find(a);auto po=s.find(b);
        ii alpha=b;
        for(auto it=po;it!=pos;--it){
            ii tmp=*it;

            //cout<<"("<<tmp.first<<","<<tmp.second<<")\n";
            if(tmp==alpha)continue;
            if(reacheable(tmp,b)||(reacheable(b,tmp))){
               b=tmp;
                //cout<<"("<<a.first<<","<<a.second<<")\n";
            }
            else{
                cout<<"NO\n";
                break;
            }
        }
       if(reacheable(a,b)) cout<<"YES\n";
       else cout<<"NO\n";
     }
  }
}
  return 0;
}


//CppSpoj <CERC07K , Key Task>
/**Milan Topalovic
*Faculty of Computing Belgrade
*/
//#include <bits/stdc++.h>
//#include <unordered_map>
//#include <unordered_set>
#include <map>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <vector>
#include <queue>
#include <set>
//#include <deque>
#include <limits.h>
//#include <climits>
#include <float.h> //DBL_MAX
#include <string>
#include <cstring>
//#include <cstdlib>
//#include <time.h>
#include <utility>
#include <iomanip>

using namespace std;

#define endl '\n'
#define dendl "\n\n"
#define inf 1e9
#define inf2 1e7
#define inf3 1e13
#define cti(chr) (((chr) - ('0'))) //char to int
#define itc(inte) (((inte) + ('0')))
#define isd(chr) (((chr) >= ('0') && (chr) <= ('9')))
#define is_upper(chr) (((chr) >= ('A') && (chr) <= ('Z')))
#define switch_case(chr) (((chr)) >= ('a') ? ('A') + ((chr) - ('a')) : ('a') + ((chr) - ('A')))
#define isin(x , y , lx , ly , bx , by) (((x) >= (lx)) && ((x) < (bx)) && ((y) >= (ly))  && ((y) < (by)))
#define maxn 1e8
#define PI 3.14159265358979323846
#define gc getchar_unlocked

template <typename T>
void afill(T *arr , int size , T val) { fill(arr , arr + size , val); }

typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int , int> pii;
typedef pair<ll , ll> pll;
typedef priority_queue<int , vector<int> , less<int> > maxqueue;
typedef priority_queue<int , vector<int> , greater<int> > minqueue;
long long mod = 1e9 + 7;
double eps = 1e-9;

int n , t , m , q , k;
int ans[101][101];
vector<pii> vec;
//You can move from interval (a, b) from our set to interval (c, d) from our set if and only if c < a < d or c < b < d.
void insert(pii pr , int a , int b , int newi , int curri)
{
   if((pr.first < a && a < pr.second) || (pr.first < b && b < pr.second))
   {
      ans[newi][curri] = 1;
      for(int i = 1 ; i <= vec.size() ; ++i)
         ans[newi][i] |= ans[curri][i];
   }
}

int main()
{
   ios_base::sync_with_stdio(0);

   cin >> n;

   for(int i = 1 ; i <= n ; ++i)
   {
      int a , b;
      cin >> q >> a >> b;
      if(q == 2)
      {
         if(ans[a][b])
            cout << "YES" << endl;
         else
            cout << "NO" << endl;
      } else {
         for(int j = 1 ; j <= vec.size() ; ++j)
            insert(vec[j - 1] , a , b , i , j);
         vec.push_back({a , b});
         for(int j = 1 ; j < vec.size() ; ++j)
            insert(vec.back() , vec[j - 1].first , vec[j - 1].second , j , i);
      }
   }

   //cin >> n;
   //scanf("%d" , &t);
   //printf("%.10lf\n" , bs());
}


//CppSpoj <CERC07K , Key Task>
/**Milan Topalovic
*Faculty of Computing Belgrade
*/
//#include <bits/stdc++.h>
//#include <unordered_map>
//#include <unordered_set>
#include <map>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <vector>
#include <queue>
#include <set>
//#include <deque>
#include <limits.h>
//#include <climits>
#include <float.h> //DBL_MAX
#include <string>
#include <cstring>
//#include <cstdlib>
//#include <time.h>
#include <utility>
#include <iomanip>

using namespace std;

#define endl '\n'
#define dendl "\n\n"
#define inf 1e9
#define inf2 1e7
#define inf3 1e13
#define cti(chr) (((chr) - ('0'))) //char to int
#define itc(inte) (((inte) + ('0')))
#define isd(chr) (((chr) >= ('0') && (chr) <= ('9')))
#define is_upper(chr) (((chr) >= ('A') && (chr) <= ('Z')))
#define switch_case(chr) (((chr)) >= ('a') ? ('A') + ((chr) - ('a')) : ('a') + ((chr) - ('A')))
#define isin(x , y , lx , ly , bx , by) (((x) >= (lx)) && ((x) < (bx)) && ((y) >= (ly))  && ((y) < (by)))
#define maxn 1e8
#define PI 3.14159265358979323846
#define gc getchar_unlocked

template <typename T>
void afill(T *arr , int size , T val) { fill(arr , arr + size , val); }

typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int , int> pii;
typedef pair<ll , ll> pll;
typedef priority_queue<int , vector<int> , less<int> > maxqueue;
typedef priority_queue<int , vector<int> , greater<int> > minqueue;
long long mod = 1e9 + 7;
double eps = 1e-9;

int n , t , m , q , k;
int ans[101][101];
vector<pii> vec;
//You can move from interval (a, b) from our set to interval (c, d) from our set if and only if c < a < d or c < b < d.
void insert(pii pr , int a , int b , int newi , int curri)
{
   if((pr.first < a && a < pr.second) || (pr.first < b && b < pr.second))
   {
      ans[newi][curri] = 1;
      for(int i = 1 ; i <= vec.size() ; ++i)
         ans[newi][i] |= ans[curri][i];
   }
}

int main()
{
   ios_base::sync_with_stdio(0);

   cin >> n;

   for(int i = 1 ; i <= n ; ++i)
   {
      int a , b;
      cin >> q >> a >> b;
      if(q == 2)
      {
         if(ans[a][b])
            cout << "YES" << endl;
         else
            cout << "NO" << endl;
      } else {
         for(int j = 1 ; j <= vec.size() ; ++j)
            insert(vec[j - 1] , a , b , i , j);
         vec.push_back({a , b});
         for(int j = 1 ; j < vec.size() - 1  ; ++j)
            insert(vec.back() , vec[j - 1].first , vec[j - 1].second , j , i);
      }
   }

   //cin >> n;
   //scanf("%d" , &t);
   //printf("%.10lf\n" , bs());
}


//CppSpoj <CERC07K , Key Task>
/**Milan Topalovic
*Faculty of Computing Belgrade
*/
//#include <bits/stdc++.h>
//#include <unordered_map>
//#include <unordered_set>
#include <map>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <vector>
#include <queue>
#include <set>
//#include <deque>
#include <limits.h>
//#include <climits>
#include <float.h> //DBL_MAX
#include <string>
#include <cstring>
//#include <cstdlib>
//#include <time.h>
#include <utility>
#include <iomanip>

using namespace std;

#define endl '\n'
#define dendl "\n\n"
#define inf 1e9
#define inf2 1e7
#define inf3 1e13
#define cti(chr) (((chr) - ('0'))) //char to int
#define itc(inte) (((inte) + ('0')))
#define isd(chr) (((chr) >= ('0') && (chr) <= ('9')))
#define is_upper(chr) (((chr) >= ('A') && (chr) <= ('Z')))
#define switch_case(chr) (((chr)) >= ('a') ? ('A') + ((chr) - ('a')) : ('a') + ((chr) - ('A')))
#define isin(x , y , lx , ly , bx , by) (((x) >= (lx)) && ((x) < (bx)) && ((y) >= (ly))  && ((y) < (by)))
#define maxn 1e8
#define PI 3.14159265358979323846
#define gc getchar_unlocked

template <typename T>
void afill(T *arr , int size , T val) { fill(arr , arr + size , val); }

typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int , int> pii;
typedef pair<ll , ll> pll;
typedef priority_queue<int , vector<int> , less<int> > maxqueue;
typedef priority_queue<int , vector<int> , greater<int> > minqueue;
long long mod = 1e9 + 7;
double eps = 1e-9;

int n , t , m , q , k;
int ans[101][101];
vector<pii> vec;
//You can move from interval (a, b) from our set to interval (c, d) from our set if and only if c < a < d or c < b < d.
void insert(pii pr , int a , int b , int newi , int curri)
{
   if((pr.first < a && a < pr.second) || (pr.first < b && b < pr.second))
   {
      ans[newi][curri] = 1;
      for(int i = 1 ; i < 101 ; ++i)
         ans[newi][i] |= ans[curri][i];
   }
}

int main()
{
   ios_base::sync_with_stdio(0);

   cin >> n;

   for(int i = 1 ; i <= n ; ++i)
   {
      int a , b;
      cin >> q >> a >> b;
      if(q == 2)
      {
         if(ans[a][b])
            cout << "YES" << endl;
         else
            cout << "NO" << endl;
      } else {
         for(int j = 1 ; j <= vec.size() ; ++j)
            insert(vec[j - 1] , a , b , i , j);
         for(int j = 1 ; j <= vec.size() ; ++j)
            insert({a , b} , vec[j - 1].first , vec[j - 1].second , j , i);
         vec.push_back({a , b});
      }
   }

   //cin >> n;
   //scanf("%d" , &t);
   //printf("%.10lf\n" , bs());
}


//CppSpoj <CERC07K , Key Task>
/**Milan Topalovic
*Faculty of Computing Belgrade
*/
//#include <bits/stdc++.h>
//#include <unordered_map>
//#include <unordered_set>
#include <map>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <vector>
#include <queue>
#include <set>
//#include <deque>
#include <limits.h>
//#include <climits>
#include <float.h> //DBL_MAX
#include <string>
#include <cstring>
//#include <cstdlib>
//#include <time.h>
#include <utility>
#include <iomanip>

using namespace std;

#define endl '\n'
#define dendl "\n\n"
#define inf 1e9
#define inf2 1e7
#define inf3 1e13
#define cti(chr) (((chr) - ('0'))) //char to int
#define itc(inte) (((inte) + ('0')))
#define isd(chr) (((chr) >= ('0') && (chr) <= ('9')))
#define is_upper(chr) (((chr) >= ('A') && (chr) <= ('Z')))
#define switch_case(chr) (((chr)) >= ('a') ? ('A') + ((chr) - ('a')) : ('a') + ((chr) - ('A')))
#define isin(x , y , lx , ly , bx , by) (((x) >= (lx)) && ((x) < (bx)) && ((y) >= (ly))  && ((y) < (by)))
#define maxn 1e8
#define PI 3.14159265358979323846
#define gc getchar_unlocked

template <typename T>
void afill(T *arr , int size , T val) { fill(arr , arr + size , val); }

typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int , int> pii;
typedef pair<ll , ll> pll;
typedef priority_queue<int , vector<int> , less<int> > maxqueue;
typedef priority_queue<int , vector<int> , greater<int> > minqueue;
long long mod = 1e9 + 7;
double eps = 1e-9;

int n , t , m , q , k;
vector<pii> vec;
vector<int> vis;

bool cango(int s , int e)
{
   pii si = vec[s] , ei = vec[e];
   if(ei.first < si.first && si.first < ei.second)
      return true;
   if(ei.first < si.second && si.second < ei.second)
      return true;
   return false;
}

bool dfs(int v , int e)
{
   vis[v] = 1;
   if(v == e)
      return true;
   for(int i = 0 ; i < vec.size() ; ++i)
      if(!vis[i] && cango(v , i) && dfs(i , e))
         return true;
   return false;
}

int main()
{
   ios_base::sync_with_stdio(0);

   cin >> n;

   for(int i = 1 ; i <= n ; ++i)
   {
      int a , b;
      cin >> q >> a >> b;

      if(q == 2)
      {
         fill(vis.begin() , vis.end() , 0);
         if(dfs(--a , --b))
            cout << "YES" << endl;
         else
            cout << "NO" << endl;
      } else {
         vec.push_back({a , b});
         vis.push_back(0);
      }
   }

   //cin >> n;
   //scanf("%d" , &t);
   //printf("%.10lf\n" , bs());
}


//CppSpoj <CERC07K , Key Task>
/**Milan Topalovic
*Faculty of Computing Belgrade
*/
//#include <bits/stdc++.h>
//#include <unordered_map>
//#include <unordered_set>
#include <map>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <vector>
#include <queue>
#include <set>
//#include <deque>
#include <limits.h>
//#include <climits>
#include <float.h> //DBL_MAX
#include <string>
#include <cstring>
//#include <cstdlib>
//#include <time.h>
#include <utility>
#include <iomanip>

using namespace std;

#define endl '\n'
#define dendl "\n\n"
#define inf 1e9
#define inf2 1e7
#define inf3 1e13
#define cti(chr) (((chr) - ('0'))) //char to int
#define itc(inte) (((inte) + ('0')))
#define isd(chr) (((chr) >= ('0') && (chr) <= ('9')))
#define is_upper(chr) (((chr) >= ('A') && (chr) <= ('Z')))
#define switch_case(chr) (((chr)) >= ('a') ? ('A') + ((chr) - ('a')) : ('a') + ((chr) - ('A')))
#define isin(x , y , lx , ly , bx , by) (((x) >= (lx)) && ((x) < (bx)) && ((y) >= (ly))  && ((y) < (by)))
#define maxn 1e8
#define PI 3.14159265358979323846
#define gc getchar_unlocked

template <typename T>
void afill(T *arr , int size , T val) { fill(arr , arr + size , val); }

typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int , int> pii;
typedef pair<ll , ll> pll;
typedef priority_queue<int , vector<int> , less<int> > maxqueue;
typedef priority_queue<int , vector<int> , greater<int> > minqueue;
long long mod = 1e9 + 7;
double eps = 1e-9;

int n , t , m , q , k;
int ans[101][101];
vector<pii> vec;
//You can move from interval (a, b) from our set to interval (c, d) from our set if and only if c < a < d or c < b < d.
void insert(pii pr , int a , int b , int newi , int curri)
{
   if((pr.first < a && a < pr.second) || (pr.first < b && b < pr.second))
   {
      ans[newi][curri] = 1;
      for(int i = 1 ; i < 101 ; ++i)
         ans[newi][i] |= ans[curri][i];
   }
}

int main()
{
   ios_base::sync_with_stdio(0);

   cin >> n;

   for(int i = 1 ; i <= n ; ++i)
   {
      int a , b;
      cin >> q >> a >> b;
      if(q == 2)
      {
         if(ans[a][b])
            cout << "YES" << endl;
         else
            cout << "NO" << endl;
      } else {
         for(int j = 1 ; j <= vec.size() ; ++j)
            insert(vec[j - 1] , a , b , vec.size() + 1 , j);
         for(int j = 1 ; j <= vec.size() ; ++j)
            insert({a , b} , vec[j - 1].first , vec[j - 1].second , j , vec.size() + 1);
         vec.push_back({a , b});
      }
   }

   //cin >> n;
   //scanf("%d" , &t);
   //printf("%.10lf\n" , bs());
}


#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

const int N = 105;
vector<int> adj[N];
vector<bool> vis;
void dfs(int n){
	vis[n] = true;
	for(int i:adj[n]){
		if(!vis[i]) dfs(i);
	}
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);cout.tie(NULL);
	int q;
	int n, a, b;
	vector<pair<int,int>> in;
	cin>>q;
	while(q--){
		vis.clear();
		vis.resize(N, false);
		cin>>n>>a>>b;
		if(n == 1){
			for(int i=0;i<in.size();i++){
				if((in[i].first < a and a < in[i].second) or (in[i].first < b and b < in[i].second))
					adj[i].push_back(in.size());
				if((a < in[i].first and in[i].first < b) or (a < in[i].second and in[i].second < b))
					adj[in.size()].push_back(i);
			}
			in.push_back({a, b});
		}
		else{
			a--;b--;
			dfs(a);
			if(vis[b]) cout<<"YES"<<endl;
			else cout<<"NO"<<endl;
		}
	}
	return 0;
}


#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

const int N = 105;
vector<int> adj[N];
vector<bool> vis;
void dfs(int n){
	vis[n] = true;
	for(int i:adj[n]){
		if(!vis[i]) dfs(i);
	}
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);cout.tie(NULL);
	int q;
	int n, a, b;
	vector<pair<int,int>> in;
	cin>>q;
	while(q--){
		vis.clear();
		vis.resize(N, false);
		cin>>n>>a>>b;
		if(n == 1){
			for(int i=0;i<in.size();i++){
				if((in[i].first < a and a < in[i].second) or (in[i].first < b and b < in[i].second) or (a < in[i].first and in[i].first < b) or (a < in[i].second and in[i].second < b)){
					adj[i].push_back(in.size());
					adj[in.size()].push_back(i);
				}
			}
			in.push_back({a, b});
		}
		else{
			a--;b--;
			dfs(a);
			if(vis[b]) cout<<"YES"<<endl;
			else cout<<"NO"<<endl;
		}
	}
	return 0;
}


#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

const int N = 105;
vector<int> adj[N];
vector<bool> vis;
void dfs(int n){
	vis[n] = true;
	for(int i:adj[n]){
		if(!vis[i]) dfs(i);
	}
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);cout.tie(NULL);
	int q;
	int n, a, b;
	vector<pair<int,int>> in;
	cin>>q;
	while(q--){
		vis.resize(N, false);
		cin>>n>>a>>b;
		if(n == 1){
			for(int i=0;i<in.size();i++){
				if((in[i].first < a and a < in[i].second) or (in[i].first < b and b < in[i].second)){
					adj[i].push_back(in.size());
					adj[in.size()].push_back(i);
				}
			}
			in.push_back({a, b});
		}
		else{
			a--;b--;
			dfs(a);
			if(vis[b]) cout<<"YES"<<endl;
			else cout<<"NO"<<endl;
		}
	}
	return 0;
}


#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

const int N = 105;
vector<int> adj[N];
vector<bool> vis;
void dfs(int n){
	vis[n] = true;
	for(int i:adj[n]){
		if(!vis[i]) dfs(i);
	}
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);cout.tie(NULL);
	int q;
	int n, a, b;
	vector<pair<int,int>> in;
	cin>>q;
	while(q--){
		vis.clear();
		vis.resize(N, false);
		cin>>n>>a>>b;
		if(n == 1){
			for(int i=0;i<in.size();i++){
				if((in[i].first < a and a < in[i].second) or (in[i].first < b and b < in[i].second)){
					adj[i].push_back(in.size());
					adj[in.size()].push_back(i);
				}
			}
			in.push_back({a, b});
		}
		else{
			a--;b--;
			dfs(a);
			if(vis[b]) cout<<"YES"<<endl;
			else cout<<"NO"<<endl;
		}
	}
	return 0;
}


#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

const int N = 105;
vector<int> adj[N];
vector<bool> vis;
void dfs(int n){
	vis[n] = true;
	for(int i:adj[n]){
		if(!vis[i]) dfs(i);
	}
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);cout.tie(NULL);
	int q;
	int n, a, b;
	vector<pair<int,int>> in;
	cin>>q;
	while(q--){
		vis.clear();
		vis.resize(N, false);
		cin>>n>>a>>b;
		if(n == 1){
			for(int i=0;i<in.size();i++){
				if((in[i].first < a and a < in[i].second) or (in[i].first < b and b < in[i].second))
					adj[in.size()].push_back(i);
				if((a < in[i].first and in[i].first < b) or (a < in[i].second and in[i].second < b))
					adj[i].push_back(in.size());
			}
			in.push_back({a, b});
		}
		else{
			a--;b--;
			dfs(a);
			if(vis[b]) cout<<"YES"<<endl;
			else cout<<"NO"<<endl;
		}
	}
	return 0;
}