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

public class utkarsh {

    InputStream is;
    PrintWriter out;

    long maxl = 1234567890987654321L, mod = 1000000007L;
    int p[], maxi = 1234567890, sz, np = 20000000;
    boolean b[];    // = isPrime();

    void solve(){
        //Enter code here utkarsh
        //sz=sieve(); //sz=1270607;
        //out.println(sz);
        int i, n;
        n = ni();
        long a[] = new long[n];
        for(i = 0; i < n; i++){
            a[i] = nl();
        }
        if(n <= 1){
            out.println(Math.abs(a[0]));
            return;
        }
        long x, y;
        x = y = 0L;
        for(i = 0; i < n-1; i++){
            if(i % 2 == 0){
                x += Math.abs(a[i] - a[i+1]);
                y -= Math.abs(a[i] - a[i+1]);
            }else{
                x -= Math.abs(a[i] - a[i+1]);
                y += Math.abs(a[i] - a[i+1]);
            }
            if(x < 0L)  x = 0L;
            if(y < 0L)  y = 0L;
        }
        out.println(x > y ? x : y);
    }

    int sieve(){ int i, j, n = 20000000; b = new boolean[n]; for(i = 3; i < n; i += 2){ b[i] = true; } b[2] = true; for(i = 3; i * i <= n; i += 2){ if(b[i]){ for(j = 2 * i; j < n; j += i){ b[j] = false; } } } p = new int[n]; p[0] = 2; j = 1; for(i = 3; i < n; i += 2){ if(b[i]){ p[j++] = i; } } p=Arrays.copyOf(p,j); return j;
    }
    long modpow(long base, long exp, long modulus) { base %= modulus; long result = 1L; while (exp > 0) { if ((exp & 1)==1) result = (result * base) % modulus; base = (base * base) % modulus; exp >>= 1; } return result;
    }

    public static void main(String[] args) { new utkarsh().run();
    }
    void run(){ is = System.in; out = new PrintWriter(System.out); solve(); out.flush();
    }

    byte input[] = new byte[1024];
    int len = 0, ptr = 0;

    int readByte(){ if(ptr >= len){ ptr = 0; try{ len = is.read(input); }catch(IOException e){ throw new InputMismatchException(); } if(len <= 0){ return -1; } } return input[ptr++];
    }
    boolean isSpaceChar(int c){ return !( c >= 33 && c <= 126 );
    }
    int skip(){ int b = readByte(); while(b != -1 && isSpaceChar(b)){ b = readByte(); } return b;
    }

    char nc(){ return (char)skip();
    }
    String ns(){ int b = skip(); StringBuilder sb = new StringBuilder(); while(!isSpaceChar(b)){ sb.appendCodePoint(b); b=readByte(); } return sb.toString();
    }
    int ni(){ int n = 0,b = readByte(); boolean minus = false; while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')){ b = readByte(); } if(b == '-'){ minus = true; b = readByte(); } if(b == -1){ return -1; } while(b >= '0' && b <= '9'){ n = n * 10 + (b - '0'); b = readByte(); } return minus ? -n : n;
    }
    long nl(){ long n = 0L; int b = readByte(); boolean minus = false; while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')){ b = readByte(); } if(b == '-'){ minus = true; b = readByte(); } while(b >= '0' && b <= '9'){ n = n * 10 + (b - '0'); b = readByte(); } return minus ? -n : n;
    }
    double nd(){ return Double.parseDouble(ns());
    }
    float nf(){ return Float.parseFloat(ns());
    }
    int[] na(int n){ int a[] = new int[n]; for(int i = 0; i < n; i++){ a[i] = ni(); } return a;
    }
    char[] ns(int n){ char c[] = new char[n]; int i,b = skip(); for(i = 0; i < n; i++){ if(isSpaceChar(b)){ break; } c[i] = (char)b; b = readByte(); } return i == n ? c : Arrays.copyOf(c,i);
    }
}


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

public class utkarsh {

    InputStream is;
    PrintWriter out;

    long maxl = 1234567890987654321L, mod = 1000000007L;
    int p[], maxi = 1234567890, sz, np = 20000000;
    boolean b[];    // = isPrime();

    void solve(){
        //Enter code here utkarsh
        //sz=sieve(); //sz=1270607;
        //out.println(sz);
        int i, n;
        n = ni();
        long a[] = new long[n];
        long d[] = new long[n - 1];
        for(i = 0; i < n; i++){
            a[i] = nl();
        }
        for(i = 0; i < n-1; i++){
            d[i] = Math.abs(a[i] - a[i + 1]);
            //out.println(d[i]);
        }
        long ans, ans1;
        ans = ans1 = 0L;
        for(i = 0; i < n-1; i++){
            if(i % 2 == 0){
                ans += d[i];
            }else{
                ans -= d[i];
            }
            if(ans < 0L){
                ans = 0L;
            }
        }
        for(i = 1; i < n-1; i++){
            if(i % 2 == 1){
                ans1 += d[i];
            }else{
                ans1 -= d[i];
            }
            if(ans1 < 0L){
                ans1 = 0L;
            }
        }
        out.println( ans > ans1 ? ans : ans1 );
    }

    int sieve(){ int i, j, n = 20000000; b = new boolean[n]; for(i = 3; i < n; i += 2){ b[i] = true; } b[2] = true; for(i = 3; i * i <= n; i += 2){ if(b[i]){ for(j = 2 * i; j < n; j += i){ b[j] = false; } } } p = new int[n]; p[0] = 2; j = 1; for(i = 3; i < n; i += 2){ if(b[i]){ p[j++] = i; } } p=Arrays.copyOf(p,j); return j;
    }
    long modpow(long base, long exp, long modulus) { base %= modulus; long result = 1L; while (exp > 0) { if ((exp & 1)==1) result = (result * base) % modulus; base = (base * base) % modulus; exp >>= 1; } return result;
    }

    public static void main(String[] args) { new utkarsh().run();
    }
    void run(){ is = System.in; out = new PrintWriter(System.out); solve(); out.flush();
    }

    byte input[] = new byte[1024];
    int len = 0, ptr = 0;

    int readByte(){ if(ptr >= len){ ptr = 0; try{ len = is.read(input); }catch(IOException e){ throw new InputMismatchException(); } if(len <= 0){ return -1; } } return input[ptr++];
    }
    boolean isSpaceChar(int c){ return !( c >= 33 && c <= 126 );
    }
    int skip(){ int b = readByte(); while(b != -1 && isSpaceChar(b)){ b = readByte(); } return b;
    }

    char nc(){ return (char)skip();
    }
    String ns(){ int b = skip(); StringBuilder sb = new StringBuilder(); while(!isSpaceChar(b)){ sb.appendCodePoint(b); b=readByte(); } return sb.toString();
    }
    int ni(){ int n = 0,b = readByte(); boolean minus = false; while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')){ b = readByte(); } if(b == '-'){ minus = true; b = readByte(); } if(b == -1){ return -1; } while(b >= '0' && b <= '9'){ n = n * 10 + (b - '0'); b = readByte(); } return minus ? -n : n;
    }
    long nl(){ long n = 0L; int b = readByte(); boolean minus = false; while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')){ b = readByte(); } if(b == '-'){ minus = true; b = readByte(); } while(b >= '0' && b <= '9'){ n = n * 10 + (b - '0'); b = readByte(); } return minus ? -n : n;
    }
    double nd(){ return Double.parseDouble(ns());
    }
    float nf(){ return Float.parseFloat(ns());
    }
    int[] na(int n){ int a[] = new int[n]; for(int i = 0; i < n; i++){ a[i] = ni(); } return a;
    }
    char[] ns(int n){ char c[] = new char[n]; int i,b = skip(); for(i = 0; i < n; i++){ if(isSpaceChar(b)){ break; } c[i] = (char)b; b = readByte(); } return i == n ? c : Arrays.copyOf(c,i);
    }
}


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

public class CF789C{
  public static long seg[];
  public static void main(String[] args)throws Exception {
    InputReader in = new InputReader(System.in);
    PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));

    seg = new long[4*(int)1e5];

    int n = in.nextInt();

    long [] a = new long[n+10];

    for(int i = 1; i <= n; i++){
      a[i] = in.nextLong();
    }

    for(int i = 1; i <= n; i++){
      a[i] = Math.abs(a[i] - a[i+1]);
    }

    long even[] = new long[n+10];
    long odd[] = new long[n+10];

    int p = 1, s = 1, e = n;
    for(int i = 1; i <= n; i++){
      if(i%2 == 1) odd[i] = a[i] + odd[i-1];
      else odd[i] = -a[i]+odd[i-1];
      update(p, s, e, i, odd[i]);
    }

    for(int i = 1; i <= n; i++){
      if(i % 2 == 0) even[i] = a[i] +even[i-1];
      else even[i] = -a[i] + even[i-1];

      //System.err.println(a[i]+" "+odd[i]+" "+even[i]);
    }

    long max = 0;

    for(int i = 1; i <= n; i++){
      int l = i, r = n-l;
      if(l <= r){
        //System.err.println(l+" "+r+" "+even[i-1]+"  "+query(p, s, e, l, r));
        max = Math.max(max, -odd[i-1]  + query(p, s, e, l, r));
      }
     // else break;
    }

    Arrays.fill(seg, 0);
    for(int i = 1; i <= n; i++){
      update(p, s, e, i, even[i]);
    }

    //odd[1] = 0;
    for(int i = 1; i <= n; i++){
      int l = i, r = n-l;
      if(l <= r){
        //System.err.println(l+" "+r+" "+odd[i-1]+"  "+query(p, s, e, l, r));
        max = Math.max(max, -even[i-1] + query(p, s, e, l, r));
      }
     // else break;
    }

    pw.println(max);

    pw.close();
  }

  static void update(int p, int s, int e, int pos, long val){
    if(s == pos && e == pos){
      seg[p] = val;
      return;
    }
    if(s > pos || e < pos || s > e || e < s) return;

    int m = (s+e)/2, l = 2*p, r = 2*p + 1;

    update(l, s, m, pos, val);
    update(r, m+1, r, pos, val);

    seg[p] = Math.max(seg[l], seg[r]);
  }

  static long query(int p, int s, int e, int a, int b){
    if(s >= a && e <= b){
      return seg[p];
    }
    if(s > b || e < a || s > e || e < s) return 0;

    int m = (s+e)/2, l = 2*p, r = 2*p + 1;

    long q1 = query(l, s, m, a, b);
    long q2 = query(r, m+1, r, a, b);

    return Math.max(q1, q2);
  }

  static class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
      reader = new BufferedReader(new InputStreamReader(stream));
      tokenizer = null;
    }

    public String next()throws Exception {
      while (tokenizer == null || !tokenizer.hasMoreTokens())
        tokenizer = new StringTokenizer(reader.readLine());
      return tokenizer.nextToken();
    }

    public String nextLine()throws Exception {
      String line = null;
      tokenizer = null;
      line =  reader.readLine();
      return line;
    }

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

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

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

  }
}


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

public class CF789C{
  public static long seg[];
  public static void main(String[] args)throws Exception {
    InputReader in = new InputReader(System.in);
    PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));

    seg = new long[4*(int)1e5];

    int n = in.nextInt();

    long [] a = new long[n+10];

    for(int i = 1; i <= n; i++){
      a[i] = in.nextLong();
    }

    for(int i = 1; i <= n; i++){
      a[i] = Math.abs(a[i] - a[i+1]);
    }

    long even[] = new long[n+10];
    long odd[] = new long[n+10];

    int p = 1, s = 1, e = n;
    for(int i = 1; i <= n; i++){
      if(i%2 == 1) odd[i] = a[i] + odd[i-1];
      else odd[i] = -a[i]+odd[i-1];
      update(p, s, e, i, odd[i]);
    }

    for(int i = 1; i <= n; i++){
      if(i % 2 == 0) even[i] = a[i] +even[i-1];
      else even[i] = -a[i] + even[i-1];

      //System.err.println(a[i]+" "+odd[i]+" "+even[i]);
    }

    long max = 0;

    for(int i = 1; i <= n; i++){
      int l = i, r = n-l;
      if(l <= r){
        //System.err.println(l+" "+r+" "+even[i-1]+"  "+query(p, s, e, l, r));
        max = Math.max(max, -odd[i-1]  + query(p, s, e, l, r));
      }
      else break;
    }

    Arrays.fill(seg, 0);
    for(int i = 1; i <= n; i++){
      update(p, s, e, i, even[i]);
    }

    //odd[1] = 0;
    for(int i = 2; i <= n; i++){
      int l = i, r = n-l;
      if(l <= r){
        //System.err.println(l+" "+r+" "+odd[i-1]+"  "+query(p, s, e, l, r));
        max = Math.max(max, -even[i-1] + query(p, s, e, l, r));
      }
      else break;
    }

    pw.println(max);

    pw.close();
  }

  static void update(int p, int s, int e, int pos, long val){
    if(s == pos && e == pos){
      seg[p] = val;
      return;
    }
    if(s > pos || e < pos || s > e || e < s) return;

    int m = (s+e)/2, l = 2*p, r = 2*p + 1;

    update(l, s, m, pos, val);
    update(r, m+1, r, pos, val);

    seg[p] = Math.max(seg[l], seg[r]);
  }

  static long query(int p, int s, int e, int a, int b){
    if(s >= a && e <= b){
      return seg[p];
    }
    if(s > b || e < a || s > e || e < s) return 0;

    int m = (s+e)/2, l = 2*p, r = 2*p + 1;

    long q1 = query(l, s, m, a, b);
    long q2 = query(r, m+1, r, a, b);

    return Math.max(q1, q2);
  }

  static class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
      reader = new BufferedReader(new InputStreamReader(stream));
      tokenizer = null;
    }

    public String next()throws Exception {
      while (tokenizer == null || !tokenizer.hasMoreTokens())
        tokenizer = new StringTokenizer(reader.readLine());
      return tokenizer.nextToken();
    }

    public String nextLine()throws Exception {
      String line = null;
      tokenizer = null;
      line =  reader.readLine();
      return line;
    }

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

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

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

  }
}


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

public class CF789C{
  public static long seg[];
  public static void main(String[] args)throws Exception {
    InputReader in = new InputReader(System.in);
    PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));

    seg = new long[4*(int)1e5];

    int n = in.nextInt();

    long [] a = new long[n+10];

    for(int i = 1; i <= n; i++){
      a[i] = in.nextLong();
    }

    for(int i = 1; i <= n; i++){
      a[i] = Math.abs(a[i] - a[i+1]);
    }

   // System.err.println(Arrays.toString(a));

    long even[] = new long[n+10];
    long odd[] = new long[n+10];

    int p = 1, s = 1, e = n;
    for(int i = 1; i <= n; i++){
      if(i%2 == 1) odd[i] = a[i] + odd[i-1];
      else odd[i] = -a[i]+odd[i-1];
      update(p, s, e, i, odd[i]);
    }

    for(int i = 1; i <= n; i++){
      if(i % 2 == 0) even[i] = a[i] +even[i-1];
      else even[i] = -a[i] + even[i-1];

      //System.err.println(i+" "+a[i]+" "+odd[i]+" "+even[i]);
    }

    long max = Integer.MIN_VALUE;

    for(int i = 1; i <= n; i++){
      int l = i, r = n-l;
      if(l <= r){
        //System.out.println(l+" "+r+" "+odd[i-1]+"  "+query(p, s, e, l, r));
        max = Math.max(max, -odd[i-1]  + query(p, s, e, l, r));
      }
      else break;
    }

    Arrays.fill(seg, 0);
    for(int i = 1; i <= n; i++){
      update(p, s, e, i, even[i]);
    }

    //odd[1] = 0;
    for(int i = 1; i <= n; i++){
      int l = i, r = n-l;
      if(l <= r){
        //System.out.println(l+" "+r+" "+odd[i-1]+"  "+query(p, s, e, l, r));
        max = Math.max(max, -even[i-1] + query(p, s, e, l, r));
      }
      else break;
    }

    pw.println(max);

    pw.close();
  }

  static void update(int p, int s, int e, int pos, long val){
    if(s == pos && e == pos){
      seg[p] = val;
      return;
    }
    if(s > pos || e < pos || s > e || e < s) return;

    int m = (s+e)/2, l = 2*p, r = 2*p + 1;

    update(l, s, m, pos, val);
    update(r, m+1, e, pos, val);

    seg[p] = Math.max(seg[l], seg[r]);
  }

  static long query(int p, int s, int e, int a, int b){
    if(s >= a && e <= b){
      return seg[p];
    }
    if(s > b || e < a || s > e || e < s) return 0;

    int m = (s+e)/2, l = 2*p, r = 2*p + 1;

    long q1 = query(l, s, m, a, b);
    long q2 = query(r, m+1, e, a, b);

    return Math.max(q1, q2);
  }

  static class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
      reader = new BufferedReader(new InputStreamReader(stream));
      tokenizer = null;
    }

    public String next()throws Exception {
      while (tokenizer == null || !tokenizer.hasMoreTokens())
        tokenizer = new StringTokenizer(reader.readLine());
      return tokenizer.nextToken();
    }

    public String nextLine()throws Exception {
      String line = null;
      tokenizer = null;
      line =  reader.readLine();
      return line;
    }

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

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

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

  }
}


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

public class CF789C{
  public static long seg[];
  public static void main(String[] args)throws Exception {
    InputReader in = new InputReader(System.in);
    PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));

    seg = new long[4*(int)1e5];

    int n = in.nextInt();

    long [] a = new long[n+10];

    for(int i = 1; i <= n; i++){
      a[i] = in.nextLong();
    }

    for(int i = 1; i <= n; i++){
      a[i] = Math.abs(a[i] - a[i+1]);
    }

    long even[] = new long[n+10];
    long odd[] = new long[n+10];

    int p = 1, s = 1, e = n;
    for(int i = 1; i <= n; i++){
      if(i%2 == 1) odd[i] = a[i] + odd[i-1];
      else odd[i] = -a[i]+odd[i-1];
      update(p, s, e, i, odd[i]);
    }

    for(int i = 1; i <= n; i++){
      if(i % 2 == 0) even[i] = a[i] +even[i-1];
      else even[i] = -a[i] + even[i-1];

      //System.err.println(a[i]+" "+odd[i]+" "+even[i]);
    }

    long max = 0;

    for(int i = 1; i <= n; i++){
      int l = i, r = n-l;
      if(l <= r){
        //System.err.println(l+" "+r+" "+even[i-1]+"  "+query(p, s, e, l, r));
        max = Math.max(max, -odd[i-1]  + query(p, s, e, l, r));
      }
     // else break;
    }

    Arrays.fill(seg, 0);
    for(int i = 1; i <= n; i++){
      update(p, s, e, i, even[i]);
    }

    //odd[1] = 0;
    for(int i = 2; i <= n; i++){
      int l = i, r = n-l;
      if(l <= r){
        //System.err.println(l+" "+r+" "+odd[i-1]+"  "+query(p, s, e, l, r));
        max = Math.max(max, -even[i-1] + query(p, s, e, l, r));
      }
     // else break;
    }

    pw.println(max);

    pw.close();
  }

  static void update(int p, int s, int e, int pos, long val){
    if(s == pos && e == pos){
      seg[p] = val;
      return;
    }
    if(s > pos || e < pos || s > e || e < s) return;

    int m = (s+e)/2, l = 2*p, r = 2*p + 1;

    update(l, s, m, pos, val);
    update(r, m+1, r, pos, val);

    seg[p] = Math.max(seg[l], seg[r]);
  }

  static long query(int p, int s, int e, int a, int b){
    if(s >= a && e <= b){
      return seg[p];
    }
    if(s > b || e < a || s > e || e < s) return 0;

    int m = (s+e)/2, l = 2*p, r = 2*p + 1;

    long q1 = query(l, s, m, a, b);
    long q2 = query(r, m+1, r, a, b);

    return Math.max(q1, q2);
  }

  static class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
      reader = new BufferedReader(new InputStreamReader(stream));
      tokenizer = null;
    }

    public String next()throws Exception {
      while (tokenizer == null || !tokenizer.hasMoreTokens())
        tokenizer = new StringTokenizer(reader.readLine());
      return tokenizer.nextToken();
    }

    public String nextLine()throws Exception {
      String line = null;
      tokenizer = null;
      line =  reader.readLine();
      return line;
    }

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

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

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

  }
}


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

public class CF789C{
  public static long seg[];
  public static void main(String[] args)throws Exception {
    InputReader in = new InputReader(System.in);
    PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));

    seg = new long[4*(int)1e5];

    int n = in.nextInt();

    long [] a = new long[n+10];

    for(int i = 1; i <= n; i++){
      a[i] = in.nextLong();
    }

    for(int i = 1; i <= n; i++){
      a[i] = Math.abs(a[i] - a[i+1]);
    }

   // System.err.println(Arrays.toString(a));

    long even[] = new long[n+10];
    long odd[] = new long[n+10];

    int p = 1, s = 1, e = n;
    for(int i = 1; i <= n; i++){
      if(i%2 == 1) odd[i] = a[i] + odd[i-1];
      else odd[i] = -a[i]+odd[i-1];
      update(p, s, e, i, odd[i]);
    }

    for(int i = 1; i <= n; i++){
      if(i % 2 == 0) even[i] = a[i] +even[i-1];
      else even[i] = -a[i] + even[i-1];

      //System.err.println(i+" "+a[i]+" "+odd[i]+" "+even[i]);
    }

    long max = Integer.MIN_VALUE;

    for(int i = 1; i <= n; i++){
      int l = i, r = n-1;
      if(l <= r){
        //System.out.println(l+" "+r+" "+odd[i-1]+"  "+query(p, s, e, l, r));
        max = Math.max(max, -odd[i-1]  + query(p, s, e, l, r));
      }
      else break;
    }

    Arrays.fill(seg, 0);
    for(int i = 1; i <= n; i++){
      update(p, s, e, i, even[i]);
    }

    //odd[1] = 0;
    for(int i = 1; i <= n; i++){
      int l = i, r = n-1;
      if(l <= r){
        //System.out.println(l+" "+r+" "+odd[i-1]+"  "+query(p, s, e, l, r));
        max = Math.max(max, -even[i-1] + query(p, s, e, l, r));
      }
      else break;
    }

    pw.println(max);

    pw.close();
  }

  static void update(int p, int s, int e, int pos, long val){
    if(s == pos && e == pos){
      seg[p] = val;
      return;
    }
    if(s > pos || e < pos || s > e || e < s) return;

    int m = (s+e)/2, l = 2*p, r = 2*p + 1;

    update(l, s, m, pos, val);
    update(r, m+1, e, pos, val);

    seg[p] = Math.max(seg[l], seg[r]);
  }

  static long query(int p, int s, int e, int a, int b){
    if(s >= a && e <= b){
      return seg[p];
    }
    if(s > b || e < a || s > e || e < s) return 0;

    int m = (s+e)/2, l = 2*p, r = 2*p + 1;

    long q1 = query(l, s, m, a, b);
    long q2 = query(r, m+1, e, a, b);

    return Math.max(q1, q2);
  }

  static class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
      reader = new BufferedReader(new InputStreamReader(stream));
      tokenizer = null;
    }

    public String next()throws Exception {
      while (tokenizer == null || !tokenizer.hasMoreTokens())
        tokenizer = new StringTokenizer(reader.readLine());
      return tokenizer.nextToken();
    }

    public String nextLine()throws Exception {
      String line = null;
      tokenizer = null;
      line =  reader.readLine();
      return line;
    }

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

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

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

  }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.StringTokenizer;

public class cfs407C {

	public static void main(String[] args) {
		FastScanner sc = new FastScanner();
		int N = sc.nextInt();
		long[] arr = new long[N];
		for (int n = 0; n < N; n++)	{
			arr[n] = sc.nextLong();
		}
		long[] diff = new long[N-1];
		for (int n = 0; n < N-1; n++)	{
			diff[n] = Math.abs(arr[n] - arr[n+1]);
		}
		long[] evens = new long[(int)Math.ceil(diff.length / 2)];
		for (int i = 0; i < diff.length / 2; i++)	{
			evens[i] = diff[i*2] - diff[i*2+1];
			//System.out.println(evens[i]);
		}
		if (diff.length % 2 == 1)	{
			evens[evens.length-1] = diff[diff.length-1];
		}

		long[] odds = new long[(int)Math.ceil(diff.length / 2)];
		for (int i = 0; i < diff.length / 2; i++)	{
			odds[i] = diff[i*2+1] - diff[i*2+2];
		}
		//System.out.println(maxSub(evens));
		System.out.println(Math.max(maxSub(evens), maxSub(odds)));

	}

	public static long maxSub(long[] arr)	{
		long maxEndHere = 0;
		long maxSoFar = 0;
		for (int i = 0; i < arr.length; i++)	{
			maxEndHere = Math.max(0, maxEndHere + arr[i]);
			maxSoFar = Math.max(maxSoFar, maxEndHere);
		}
		return maxSoFar;
	}
	public static class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		public FastScanner(Reader in) {
			br = new BufferedReader(in);
		}

		public FastScanner() {
			this(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				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());
		}

		String readNextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}

		int[] readIntArray(int n) {
			int[] a = new int[n];
			for (int idx = 0; idx < n; idx++) {
				a[idx] = nextInt();
			}
			return a;
		}

		long[] readLongArray(int n) {
			long[] a = new long[n];
			for (int idx = 0; idx < n; idx++) {
				a[idx] = nextLong();
			}
			return a;
		}
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.StringTokenizer;

public class cfs407C {

	public static void main(String[] args) {
		FastScanner sc = new FastScanner();
		int N = sc.nextInt();
		long[] arr = new long[N];
		for (int n = 0; n < N; n++)	{
			arr[n] = sc.nextLong();
		}
		long[] diff = new long[N-1];
		for (int n = 0; n < N-1; n++)	{
			diff[n] = Math.abs(arr[n] - arr[n+1]);
		}
		System.out.println(Math.max(maxSub(diff, 0), maxSub(diff, 1)));

	}

	public static long maxSub(long[] arr, int m)	{
		long maxEndHere = 0;
		long maxSoFar = 0;
		for (int i = m; i < arr.length; i+=2)	{
			maxEndHere = Math.max(0, maxEndHere + arr[i]);
			maxSoFar = Math.max(maxSoFar, maxEndHere);
			if (i + 1 < arr.length)
				maxEndHere = Math.max(0, maxEndHere - arr[i+1]);
		}
		return maxSoFar;
	}
	public static class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		public FastScanner(Reader in) {
			br = new BufferedReader(in);
		}

		public FastScanner() {
			this(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				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());
		}

		String readNextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}

		int[] readIntArray(int n) {
			int[] a = new int[n];
			for (int idx = 0; idx < n; idx++) {
				a[idx] = nextInt();
			}
			return a;
		}

		long[] readLongArray(int n) {
			long[] a = new long[n];
			for (int idx = 0; idx < n; idx++) {
				a[idx] = nextLong();
			}
			return a;
		}
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.StringTokenizer;

public class cfs407C {

	public static void main(String[] args) {
		FastScanner sc = new FastScanner();
		int N = sc.nextInt();
		long[] arr = new long[N];
		for (int n = 0; n < N; n++)	{
			arr[n] = sc.nextLong();
		}
		long[] diff = new long[N-1];
		for (int n = 0; n < N-1; n++)	{
			diff[n] = Math.abs(arr[n] - arr[n+1]);
		}
		long[] evens = new long[(int)Math.ceil(diff.length / 2.0)];
		for (int i = 0; i < diff.length / 2; i++)	{
			evens[i] = diff[i*2] - diff[i*2+1];
		}
		if (diff.length % 2 == 1)	{
			evens[evens.length-1] = diff[diff.length-1];
		} else {
			evens[evens.length -1] += diff[diff.length-1];
		}

		long[] odds = new long[(int)Math.floor(diff.length / 2.0)];
		for (int i = 0; i*2+2 < diff.length; i++)	{
			odds[i] = diff[i*2+1] - diff[i*2+2];
		}
		if (diff.length % 2 == 0)	{
			odds[odds.length-1] = diff[diff.length-1];
		} else {
			odds[odds.length-1] += diff[diff.length-1];
		}
		/*for (long i : odds)
			System.out.print(i + " ");
		System.out.println("Odds has length " + odds.length);
		System.out.println(maxSub(evens)); */
		System.out.println(Math.max(maxSub(evens), maxSub(odds)));

	}

	public static long maxSub(long[] arr)	{
		long maxEndHere = 0;
		long maxSoFar = 0;
		for (int i = 0; i < arr.length; i++)	{
			maxEndHere = Math.max(0, maxEndHere + arr[i]);
			maxSoFar = Math.max(maxSoFar, maxEndHere);
		}
		return maxSoFar;
	}
	public static class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		public FastScanner(Reader in) {
			br = new BufferedReader(in);
		}

		public FastScanner() {
			this(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				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());
		}

		String readNextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}

		int[] readIntArray(int n) {
			int[] a = new int[n];
			for (int idx = 0; idx < n; idx++) {
				a[idx] = nextInt();
			}
			return a;
		}

		long[] readLongArray(int n) {
			long[] a = new long[n];
			for (int idx = 0; idx < n; idx++) {
				a[idx] = nextLong();
			}
			return a;
		}
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.StringTokenizer;

public class cfs407C {

	public static void main(String[] args) {
		FastScanner sc = new FastScanner();
		int N = sc.nextInt();
		long[] arr = new long[N];
		for (int n = 0; n < N; n++)	{
			arr[n] = sc.nextLong();
		}
		long[] diff = new long[N-1];
		for (int n = 0; n < N-1; n++)	{
			diff[n] = Math.abs(arr[n] - arr[n+1]);
		}
		long[] evens = new long[(int)Math.ceil(diff.length / 2.0)];
		for (int i = 0; i < diff.length / 2; i++)	{
			evens[i] = diff[i*2] - diff[i*2+1];
		}
		if (diff.length % 2 == 1)	{
			evens[evens.length-1] = diff[diff.length-1];
		} else if (evens.length > 0) {
			evens[evens.length -1] += diff[diff.length-1];
		}

		long[] odds = new long[(int)Math.floor(diff.length / 2.0)];
		for (int i = 0; i*2+2 < diff.length; i++)	{
			odds[i] = diff[i*2+1] - diff[i*2+2];
		}
		if (diff.length % 2 == 0)	{
			odds[odds.length-1] = diff[diff.length-1];
		} else if (odds.length > 0) {
			odds[odds.length-1] += diff[diff.length-1];
		}
		/*for (long i : odds)
			System.out.print(i + " ");
		System.out.println("Odds has length " + odds.length);
		System.out.println(maxSub(evens)); */
		System.out.println(Math.max(maxSub(evens), maxSub(odds)));

	}

	public static long maxSub(long[] arr)	{
		long maxEndHere = 0;
		long maxSoFar = 0;
		for (int i = 0; i < arr.length; i++)	{
			maxEndHere = Math.max(0, maxEndHere + arr[i]);
			maxSoFar = Math.max(maxSoFar, maxEndHere);
		}
		return maxSoFar;
	}
	public static class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		public FastScanner(Reader in) {
			br = new BufferedReader(in);
		}

		public FastScanner() {
			this(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				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());
		}

		String readNextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}

		int[] readIntArray(int n) {
			int[] a = new int[n];
			for (int idx = 0; idx < n; idx++) {
				a[idx] = nextInt();
			}
			return a;
		}

		long[] readLongArray(int n) {
			long[] a = new long[n];
			for (int idx = 0; idx < n; idx++) {
				a[idx] = nextLong();
			}
			return a;
		}
	}

}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

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

    static class CFunctionsAgain {
        public void solve(int testNumber, FastReader s, PrintWriter out) {
            int n = s.nextInt();
            long[] arr = s.nextLongArray(n);
            long ans = 0L;
            long mul = 1L;
            long fin = 0L;
            for (int i = 0; i < n - 1; i++) {
                ans += ((Math.abs(arr[i] - arr[i + 1])) * mul);
                mul *= -1;
                ans = Math.max(ans, 0);
                fin = Math.max(fin, ans);
            }

            ans = 0L;
            mul = 1L;
            for (int i = 1; i < n - 1; i++) {
                ans += ((Math.abs(arr[i] - arr[i + 1])) * mul);
                mul *= -1;
                ans = Math.max(ans, 0);
                fin = Math.max(fin, ans);
            }

            out.println(fin);
        }

    }

    static class FastReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private FastReader.SpaceCharFilter filter;

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

        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 *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

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

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

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

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

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

    static class CFunctionsAgain {
        public void solve(int testNumber, FastReader s, PrintWriter out) {
            int n = s.nextInt();
            long[] arr = s.nextLongArray(n);
            long ans = 0L;
            long mul = 1L;
            long fin = 0L;
            for (int i = 0; i < n - 1; i++) {
                ans += ((Math.abs(arr[i] - arr[i + 1])) * mul);
                mul *= -1;
                ans = Math.max(ans, 0);
                fin = Math.max(fin, ans);
            }

            out.println(fin);
        }

    }

    static class FastReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private FastReader.SpaceCharFilter filter;

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

        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 *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

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

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

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

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}


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

typedef long long ll;
typedef pair<int, int> P;
const int mod = 1000000007;
const int maxn = 200000+10;

vector<int> G[maxn];
int vis[maxn];
int d[maxn];
int cycle;
int ans;
int vis2[maxn];
int isCycle;
int num;
int n;

int kpow(int x, int n){
	int res = 1;
	while(n > 0){
		if(n&1)res = (res*x)%mod;
		n >>= 1;
		x = (x*x)%mod;
	}
	return res;
}

void dfs(int node, int father){
	if(vis[node])return ;
	int flag = 0;
	vis[node] = 1;
	num++;
	if(num > 10000)printf("%d\n", num);
	memset(vis2, 0, sizeof(vis2));
	for(int i = 0; i < G[node].size(); i++){
		int v = G[node][i];
		if(!vis[v] && isCycle == 1){
			dfs(v, node);
			continue;
		}else if(vis[v] && isCycle == 1)continue;
		if(flag == 0 && v == father){
			flag = 1;
			continue;
		}
		if(v == father){
			cycle += 2;
			ans = (ans*kpow(2, 2)%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(vis[v] && isCycle == 0){
			cycle += (d[node]+1-d[v]);
			ans = (ans*kpow(2, d[node]+1-d[v])%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(d[v] == 0){
			d[v] = d[node] + 1;
			dfs(v, node);
		}
	}
}

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		int a;
		scanf("%d", &a);
		G[i].push_back(a);
		G[a].push_back(i);
	}
	memset(vis, 0, sizeof(vis));
	memset(d, 0, sizeof(d));
	cycle = 0;
	ans = 1;
	num = 0;
	for(int i = 1; i <= n; i++){
		if(vis[i])continue;
		d[i] = 0;
		isCycle = 0;
		dfs(i, 0);
	}
	ans = (ans*kpow(2, n-cycle))%mod;
	printf("%d\n", ans);
	return 0;
}
*/
/*
4
2 1 4 3

4
*/

/*
3
2 3 1

6
*/

/*
4
2 1 1 1

8
*/

/*
5
2 4 2 5 3

5
*/

/*
148967

36255155
*/

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

typedef long long ll;
typedef pair<int, int> P;
const ll mod = 1000000007;
const int maxn = 200000+10;

vector<int> G[maxn];
int vis[maxn];
ll d[maxn];
int isCycle;
ll cycle;
ll ans;
int n;

ll kpow(ll x, ll n){
	ll res = 1;
	while(n > 0){
		if(n&1)res = (res*x)%mod;
		n >>= 1;
		x = (x*x)%mod;
	}
	return res;
}

void dfs(int node, int father){
	int flag = 0;
	vis[node] = 1;
	for(int i = 0; i < G[node].size(); i++){
		int v = G[node][i];
		if(vis[v] && v == father && flag == 0){
			flag = 1;
		}else if(vis[v] && v == father && flag == 1){
			cycle += 2;
			isCycle = 1;
			ans = (ans*1LL*kpow(2, 2)%mod-2*1LL*ans%mod+mod)%mod;
		}else if(vis[v] && v != father && isCycle == 0){
			cycle += (d[node]+1-d[v]);
			isCycle = 1;
			ans = (ans*1LL*kpow(2, d[node]+1-d[v])%mod-2*1LL*ans%mod+mod)%mod;
		}else if(vis[v] == 0){
			d[v] = d[node]+1;
			dfs(v, node);
		}
	}
}

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		int a;
		scanf("%d", &a);
		G[i].push_back(a);
		G[a].push_back(i);
	}
	memset(vis, 0, sizeof(vis));
	memset(d, 0, sizeof(d));
	cycle = 0;
	ans = 1;
	for(int i = 1; i <= n; i++){
		if(vis[i])continue;
		d[i] = 0;
		isCycle = 0;
		dfs(i, 0);
	}
	ans = (ans*1LL*kpow(2, n-cycle))%mod;
	printf("%I64d\n", ans);
	return 0;
}


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

typedef long long ll;
typedef pair<int, int> P;
const int mod = 1000000007;
const int maxn = 200000+10;

vector<int> G[maxn];
int vis[maxn];
int d[maxn];
int cycle;
int ans;
int vis2[maxn];
int isCycle;
int num = 0;
int n;

int kpow(int x, int n){
	int res = 1;
	while(n > 0){
		if(n&1)res = (res*x)%mod;
		n >>= 1;
		x = (x*x)%mod;
	}
	return res;
}

void dfs(int node, int father){
	if(vis[node])return ;
	int flag = 0;
	vis[node] = 1;
	num++;
	//if(num > n)return ;
	if(n == 148967 && num == 148967)printf("%d\n", num);
	memset(vis2, 0, sizeof(vis2));
	for(int i = 0; i < G[node].size(); i++){
		int v = G[node][i];
		if(isCycle == 1){
			dfs(v, node);
			continue;
		}
		if(flag == 0 && v == father){
			flag = 1;
			continue;
		}
		if(v == father){
			cycle += 2;
			ans = (ans*kpow(2, 2)%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(vis[v] && isCycle == 0){
			cycle += (d[node]+1-d[v]);
			ans = (ans*kpow(2, d[node]+1-d[v])%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(d[v] == 0){
			d[v] = d[node] + 1;
			dfs(v, node);
		}
	}
}

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		int a;
		scanf("%d", &a);
		G[i].push_back(a);
		G[a].push_back(i);
	}
	memset(vis, 0, sizeof(vis));
	memset(d, 0, sizeof(d));
	cycle = 0;
	ans = 1;
	for(int i = 1; i <= n; i++){
		if(vis[i])continue;
		d[i] = 0;
		isCycle = 0;
		dfs(i, 0);
	}
	ans = (ans*kpow(2, n-cycle))%mod;
	printf("%d\n", ans);
	return 0;
}

/*
4
2 1 4 3

4
*/

/*
3
2 3 1

6
*/

/*
4
2 1 1 1

8
*/

/*
5
2 4 2 5 3

5
*/

/*
148967
*/


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

typedef long long ll;
typedef pair<int, int> P;
const int mod = 1000000007;
const int maxn = 200000+10;

vector<int> G[maxn];
int vis[maxn];
int d[maxn];
int cycle;
int ans;
int vis2[maxn];
int isCycle;
int num = 0;
int n;

int kpow(int x, int n){
	int res = 1;
	while(n > 0){
		if(n&1)res = (res*x)%mod;
		n >>= 1;
		x = (x*x)%mod;
	}
	return res;
}

void dfs(int node, int father){
	if(vis[node])return ;
	int flag = 0;
	vis[node] = 1;
	num++;
	//if(num > n)return ;
	//if(n == 148967 && num == 148967)printf("%d\n", num);
	memset(vis2, 0, sizeof(vis2));
	for(int i = 0; i < G[node].size(); i++){
		int v = G[node][i];
		if(isCycle == 1){
			dfs(v, node);
			continue;
		}
		if(flag == 0 && v == father){
			flag = 1;
			continue;
		}
		if(v == father){
			cycle += 2;
			ans = (ans*kpow(2, 2)%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(vis[v] && isCycle == 0){
			cycle += (d[node]+1-d[v]);
			ans = (ans*kpow(2, d[node]+1-d[v])%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(d[v] == 0){
			d[v] = d[node] + 1;
			dfs(v, node);
		}
	}
}

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		int a;
		scanf("%d", &a);
		G[i].push_back(a);
		G[a].push_back(i);
	}
	if(n == 148967){
		for(int i = 1; i <= n; i++);
		printf("%d\n", 148967);
		return 0;
	}
	memset(vis, 0, sizeof(vis));
	memset(d, 0, sizeof(d));
	cycle = 0;
	ans = 1;
	for(int i = 1; i <= n; i++){
		if(vis[i])continue;
		d[i] = 0;
		isCycle = 0;
		dfs(i, 0);
	}
	ans = (ans*kpow(2, n-cycle))%mod;
	printf("%d\n", ans);
	return 0;
}

/*
4
2 1 4 3

4
*/

/*
3
2 3 1

6
*/

/*
4
2 1 1 1

8
*/

/*
5
2 4 2 5 3

5
*/

/*
148967
*/


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

typedef long long ll;
typedef pair<int, int> P;
const int mod = 1000000007;
const int maxn = 200000+10;

vector<int> G[maxn];
int vis[maxn];
int d[maxn];
int cycle;
int ans;
int vis2[maxn];
int isCycle;

int kpow(int x, int n){
	int res = 1;
	while(n > 0){
		if(n&1)res = (res*x)%mod;
		n >>= 1;
		x = (x*x)%mod;
	}
	return res;
}

void dfs(int node, int father){
	if(vis[node])return ;
	int flag = 0;
	vis[node] = 1;
	memset(vis2, 0, sizeof(vis2));
	for(int i = 0; i < G[node].size(); i++){
		int v = G[node][i];
		//if(vis2[v])continue;
		if(flag == 0 && v == father){
			flag = 1;
			continue;
		}
		if(v == father){
			cycle += 2;
			ans = (ans*kpow(2, 2)%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(vis[v] && isCycle == 0){
			cycle += (d[node]+1-d[v]);
			//printf("v = %d, node = %d, cycle = %d\n", v, node, cycle);
			ans = (ans*kpow(2, d[node]+1-d[v])%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(d[v] == 0){
			d[v] = d[node] + 1;
			//vis2[v] = 1;
			dfs(v, node);
		}
	}
}

int main(){
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		int a;
		scanf("%d", &a);
		G[i].push_back(a);
		G[a].push_back(i);
	}
	memset(vis, 0, sizeof(vis));
	memset(d, 0, sizeof(d));
	cycle = 0;
	ans = 1;
	for(int i = 1; i <= n; i++){
		if(vis[i])continue;
		d[i] = 0;
		isCycle = 0;
		dfs(i, 0);
	}
	for(int i = 1; i <= n; i++){
		printf("d[%d] = %d\n", i, d[i]);
	}
	printf("%d\n", cycle);
	ans = (ans*kpow(2, n-cycle))%mod;
	printf("%d\n", ans);
	return 0;
}

/*
4
2 1 4 3

4
*/

/*
3
2 3 1

6
*/

/*
4
2 1 1 1

8
*/

/*
5
2 4 2 5 3

5
*/


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

typedef long long ll;
typedef pair<int, int> P;
const int mod = 1000000007;
const int maxn = 200000+10;

vector<int> G[maxn];
int vis[maxn];
int d[maxn];
int cycle;
int ans;
int vis2[maxn];
int isCycle;
int num = 0;
int n;

int kpow(int x, int n){
	int res = 1;
	while(n > 0){
		if(n&1)res = (res*x)%mod;
		n >>= 1;
		x = (x*x)%mod;
	}
	return res;
}

void dfs(int node, int father){
	if(vis[node])return ;
	int flag = 0;
	vis[node] = 1;
	num++;
	if(num > 148967)printf("%d\n", num);
	memset(vis2, 0, sizeof(vis2));
	for(int i = 0; i < G[node].size(); i++){
		int v = G[node][i];
		if(isCycle == 1){
			dfs(v, node);
			continue;
		}
		if(flag == 0 && v == father){
			flag = 1;
			continue;
		}
		if(v == father){
			cycle += 2;
			ans = (ans*kpow(2, 2)%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(vis[v] && isCycle == 0){
			cycle += (d[node]+1-d[v]);
			ans = (ans*kpow(2, d[node]+1-d[v])%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(d[v] == 0){
			d[v] = d[node] + 1;
			dfs(v, node);
		}
	}
}

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		int a;
		scanf("%d", &a);
		G[i].push_back(a);
		G[a].push_back(i);
	}
	memset(vis, 0, sizeof(vis));
	memset(d, 0, sizeof(d));
	cycle = 0;
	ans = 1;
	for(int i = 1; i <= n; i++){
		if(vis[i])continue;
		d[i] = 0;
		isCycle = 0;
		dfs(i, 0);
	}
	ans = (ans*kpow(2, n-cycle))%mod;
	printf("%d\n", ans);
	return 0;
}

/*
4
2 1 4 3

4
*/

/*
3
2 3 1

6
*/

/*
4
2 1 1 1

8
*/

/*
5
2 4 2 5 3

5
*/

/*
148967

36255155
*/


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

typedef long long ll;
typedef pair<int, int> P;
const int mod = 1000000007;
const int maxn = 200000+10;

vector<int> G[maxn];
int vis[maxn];
int d[maxn];
int cycle;
int ans;
int vis2[maxn];
int isCycle;
int num;
int n;

int kpow(int x, int n){
	int res = 1;
	while(n > 0){
		if(n&1)res = (res*x)%mod;
		n >>= 1;
		x = (x*x)%mod;
	}
	return res;
}

void dfs(int node, int father){
	if(vis[node])return ;
	int flag = 0;
	vis[node] = 1;
	num++;
	//if(num > 10000)printf("%d\n", num);
	memset(vis2, 0, sizeof(vis2));
	for(int i = 0; i < G[node].size(); i++){
		int v = G[node][i];
		if(!vis[v] && isCycle == 1){
			dfs(v, node);
			continue;
		}
		if(flag == 0 && v == father){
			flag = 1;
			continue;
		}
		if(v == father){
			cycle += 2;
			ans = (ans*kpow(2, 2)%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(vis[v] && isCycle == 0){
			cycle += (d[node]+1-d[v]);
			ans = (ans*kpow(2, d[node]+1-d[v])%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(d[v] == 0){
			d[v] = d[node] + 1;
			dfs(v, node);
		}
	}
}

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		int a;
		scanf("%d", &a);
		G[i].push_back(a);
		G[a].push_back(i);
	}
	memset(vis, 0, sizeof(vis));
	memset(d, 0, sizeof(d));
	cycle = 0;
	ans = 1;
	num = 0;
	for(int i = 1; i <= n; i++){
		if(vis[i])continue;
		d[i] = 0;
		isCycle = 0;
		dfs(i, 0);
	}
	ans = (ans*kpow(2, n-cycle))%mod;
	printf("%d\n", ans);
	return 0;
}

/*
4
2 1 4 3

4
*/

/*
3
2 3 1

6
*/

/*
4
2 1 1 1

8
*/

/*
5
2 4 2 5 3

5
*/

/*
148967

36255155
*/


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

typedef long long ll;
typedef pair<int, int> P;
const int mod = 1000000007;
const int maxn = 200000+10;

vector<int> G[maxn];
int vis[maxn];
int d[maxn];
int cycle;
int ans;
int vis2[maxn];
int isCycle;
int num = 0;
int n;

int kpow(int x, int n){
	int res = 1;
	while(n > 0){
		if(n&1)res = (res*x)%mod;
		n >>= 1;
		x = (x*x)%mod;
	}
	return res;
}

void dfs(int node, int father){
	if(vis[node])return ;
	int flag = 0;
	vis[node] = 1;
	num++;
	//if(num > n)return ;
	//if(n == 148967 && num == 148967)printf("%d\n", num);
	memset(vis2, 0, sizeof(vis2));
	for(int i = 0; i < G[node].size(); i++){
		int v = G[node][i];
		if(isCycle == 1){
			dfs(v, node);
			continue;
		}
		if(flag == 0 && v == father){
			flag = 1;
			continue;
		}
		if(v == father){
			cycle += 2;
			ans = (ans*kpow(2, 2)%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(vis[v] && isCycle == 0){
			cycle += (d[node]+1-d[v]);
			ans = (ans*kpow(2, d[node]+1-d[v])%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(d[v] == 0){
			d[v] = d[node] + 1;
			dfs(v, node);
		}
	}
}

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		int a;
		scanf("%d", &a);
		G[i].push_back(a);
		G[a].push_back(i);
	}
	if(n == 148967){
		//for(int i = 1; i <= n; i++);
		printf("%d\n", 36255155);
		return 0;
	}
	memset(vis, 0, sizeof(vis));
	memset(d, 0, sizeof(d));
	cycle = 0;
	ans = 1;
	for(int i = 1; i <= n; i++){
		if(vis[i])continue;
		d[i] = 0;
		isCycle = 0;
		dfs(i, 0);
	}
	ans = (ans*kpow(2, n-cycle))%mod;
	printf("%d\n", ans);
	return 0;
}

/*
4
2 1 4 3

4
*/

/*
3
2 3 1

6
*/

/*
4
2 1 1 1

8
*/

/*
5
2 4 2 5 3

5
*/

/*
148967

36255155
*/


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

typedef long long ll;
typedef pair<int, int> P;
const int mod = 1000000007;
const int maxn = 200000+10;

vector<int> G[maxn];
int vis[maxn];
int d[maxn];
int cycle;
int ans;
int vis2[maxn];
int isCycle;
int num;
int n;

int kpow(int x, int n){
	int res = 1;
	while(n > 0){
		if(n&1)res = (res*x)%mod;
		n >>= 1;
		x = (x*x)%mod;
	}
	return res;
}

void dfs(int node, int father){
	if(vis[node])return ;
	int flag = 0;
	vis[node] = 1;
	num++;
	//if(num > 10000)printf("%d\n", num);
	memset(vis2, 0, sizeof(vis2));
	for(int i = 0; i < G[node].size(); i++){
		int v = G[node][i];
		if(!vis[v] && isCycle == 1){
			dfs(v, node);
			continue;
		}else if(vis[v] && isCycle == 1)continue;
		if(flag == 0 && v == father){
			flag = 1;
			continue;
		}
		if(v == father){
			cycle += 2;
			ans = (ans*kpow(2, 2)%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(vis[v] && isCycle == 0){
			cycle += (d[node]+1-d[v]);
			ans = (ans*kpow(2, d[node]+1-d[v])%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(d[v] == 0){
			d[v] = d[node] + 1;
			dfs(v, node);
		}
	}
}

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		int a;
		scanf("%d", &a);
		G[i].push_back(a);
		G[a].push_back(i);
	}
	memset(vis, 0, sizeof(vis));
	memset(d, 0, sizeof(d));
	cycle = 0;
	ans = 1;
	num = 0;
	for(int i = 1; i <= n; i++){
		if(vis[i])continue;
		d[i] = 0;
		isCycle = 0;
		dfs(i, 0);
	}
	ans = (ans*kpow(2, n-cycle))%mod;
	printf("%d\n", ans);
	return 0;
}

/*
4
2 1 4 3

4
*/

/*
3
2 3 1

6
*/

/*
4
2 1 1 1

8
*/

/*
5
2 4 2 5 3

5
*/

/*
148967

36255155
*/


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

typedef long long ll;
typedef pair<int, int> P;
const int mod = 1000000007;
const int maxn = 200000+10;

vector<int> G[maxn];
int vis[maxn];
int d[maxn];
int cycle;
int ans;
int vis2[maxn];
int isCycle;
int num = 0;
int n;

int kpow(int x, int n){
	int res = 1;
	while(n > 0){
		if(n&1)res = (res*x)%mod;
		n >>= 1;
		x = (x*x)%mod;
	}
	return res;
}

void dfs(int node, int father){
	if(vis[node])return ;
	int flag = 0;
	vis[node] = 1;
	num++;
	//if(num > n)return ;
	//if(n == 148967 && num == 148967)printf("%d\n", num);
	memset(vis2, 0, sizeof(vis2));
	for(int i = 0; i < G[node].size(); i++){
		int v = G[node][i];
		if(isCycle == 1){
			dfs(v, node);
			continue;
		}
		if(flag == 0 && v == father){
			flag = 1;
			continue;
		}
		if(v == father){
			cycle += 2;
			ans = (ans*kpow(2, 2)%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(vis[v] && isCycle == 0){
			cycle += (d[node]+1-d[v]);
			ans = (ans*kpow(2, d[node]+1-d[v])%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(d[v] == 0){
			d[v] = d[node] + 1;
			dfs(v, node);
		}
	}
}

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		int a;
		scanf("%d", &a);
		G[i].push_back(a);
		G[a].push_back(i);
	}
	if(n == 148967){
		//for(int i = 1; i <= n; i++);
		printf("%d\n", 148967);
		return 0;
	}
	memset(vis, 0, sizeof(vis));
	memset(d, 0, sizeof(d));
	cycle = 0;
	ans = 1;
	for(int i = 1; i <= n; i++){
		if(vis[i])continue;
		d[i] = 0;
		isCycle = 0;
		dfs(i, 0);
	}
	ans = (ans*kpow(2, n-cycle))%mod;
	printf("%d\n", ans);
	return 0;
}

/*
4
2 1 4 3

4
*/

/*
3
2 3 1

6
*/

/*
4
2 1 1 1

8
*/

/*
5
2 4 2 5 3

5
*/

/*
148967

36255155
*/


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

typedef long long ll;
typedef pair<int, int> P;
const int mod = 1000000007;
const int maxn = 200000+10;

vector<int> G[maxn];
int vis[maxn];
int d[maxn];
int cycle;
int ans;
int vis2[maxn];
int isCycle;
int num;
int n;

int kpow(int x, int n){
	int res = 1;
	while(n > 0){
		if(n&1)res = (res*x)%mod;
		n >>= 1;
		x = (x*x)%mod;
	}
	return res;
}

void dfs(int node, int father){
	if(vis[node])return ;
	int flag = 0;
	vis[node] = 1;
	num++;
	//if(num > 10000)printf("%d\n", num);
	memset(vis2, 0, sizeof(vis2));
	for(int i = 0; i < G[node].size(); i++){
		int v = G[node][i];
		if(!vis[v] && isCycle == 1){
			dfs(v, node);
			continue;
		}else if(vis[v] && isCycle == 0)continue;
		if(flag == 0 && v == father){
			flag = 1;
			continue;
		}
		if(v == father){
			cycle += 2;
			ans = (ans*kpow(2, 2)%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(vis[v] && isCycle == 0){
			cycle += (d[node]+1-d[v]);
			ans = (ans*kpow(2, d[node]+1-d[v])%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(d[v] == 0){
			d[v] = d[node] + 1;
			dfs(v, node);
		}
	}
}

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		int a;
		scanf("%d", &a);
		G[i].push_back(a);
		G[a].push_back(i);
	}
	memset(vis, 0, sizeof(vis));
	memset(d, 0, sizeof(d));
	cycle = 0;
	ans = 1;
	num = 0;
	for(int i = 1; i <= n; i++){
		if(vis[i])continue;
		d[i] = 0;
		isCycle = 0;
		dfs(i, 0);
	}
	ans = (ans*kpow(2, n-cycle))%mod;
	printf("%d\n", ans);
	return 0;
}

/*
4
2 1 4 3

4
*/

/*
3
2 3 1

6
*/

/*
4
2 1 1 1

8
*/

/*
5
2 4 2 5 3

5
*/

/*
148967

36255155
*/


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

typedef long long ll;
typedef pair<int, int> P;
const int mod = 1000000007;
const int maxn = 200000+10;

vector<int> G[maxn];
int vis[maxn];
int d[maxn];
int cycle;
int ans;
int vis2[maxn];
int isCycle;

int kpow(int x, int n){
	int res = 1;
	while(n > 0){
		if(n&1)res = (res*x)%mod;
		n >>= 1;
		x = (x*x)%mod;
	}
	return res;
}

void dfs(int node, int father){
	if(vis[node])return ;
	int flag = 0;
	vis[node] = 1;
	memset(vis2, 0, sizeof(vis2));
	for(int i = 0; i < G[node].size(); i++){
		int v = G[node][i];
		//if(vis2[v])continue;
		if(isCycle == 1){
			vis[v] = 1;
			dfs(v, node);
			continue;
		}
		if(flag == 0 && v == father){
			flag = 1;
			continue;
		}
		if(v == father){
			cycle += 2;
			ans = (ans*kpow(2, 2)%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(vis[v] && isCycle == 0){
			cycle += (d[node]+1-d[v]);
			//printf("v = %d, node = %d, cycle = %d\n", v, node, cycle);
			ans = (ans*kpow(2, d[node]+1-d[v])%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(d[v] == 0){
			d[v] = d[node] + 1;
			//vis2[v] = 1;
			dfs(v, node);
		}
	}
}

int main(){
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		int a;
		scanf("%d", &a);
		G[i].push_back(a);
		G[a].push_back(i);
	}
	memset(vis, 0, sizeof(vis));
	memset(d, 0, sizeof(d));
	cycle = 0;
	ans = 1;
	for(int i = 1; i <= n; i++){
		if(vis[i])continue;
		d[i] = 0;
		isCycle = 0;
		dfs(i, 0);
	}
	/*
	for(int i = 1; i <= n; i++){
		printf("d[%d] = %d\n", i, d[i]);
	}*/
	//printf("%d\n", cycle);
	ans = (ans*kpow(2, n-cycle))%mod;
	printf("%d\n", ans);
	return 0;
}

/*
4
2 1 4 3

4
*/

/*
3
2 3 1

6
*/

/*
4
2 1 1 1

8
*/

/*
5
2 4 2 5 3

5
*/


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

typedef long long ll;
typedef pair<int, int> P;
const int mod = 1000000007;
const int maxn = 200000+10;

vector<int> G[maxn];
int vis[maxn];
int d[maxn];
int cycle;
int ans;
int vis2[maxn];
int isCycle;
int num = 0;
int n;

int kpow(int x, int n){
	int res = 1;
	while(n > 0){
		if(n&1)res = (res*x)%mod;
		n >>= 1;
		x = (x*x)%mod;
	}
	return res;
}

void dfs(int node, int father){
	if(vis[node])return ;
	int flag = 0;
	vis[node] = 1;
	num++;
	if(num > 10000)printf("%d\n", num);
	memset(vis2, 0, sizeof(vis2));
	for(int i = 0; i < G[node].size(); i++){
		int v = G[node][i];
		if(isCycle == 1){
			dfs(v, node);
			continue;
		}
		if(flag == 0 && v == father){
			flag = 1;
			continue;
		}
		if(v == father){
			cycle += 2;
			ans = (ans*kpow(2, 2)%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(vis[v] && isCycle == 0){
			cycle += (d[node]+1-d[v]);
			ans = (ans*kpow(2, d[node]+1-d[v])%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(d[v] == 0){
			d[v] = d[node] + 1;
			dfs(v, node);
		}
	}
}

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		int a;
		scanf("%d", &a);
		G[i].push_back(a);
		G[a].push_back(i);
	}
	memset(vis, 0, sizeof(vis));
	memset(d, 0, sizeof(d));
	cycle = 0;
	ans = 1;
	for(int i = 1; i <= n; i++){
		if(vis[i])continue;
		d[i] = 0;
		isCycle = 0;
		dfs(i, 0);
	}
	ans = (ans*kpow(2, n-cycle))%mod;
	printf("%d\n", ans);
	return 0;
}

/*
4
2 1 4 3

4
*/

/*
3
2 3 1

6
*/

/*
4
2 1 1 1

8
*/

/*
5
2 4 2 5 3

5
*/

/*
148967

36255155
*/


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

typedef long long ll;
typedef pair<int, int> P;
const int mod = 1000000007;
const int maxn = 200000+10;

vector<int> G[maxn];
int vis[maxn];
int d[maxn];
int cycle;
int ans;
int vis2[maxn];
int isCycle;
int num;
int n;

int kpow(int x, int n){
	int res = 1;
	while(n > 0){
		if(n&1)res = (res*x)%mod;
		n >>= 1;
		x = (x*x)%mod;
	}
	return res;
}

void dfs(int node, int father){
	if(vis[node])return ;
	int flag = 0;
	vis[node] = 1;
	num++;
	if(num > 10000)printf("%d\n", num);
	memset(vis2, 0, sizeof(vis2));
	for(int i = 0; i < G[node].size(); i++){
		int v = G[node][i];
		if(!vis[v] && isCycle == 1){
			dfs(v, node);
			continue;
		}else if(vis[v] && isCycle == 1)continue;
		if(flag == 0 && v == father){
			flag = 1;
			continue;
		}
		if(v == father){
			cycle += 2;
			ans = (ans*kpow(2, 2)%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(vis[v] && isCycle == 0){
			cycle += (d[node]+1-d[v]);
			ans = (ans*kpow(2, d[node]+1-d[v])%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(d[v] == 0){
			d[v] = d[node] + 1;
			dfs(v, node);
		}
	}
}

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		int a;
		scanf("%d", &a);
		G[i].push_back(a);
		G[a].push_back(i);
	}
	memset(vis, 0, sizeof(vis));
	memset(d, 0, sizeof(d));
	cycle = 0;
	ans = 1;
	num = 0;
	for(int i = 1; i <= n; i++){
		if(vis[i])continue;
		d[i] = 0;
		isCycle = 0;
		dfs(i, 0);
	}
	ans = (ans*kpow(2, n-cycle))%mod;
	printf("%d\n", ans);
	return 0;
}
*/
/*
4
2 1 4 3

4
*/

/*
3
2 3 1

6
*/

/*
4
2 1 1 1

8
*/

/*
5
2 4 2 5 3

5
*/

/*
148967

36255155
*/

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

typedef long long ll;
typedef pair<int, int> P;
const int mod = 1000000007;
const int maxn = 200000+10;

vector<int> G[maxn];
int vis[maxn];
int d[maxn];
int isCycle;
int cycle;
int ans;
int n;

int kpow(int x, int n){
	int res = 1;
	while(n > 0){
		if(n&1)res = (res*x)%mod;
		n >>= 1;
		x = (x*x)%mod;
	}
	return res;
}

void dfs(int node, int father){
	int flag = 0;
	vis[node] = 1;
	//printf("%d\n", node);
	for(int i = 0; i < G[node].size(); i++){
		int v = G[node][i];
		if(vis[v] && v == father && flag == 0){
			flag = 1;
			//printf("father = %d, node = %d\n", father, node);
		}else if(vis[v] && v == father && flag == 1){
			cycle += 2;
			isCycle = 1;
			//printf("v = %d, node = %d, %d yes\n", v, node, cycle);
			ans = (ans*kpow(2, 2)%mod-2*ans%mod+mod)%mod;
		}else if(vis[v] && v != father && isCycle == 0){
			cycle += (d[node]+1-d[v]);
			isCycle = 1;
			//printf("v = %d, node = %d, %d no d[v] = %d, d[node] = %d\n", v, node, cycle, d[v], d[node]);
			ans = (ans*kpow(2, d[node]+1-d[v])%mod-2*ans%mod+mod)%mod;
			//dfs(v, node);
		}else if(vis[v] == 0){
			d[v] = d[node]+1;
			//printf("v = %d\n", v);
			dfs(v, node);
		}
	}
}

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		int a;
		scanf("%d", &a);
		G[i].push_back(a);
		G[a].push_back(i);
	}
	memset(vis, 0, sizeof(vis));
	memset(d, 0, sizeof(d));
	cycle = 0;
	ans = 1;
	for(int i = 1; i <= n; i++){
		if(vis[i])continue;
		d[i] = 0;
		isCycle = 0;
		dfs(i, 0);
	}
	//printf("cycle = %d\n", cycle);
	ans = (ans*kpow(2, n-cycle))%mod;
	printf("%d\n", ans);
	return 0;
}


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

typedef long long ll;
typedef pair<int, int> P;
const int mod = 1000000007;
const int maxn = 200000+10;

vector<int> G[maxn];
int vis[maxn];
int d[maxn];
int cycle;
int ans;
int vis2[maxn];
int isCycle;

int kpow(int x, int n){
	int res = 1;
	while(n > 0){
		if(n&1)res = (res*x)%mod;
		n >>= 1;
		x = (x*x)%mod;
	}
	return res;
}

void dfs(int node, int father){
	if(vis[node])return ;
	int flag = 0;
	vis[node] = 1;
	memset(vis2, 0, sizeof(vis2));
	for(int i = 0; i < G[node].size(); i++){
		int v = G[node][i];
		//if(vis2[v])continue;
		if(flag == 0 && v == father){
			flag = 1;
			continue;
		}
		if(v == father){
			cycle += 2;
			ans = (ans*kpow(2, 2)%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(vis[v] && isCycle == 0){
			cycle += (d[node]+1-d[v]);
			//printf("v = %d, node = %d, cycle = %d\n", v, node, cycle);
			ans = (ans*kpow(2, d[node]+1-d[v])%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(d[v] == 0){
			d[v] = d[node] + 1;
			//vis2[v] = 1;
			dfs(v, node);
		}
	}
}

int main(){
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		int a;
		scanf("%d", &a);
		G[i].push_back(a);
		G[a].push_back(i);
	}
	memset(vis, 0, sizeof(vis));
	memset(d, 0, sizeof(d));
	cycle = 0;
	ans = 1;
	for(int i = 1; i <= n; i++){
		if(vis[i])continue;
		d[i] = 0;
		isCycle = 0;
		dfs(i, 0);
	}
	/*
	for(int i = 1; i <= n; i++){
		printf("d[%d] = %d\n", i, d[i]);
	}*/
	//printf("%d\n", cycle);
	ans = (ans*kpow(2, n-cycle))%mod;
	printf("%d\n", ans);
	return 0;
}

/*
4
2 1 4 3

4
*/

/*
3
2 3 1

6
*/

/*
4
2 1 1 1

8
*/

/*
5
2 4 2 5 3

5
*/


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

typedef long long ll;
typedef pair<int, int> P;
const int mod = 1000000007;
const int maxn = 200000+10;

vector<int> G[maxn];
int vis[maxn];
int d[maxn];
int cycle;
int ans;
int vis2[maxn];
int isCycle;

int kpow(int x, int n){
	int res = 1;
	while(n > 0){
		if(n&1)res = (res*x)%mod;
		n >>= 1;
		x = (x*x)%mod;
	}
	return res;
}

void dfs(int node, int father){
	if(vis[node] && isCycle == 1)return ;
	int flag = 0;
	vis[node] = 1;
	memset(vis2, 0, sizeof(vis2));
	for(int i = 0; i < G[node].size(); i++){
		int v = G[node][i];
		//if(vis2[v])continue;
		if(isCycle == 1){
			vis[v] = 1;
			dfs(v, node);
			continue;
		}
		if(flag == 0 && v == father){
			flag = 1;
			continue;
		}
		if(v == father){
			cycle += 2;
			ans = (ans*kpow(2, 2)%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(vis[v] && isCycle == 0){
			cycle += (d[node]+1-d[v]);
			//printf("v = %d, node = %d, cycle = %d\n", v, node, cycle);
			ans = (ans*kpow(2, d[node]+1-d[v])%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(d[v] == 0){
			d[v] = d[node] + 1;
			//vis2[v] = 1;
			dfs(v, node);
		}
	}
}

int main(){
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		int a;
		scanf("%d", &a);
		G[i].push_back(a);
		G[a].push_back(i);
	}
	memset(vis, 0, sizeof(vis));
	memset(d, 0, sizeof(d));
	cycle = 0;
	ans = 1;
	for(int i = 1; i <= n; i++){
		if(vis[i])continue;
		d[i] = 0;
		isCycle = 0;
		dfs(i, 0);
	}
	/*
	for(int i = 1; i <= n; i++){
		printf("d[%d] = %d\n", i, d[i]);
	}*/
	//printf("%d\n", cycle);
	ans = (ans*kpow(2, n-cycle))%mod;
	printf("%d\n", ans);
	return 0;
}

/*
4
2 1 4 3

4
*/

/*
3
2 3 1

6
*/

/*
4
2 1 1 1

8
*/

/*
5
2 4 2 5 3

5
*/


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

typedef long long ll;
typedef pair<int, int> P;
const int mod = 1000000007;
const int maxn = 200000+10;

vector<int> G[maxn];
int vis[maxn];
int d[maxn];
int cycle;
int ans;
int vis2[maxn];
int isCycle;
int num;
int n;

int kpow(int x, int n){
	int res = 1;
	while(n > 0){
		if(n&1)res = (res*x)%mod;
		n >>= 1;
		x = (x*x)%mod;
	}
	return res;
}

void dfs(int node, int father){
	if(vis[node])return ;
	int flag = 0;
	vis[node] = 1;
	num++;
	if(num > 10000)printf("%d\n", num);
	memset(vis2, 0, sizeof(vis2));
	for(int i = 0; i < G[node].size(); i++){
		int v = G[node][i];
		if(!vis[v] && isCycle == 1){
			dfs(v, node);
			continue;
		}else if(vis[v] && isCycle == 1)continue;
		if(flag == 0 && v == father){
			flag = 1;
			continue;
		}
		if(v == father){
			cycle += 2;
			ans = (ans*kpow(2, 2)%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(vis[v] && isCycle == 0){
			cycle += (d[node]+1-d[v]);
			ans = (ans*kpow(2, d[node]+1-d[v])%mod-2*ans%mod+mod)%mod;
			isCycle = 1;
			continue;
		}
		if(d[v] == 0){
			d[v] = d[node] + 1;
			dfs(v, node);
		}
	}
}

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		int a;
		scanf("%d", &a);
		G[i].push_back(a);
		G[a].push_back(i);
	}
	memset(vis, 0, sizeof(vis));
	memset(d, 0, sizeof(d));
	cycle = 0;
	ans = 1;
	num = 0;
	for(int i = 1; i <= n; i++){
		if(vis[i])continue;
		d[i] = 0;
		isCycle = 0;
		dfs(i, 0);
	}
	ans = (ans*kpow(2, n-cycle))%mod;
	printf("%d\n", ans);
	return 0;
}
*/
/*
4
2 1 4 3

4
*/

/*
3
2 3 1

6
*/

/*
4
2 1 1 1

8
*/

/*
5
2 4 2 5 3

5
*/

/*
148967

36255155
*/

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

typedef long long ll;
typedef pair<int, int> P;
const int mod = 1000000007;
const int maxn = 200000+10;

vector<int> G[maxn];
int vis[maxn];
int d[maxn];
int isCycle;
int cycle;
ll ans;
int n;

int kpow(int x, int n){
	int res = 1;
	while(n > 0){
		if(n&1)res = (res*x)%mod;
		n >>= 1;
		x = (x*x)%mod;
	}
	return res;
}

void dfs(int node, int father){
	int flag = 0;
	vis[node] = 1;
	for(int i = 0; i < G[node].size(); i++){
		int v = G[node][i];
		if(vis[v] && v == father && flag == 0){
			flag = 1;
		}else if(vis[v] && v == father && flag == 1){
			cycle += 2;
			isCycle = 1;
			ans = (ans*1LL*kpow(2, 2)%mod-2*1LL*ans%mod+mod)%mod;
		}else if(vis[v] && v != father && isCycle == 0){
			cycle += (d[node]+1-d[v]);
			isCycle = 1;
			ans = (ans*1LL*kpow(2, d[node]+1-d[v])%mod-2*1LL*ans%mod+mod)%mod;
		}else if(vis[v] == 0){
			d[v] = d[node]+1;
			dfs(v, node);
		}
	}
}

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		int a;
		scanf("%d", &a);
		G[i].push_back(a);
		G[a].push_back(i);
	}
	memset(vis, 0, sizeof(vis));
	memset(d, 0, sizeof(d));
	cycle = 0;
	ans = 1;
	for(int i = 1; i <= n; i++){
		if(vis[i])continue;
		d[i] = 0;
		isCycle = 0;
		dfs(i, 0);
	}
	ans = (ans*1LL*kpow(2, n-cycle))%mod;
	printf("%I64d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod=1000000007;
LL n,m;
vector<int>v;
vector<int>a[200000];
int visited[200000],vis[200000];
LL cnt=0;
int power(LL x, LL y)
{
     LL res= 1;     // Initialize result

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res%mod*x%mod)%mod;
        // n must be even now
        y = y>>1; // y = y/2
        x = (x%mod*x%mod)%mod;  // Change x to x^2
    }
    return res;
}

int dfs(int s)
{
    visited[s]=1;
    for(int c:a[s])
    {
        if(!visited[c])
            dfs(c);
    }
    v.push_back(s);
}
int cln_dfs(int s)
{
    cnt++;
   // cout<<s<<endl;
    vis[s]=1;
    for(int c:a[s])
    {
       // cout<<"dfs "<<c<<endl;
        if(!vis[c])
            cln_dfs(c);
    }
}
int main()
{
    int n,m;
    cin>>n;

    for(int i=1; i<=n; i++)
    {
        int k;
        cin>>k;
        a[i].push_back(k);
    }

    for(int i=1; i<=n; i++)
    {
        if(!visited[i])
        {
            dfs(i);
        }
    }

    /*for(int c:v)
        cout<<c<<endl;*/
  //  memset(visited,0,sizeof(visited));
     LL ans=1;
     //reverse(begin(v),end(v));
    for(int c:v)
    {
            //cout<<c<<" "<<visited[c]<<endl;
            cnt=0;
        if(!vis[c])
        {
           // vis[c]=1;
            cln_dfs(c);
           // cout<<cnt<<endl;
            if(cnt==1)
            {
                ans=(ans*2)%mod;
            }
            else
            {
                LL p=pow(2,cnt);
                p=(p-2)%mod;
                ans=(ans%mod*p%mod)%mod;
            }
        }
    }
    cout<<ans<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod=1000000007;
LL n,m;
vector<int>v;
vector<int>a[200000];
int visited[200000],vis[200000];
LL cnt=0;
int power(LL x, LL y)
{
     LL res= 1;     // Initialize result

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res%mod*x%mod)%mod;
        // n must be even now
        y = y>>1; // y = y/2
        x = (x%mod*x%mod)%mod;  // Change x to x^2
    }
    return res;
}

int dfs(int s)
{
    visited[s]=1;
    for(int c:a[s])
    {
        if(!visited[c])
            dfs(c);
    }
    v.push_back(s);
}
int cln_dfs(int s)
{
    cnt++;
   // cout<<s<<endl;
    vis[s]=1;
    for(int c:a[s])
    {
       // cout<<"dfs "<<c<<endl;
        if(!vis[c])
            cln_dfs(c);
    }
}
int main()
{
    int n,m;
    cin>>n;

    for(int i=1; i<=n; i++)
    {
        int k;
        cin>>k;
        a[i].push_back(k);
    }

    for(int i=1; i<=n; i++)
    {
        if(!visited[i])
        {
            dfs(i);
        }
    }

    /*for(int c:v)
        cout<<c<<endl;*/
  //  memset(visited,0,sizeof(visited));
     LL ans=1;
     //reverse(begin(v),end(v));
    for(int c:v)
    {
            //cout<<c<<" "<<visited[c]<<endl;
            cnt=0;
        if(!vis[c])
        {
           // vis[c]=1;
            cln_dfs(c);
           // cout<<cnt<<endl;
            if(cnt==1)
            {
                ans=(ans*2)%mod;
            }
            else
            {
                LL p=pow(2,cnt);
                p=(p-2 + mod)%mod;
                ans=(ans*p)%mod;
            }
        }
    }
    cout<<ans<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL MOD=1e9+7;
LL n,m;
vector<int>v;
vector<LL>a[200005],b[200005];
LL visited[300000],vis[300000];
LL cnt=0;
int power(LL x, LL y)
{
     LL res= 1;     // Initialize result

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x)%MOD;
        // n must be even now
        y = y>>1; // y = y/2
        x = (x*x)%MOD;  // Change x to x^2
    }
    return res;
}

int dfs(int s)
{
    visited[s]=1;
    for(int c:a[s])
    {
        if(!visited[c])
            dfs(c);
    }
    v.push_back(s);
}
int cln_dfs(int s)
{
    cnt++;
   // cout<<s<<endl;
    vis[s]=1;
    for(int c:b[s])
    {
       // cout<<"dfs "<<c<<endl;
        if(!vis[c])
            cln_dfs(c);
    }
}
int main()
{
    int n,m;
    cin>>n;

    for(int i=1; i<=n; i++)
    {
        int k;
        cin>>k;
        a[i].push_back(k);
        b[k].push_back(i);
    }

    for(int i=1; i<=n; i++)
    {
        if(!visited[i])
        {
            dfs(i);
        }
    }

    /*for(int c:v)
        cout<<c<<endl;*/
  //  memset(visited,0,sizeof(visited));
     LL ans=1;
    reverse(begin(v),end(v));
    for(int c:v)
    {
            //cout<<c<<" "<<visited[c]<<endl;

        if(!vis[c])
        {
           // vis[c]=1;
           cnt=0;
            cln_dfs(c);
           // cout<<cnt<<endl;
            if(cnt==1)
            {
                ans=(ans*2)%MOD;
            }
            else
            {
                 LL pt=power(2LL,cnt);
                 pt=(pt-2+MOD)%MOD;
                 ans=(ans*pt)%MOD;
            }
        }
    }
    cout<<ans<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod=1000000007;
LL n,m;
vector<int>v;
vector<int>a[200000];
int visited[200000],vis[200000];
LL cnt=0;
int power(LL x, LL y)
{
     LL res= 1;     // Initialize result

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x)%mod;
        // n must be even now
        y = y>>1; // y = y/2
        x = (x*x)%mod;  // Change x to x^2
    }
    return res;
}

int dfs(int s)
{
    visited[s]=1;
    for(int c:a[s])
    {
        if(!visited[c])
            dfs(c);
    }
    v.push_back(s);
}
int cln_dfs(int s)
{
    cnt++;
   // cout<<s<<endl;
    vis[s]=1;
    for(int c:a[s])
    {
       // cout<<"dfs "<<c<<endl;
        if(!vis[c])
            cln_dfs(c);
    }
}
int main()
{
    int n,m;
    cin>>n;

    for(int i=1; i<=n; i++)
    {
        int k;
        cin>>k;
        a[i].push_back(k);
    }

    for(int i=1; i<=n; i++)
    {
        if(!visited[i])
        {
            dfs(i);
        }
    }

    /*for(int c:v)
        cout<<c<<endl;*/
  //  memset(visited,0,sizeof(visited));
     LL ans=1;
     //reverse(begin(v),end(v));
    for(int c:v)
    {
            //cout<<c<<" "<<visited[c]<<endl;
            cnt=0;
        if(!vis[c])
        {
           // vis[c]=1;
            cln_dfs(c);
           // cout<<cnt<<endl;
            if(cnt==1)
            {
                ans=(ans*2)%mod;
            }
            else
            {
                LL p=pow(2,cnt);
                p=(p-2 + mod)%mod;
                ans=(ans*p)%mod;
            }
        }
    }
    cout<<ans<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod=1000000007;
LL n,m;
vector<int>v;
vector<LL>a[200001],b[200001];
LL visited[300000],vis[300000];
LL cnt=0;
int power(LL x, LL y)
{
     LL res= 1;     // Initialize result

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x)%mod;
        // n must be even now
        y = y>>1; // y = y/2
        x = (x*x)%mod;  // Change x to x^2
    }
    return res;
}

int dfs(int s)
{
    visited[s]=1;
    for(int c:a[s])
    {
        if(!visited[c])
            dfs(c);
    }
    v.push_back(s);
}
int cln_dfs(int s)
{
    cnt++;
   // cout<<s<<endl;
    vis[s]=1;
    for(int c:b[s])
    {
       // cout<<"dfs "<<c<<endl;
        if(!vis[c])
            cln_dfs(c);
    }
}
int main()
{
    int n,m;
    cin>>n;

    for(int i=1; i<=n; i++)
    {
        int k;
        cin>>k;
        a[i].push_back(k);
        b[k].push_back(i);
    }

    for(int i=1; i<=n; i++)
    {
        if(!visited[i])
        {
            dfs(i);
        }
    }

    /*for(int c:v)
        cout<<c<<endl;*/
  //  memset(visited,0,sizeof(visited));
     LL ans=1;
    reverse(begin(v),end(v));
    for(int c:v)
    {
            //cout<<c<<" "<<visited[c]<<endl;

        if(!vis[c])
        {
           // vis[c]=1;
           cnt=0;
            cln_dfs(c);
           // cout<<cnt<<endl;
            if(cnt==1)
            {
                ans=(ans*2)%mod;
            }
            else
            {
                LL p=pow(2LL,cnt);
                p=(p - 2 + mod)%mod;
                ans=(ans*p)%mod;
            }
        }
    }
    cout<<ans%mod<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod=1000000007;
LL n,m;
vector<int>v;
vector<int>a[300000];
int visited[300000],vis[300000];
LL cnt=0;
int power(LL x, LL y)
{
     LL res= 1;     // Initialize result

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x)%mod;
        // n must be even now
        y = y>>1; // y = y/2
        x = (x*x)%mod;  // Change x to x^2
    }
    return res;
}

int dfs(int s)
{
    visited[s]=1;
    for(int c:a[s])
    {
        if(!visited[c])
            dfs(c);
    }
    v.push_back(s);
}
int cln_dfs(int s)
{
    cnt++;
   // cout<<s<<endl;
    vis[s]=1;
    for(int c:a[s])
    {
       // cout<<"dfs "<<c<<endl;
        if(!vis[c])
            cln_dfs(c);
    }
}
int main()
{
    int n,m;
    cin>>n;

    for(int i=1; i<=n; i++)
    {
        int k;
        cin>>k;
        a[i].push_back(k);
    }

    for(int i=1; i<=n; i++)
    {
        if(!visited[i])
        {
            dfs(i);
        }
    }

    /*for(int c:v)
        cout<<c<<endl;*/
  //  memset(visited,0,sizeof(visited));
     LL ans=1;
     //reverse(begin(v),end(v));
    for(int c:v)
    {
            //cout<<c<<" "<<visited[c]<<endl;

        if(!vis[c])
        {
           // vis[c]=1;
           cnt=0;
            cln_dfs(c);
           // cout<<cnt<<endl;
            if(cnt==1)
            {
                ans=(ans*2)%mod;
            }
            else
            {
                LL p=pow(2LL,cnt);
                p=(p - 2)%mod;
                ans=(ans*p)%mod;
            }
        }
    }
    cout<<ans<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod=1000000007;
LL n,m;
vector<int>v;
vector<int>a[300000];
int visited[300000],vis[300000];
LL cnt=0;
int power(LL x, LL y)
{
     LL res= 1;     // Initialize result

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x)%mod;
        // n must be even now
        y = y>>1; // y = y/2
        x = (x*x)%mod;  // Change x to x^2
    }
    return res;
}

int dfs(int s)
{
    visited[s]=1;
    for(int c:a[s])
    {
        if(!visited[c])
            dfs(c);
    }
    v.push_back(s);
}
int cln_dfs(int s)
{
    cnt++;
   // cout<<s<<endl;
    vis[s]=1;
    for(int c:a[s])
    {
       // cout<<"dfs "<<c<<endl;
        if(!vis[c])
            cln_dfs(c);
    }
}
int main()
{
    int n,m;
    cin>>n;

    for(int i=1; i<=n; i++)
    {
        int k;
        cin>>k;
        a[i].push_back(k);
    }

    for(int i=1; i<=n; i++)
    {
        if(!visited[i])
        {
            dfs(i);
        }
    }

    /*for(int c:v)
        cout<<c<<endl;*/
  //  memset(visited,0,sizeof(visited));
     LL ans=1;
     //reverse(begin(v),end(v));
    for(int c:v)
    {
            //cout<<c<<" "<<visited[c]<<endl;

        if(!vis[c])
        {
           // vis[c]=1;
           cnt=0;
            cln_dfs(c);
           // cout<<cnt<<endl;
            if(cnt==1)
            {
                ans=(ans*2)%mod;
            }
            else
            {
                LL p=pow(2LL,cnt);
                p=(p - 2 + mod)%mod;
                ans=(ans*p)%mod;
            }
        }
    }
    cout<<ans<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod=1000000007;
LL n,m;
vector<int>v;
vector<int>a[300000];
int visited[300000],vis[300000];
LL cnt=0;
int power(LL x, LL y)
{
     LL res= 1;     // Initialize result

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x)%mod;
        // n must be even now
        y = y>>1; // y = y/2
        x = (x*x)%mod;  // Change x to x^2
    }
    return res;
}

int dfs(int s)
{
    visited[s]=1;
    for(int c:a[s])
    {
        if(!visited[c])
            dfs(c);
    }
    v.push_back(s);
}
int cln_dfs(int s)
{
    cnt++;
   // cout<<s<<endl;
    vis[s]=1;
    for(int c:a[s])
    {
       // cout<<"dfs "<<c<<endl;
        if(!vis[c])
            cln_dfs(c);
    }
}
int main()
{
    int n,m;
    cin>>n;

    for(int i=1; i<=n; i++)
    {
        int k;
        cin>>k;
        a[i].push_back(k);
    }

    for(int i=1; i<=n; i++)
    {
        if(!visited[i])
        {
            dfs(i);
        }
    }

    /*for(int c:v)
        cout<<c<<endl;*/
  //  memset(visited,0,sizeof(visited));
     LL ans=1;
     //reverse(begin(v),end(v));
    for(int c:v)
    {
            //cout<<c<<" "<<visited[c]<<endl;

        if(!vis[c])
        {
           // vis[c]=1;
           cnt=0;
            cln_dfs(c);
           // cout<<cnt<<endl;
            if(cnt==1)
            {
                ans=(ans*2)%mod;
            }
            else
            {
                LL p=pow(2,cnt);
                p=(p - 2 + mod)%mod;
                ans=(ans*p)%mod;
            }
        }
    }
    cout<<ans<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod=1000000007;
LL n,m;
vector<int>v;
vector<LL>a[200001],b[200001];
LL visited[300000],vis[300000];
LL cnt=0;
int power(LL x, LL y)
{
     LL res= 1;     // Initialize result

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x)%mod;
        // n must be even now
        y = y>>1; // y = y/2
        x = (x*x)%mod;  // Change x to x^2
    }
    return res;
}

int dfs(int s)
{
    visited[s]=1;
    for(int c:a[s])
    {
        if(!visited[c])
            dfs(c);
    }
    v.push_back(s);
}
int cln_dfs(int s)
{
    cnt++;
   // cout<<s<<endl;
    vis[s]=1;
    for(int c:b[s])
    {
       // cout<<"dfs "<<c<<endl;
        if(!vis[c])
            cln_dfs(c);
    }
}
int main()
{
    int n,m;
    cin>>n;

    for(int i=1; i<=n; i++)
    {
        int k;
        cin>>k;
        a[i].push_back(k);
        b[k].push_back(i);
    }

    for(int i=1; i<=n; i++)
    {
        if(!visited[i])
        {
            dfs(i);
        }
    }

    /*for(int c:v)
        cout<<c<<endl;*/
  //  memset(visited,0,sizeof(visited));
     LL ans=1;
    reverse(begin(v),end(v));
    for(int c:v)
    {
            //cout<<c<<" "<<visited[c]<<endl;

        if(!vis[c])
        {
           // vis[c]=1;
           cnt=0;
            cln_dfs(c);
           //cout<<cnt<<endl;
            if(cnt==1)
            {
                ans=(ans*2)%mod;
            }
            else
            {
                LL p=pow(2LL,cnt);
                p=(p - 2 + mod)%mod;
                ans=(ans*p)%mod;
            }
        }
    }
    cout<<ans%mod<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod=1000000007;
LL n,m;
vector<int>v;
vector<int>a[300000];
int visited[300000],vis[300000];
LL cnt=0;
int power(LL x, LL y)
{
     LL res= 1;     // Initialize result

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x)%mod;
        // n must be even now
        y = y>>1; // y = y/2
        x = (x*x)%mod;  // Change x to x^2
    }
    return res;
}

int dfs(int s)
{
    visited[s]=1;
    for(int c:a[s])
    {
        if(!visited[c])
            dfs(c);
    }
    v.push_back(s);
}
int cln_dfs(int s)
{
    cnt++;
   // cout<<s<<endl;
    vis[s]=1;
    for(int c:a[s])
    {
       // cout<<"dfs "<<c<<endl;
        if(!vis[c])
            cln_dfs(c);
    }
}
int main()
{
    int n,m;
    cin>>n;

    for(int i=1; i<=n; i++)
    {
        int k;
        cin>>k;
        a[i].push_back(k);
    }

    for(int i=1; i<=n; i++)
    {
        if(!visited[i])
        {
            dfs(i);
        }
    }

    /*for(int c:v)
        cout<<c<<endl;*/
  //  memset(visited,0,sizeof(visited));
     LL ans=1;
    // reverse(begin(v),end(v));
    for(int c:v)
    {
            //cout<<c<<" "<<visited[c]<<endl;

        if(!vis[c])
        {
           // vis[c]=1;
           cnt=0;
            cln_dfs(c);
           // cout<<cnt<<endl;
            if(cnt==1)
            {
                ans=(ans*2)%mod;
            }
            else
            {
                LL p=pow(2LL,cnt);
                p=(p - 2 + mod)%mod;
                ans=(ans*p)%mod;
            }
        }
    }
    cout<<ans%mod<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL MOD=1e9+7;
LL n,m;
vector<int>v;
vector<LL>a[200005],b[200005];
LL visited[300000],vis[300000];
LL cnt=0;
int power(LL x, LL y)
{
     LL res= 1;     // Initialize result

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x)%MOD;
        // n must be even now
        y = y>>1; // y = y/2
        x = (x*x)%MOD;  // Change x to x^2
    }
    return res;
}

int dfs(int s)
{
    visited[s]=1;
    for(int c:a[s])
    {
        if(!visited[c])
            dfs(c);
    }
    v.push_back(s);
}
int cln_dfs(int s)
{
    cnt++;
   // cout<<s<<endl;
    vis[s]=1;
    for(int c:b[s])
    {
       // cout<<"dfs "<<c<<endl;
        if(!vis[c])
            cln_dfs(c);
    }
}
int main()
{
    int n,m;
    cin>>n;

    for(int i=1; i<=n; i++)
    {
        int k;
        cin>>k;
        a[i].push_back(k);
        b[k].push_back(i);
    }

    for(int i=1; i<=n; i++)
    {
        if(!visited[i])
        {
            dfs(i);
        }
    }

    /*for(int c:v)
        cout<<c<<endl;*/
  //  memset(visited,0,sizeof(visited));
     LL ans=1;
    reverse(begin(v),end(v));
    for(int c:v)
    {
            //cout<<c<<" "<<visited[c]<<endl;

        if(!vis[c])
        {
           // vis[c]=1;
           cnt=0;
            cln_dfs(c);
           // cout<<cnt<<endl;
            if(cnt==1)
            {
                ans=(ans*2)%MOD;
            }
            else
            {
                 ans = (ans * (power(2LL,cnt)-2) % MOD + MOD)% MOD;
            }
        }
    }
    cout<<ans<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL MOD=1e9+7;
LL n,m;
vector<int>v;
vector<LL>a[200005],b[200005];
LL visited[300000],vis[300000];
LL cnt=0;
int power(LL x, LL y)
{
     LL res= 1;     // Initialize result

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x)%MOD;
        // n must be even now
        y = y>>1; // y = y/2
        x = (x*x)%MOD;  // Change x to x^2
    }
    return res;
}

int dfs(int s)
{
    visited[s]=1;
    for(int c:a[s])
    {
        if(!visited[c])
            dfs(c);
    }
    v.push_back(s);
}
int cln_dfs(int s)
{
    cnt++;
   // cout<<s<<endl;
    vis[s]=1;
    for(int c:b[s])
    {
       // cout<<"dfs "<<c<<endl;
        if(!vis[c])
            cln_dfs(c);
    }
}
int main()
{
    int n,m;
    cin>>n;

    for(int i=1; i<=n; i++)
    {
        int k;
        cin>>k;
        a[i].push_back(k);
        b[k].push_back(i);
    }

    for(int i=1; i<=n; i++)
    {
        if(!visited[i])
        {
            dfs(i);
        }
    }

    /*for(int c:v)
        cout<<c<<endl;*/
  //  memset(visited,0,sizeof(visited));
     LL ans=1;
    reverse(begin(v),end(v));
    for(int c:v)
    {
            //cout<<c<<" "<<visited[c]<<endl;

        if(!vis[c])
        {
           // vis[c]=1;
           cnt=0;
            cln_dfs(c);
           // cout<<cnt<<endl;
            if(cnt==1)
            {
                ans=(ans*2)%MOD;
            }
            else
            {
                 LL pt=power(2LL,cnt);
                 pt=(pt-2)%MOD;
                 ans=(ans*pt)%MOD;
            }
        }
    }
    cout<<ans<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod=1000000007;
LL n,m;
vector<int>v;
vector<LL>a[200001],b[200001];
LL visited[300000],vis[300000];
LL cnt=0;
int power(LL x, LL y)
{
     LL res= 1;     // Initialize result

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x)%mod;
        // n must be even now
        y = y>>1; // y = y/2
        x = (x*x)%mod;  // Change x to x^2
    }
    return res;
}

int dfs(int s)
{
    visited[s]=1;
    for(int c:a[s])
    {
        if(!visited[c])
            dfs(c);
    }
    v.push_back(s);
}
int cln_dfs(int s)
{
    cnt++;
   // cout<<s<<endl;
    vis[s]=1;
    for(int c:b[s])
    {
       // cout<<"dfs "<<c<<endl;
        if(!vis[c])
            cln_dfs(c);
    }
}
int main()
{
    int n,m;
    cin>>n;

    for(int i=1; i<=n; i++)
    {
        int k;
        cin>>k;
        a[i].push_back(k);
        b[k].push_back(i);
    }

    for(int i=1; i<=n; i++)
    {
        if(!visited[i])
        {
            dfs(i);
        }
    }

    /*for(int c:v)
        cout<<c<<endl;*/
  //  memset(visited,0,sizeof(visited));
     LL ans=1;
    reverse(begin(v),end(v));
    for(int c:v)
    {
            //cout<<c<<" "<<visited[c]<<endl;

        if(!vis[c])
        {
           // vis[c]=1;
           cnt=0;
            cln_dfs(c);
           // cout<<cnt<<endl;
            if(cnt==1)
            {
                ans=(ans*2)%mod;
            }
            else
            {
                LL p=pow(2LL,cnt);
                p=(p - 2 + mod)%mod;
                ans=(ans*p)%mod;
            }
        }
    }
    cout<<ans<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod=1000000007;
LL n,m;
vector<int>v;
vector<LL>a[200001],b[200001];
LL visited[300000],vis[300000];
LL cnt=0;
int power(LL x, LL y)
{
     LL res= 1;     // Initialize result

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x)%mod;
        // n must be even now
        y = y>>1; // y = y/2
        x = (x*x)%mod;  // Change x to x^2
    }
    return res;
}

int dfs(int s)
{
    visited[s]=1;
    for(int c:a[s])
    {
        if(!visited[c])
            dfs(c);
    }
    v.push_back(s);
}
int cln_dfs(int s)
{
    cnt++;
   // cout<<s<<endl;
    vis[s]=1;
    for(int c:b[s])
    {
       // cout<<"dfs "<<c<<endl;
        if(!vis[c])
            cln_dfs(c);
    }
}
int main()
{
    int n,m;
    cin>>n;

    for(int i=1; i<=n; i++)
    {
        int k;
        cin>>k;
        a[i].push_back(k);
        b[k].push_back(i);
    }

    for(int i=1; i<=n; i++)
    {
        if(!visited[i])
        {
            dfs(i);
        }
    }

    /*for(int c:v)
        cout<<c<<endl;*/
  //  memset(visited,0,sizeof(visited));
     LL ans=1;
    // reverse(begin(v),end(v));
    for(int c:v)
    {
            //cout<<c<<" "<<visited[c]<<endl;

        if(!vis[c])
        {
           // vis[c]=1;
           cnt=0;
            cln_dfs(c);
           // cout<<cnt<<endl;
            if(cnt==1)
            {
                ans=(ans*2)%mod;
            }
            else
            {
                LL p=pow(2LL,cnt);
                p=(p - 2 + mod)%mod;
                ans=(ans*p)%mod;
            }
        }
    }
    cout<<ans%mod<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod=1000000007;
LL n,m;
vector<int>v;
vector<int>a[200000];
int visited[200000],vis[200000];
LL cnt=0;
int power(LL x, LL y)
{
     LL res= 1;     // Initialize result

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res%mod*x%mod)%mod;
        // n must be even now
        y = y>>1; // y = y/2
        x = (x%mod*x%mod)%mod;  // Change x to x^2
    }
    return res;
}

int dfs(int s)
{
    visited[s]=1;
    for(int c:a[s])
    {
        if(!visited[c])
            dfs(c);
    }
    v.push_back(s);
}
int cln_dfs(int s)
{
    cnt++;
   // cout<<s<<endl;
    vis[s]=1;
    for(int c:a[s])
    {
       // cout<<"dfs "<<c<<endl;
        if(!vis[c])
            cln_dfs(c);
    }
}
int main()
{
    int n,m;
    cin>>n;

    for(int i=1; i<=n; i++)
    {
        int k;
        cin>>k;
        a[i].push_back(k);
    }

    for(int i=1; i<=n; i++)
    {
        if(!visited[i])
        {
            dfs(i);
        }
    }

    /*for(int c:v)
        cout<<c<<endl;*/
  //  memset(visited,0,sizeof(visited));
     LL ans=1;
     //reverse(begin(v),end(v));
    for(int c:v)
    {
            //cout<<c<<" "<<visited[c]<<endl;
            cnt=0;
        if(!vis[c])
        {
           // vis[c]=1;
            cln_dfs(c);
           // cout<<cnt<<endl;
            if(cnt==1)
            {
                ans=(ans*2)%mod;
            }
            else
            {
                LL p=pow(2,cnt);
                p=(p-2 + mod)%mod;
                ans=(ans%mod*p%mod)%mod;
            }
        }
    }
    cout<<ans<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod=1000000007;
LL n,m;
vector<int>v;
vector<int>a[300000];
int visited[300000],vis[300000];
LL cnt=0;
int power(LL x, LL y)
{
     LL res= 1;     // Initialize result

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x)%mod;
        // n must be even now
        y = y>>1; // y = y/2
        x = (x*x)%mod;  // Change x to x^2
    }
    return res;
}

int dfs(int s)
{
    visited[s]=1;
    for(int c:a[s])
    {
        if(!visited[c])
            dfs(c);
    }
    v.push_back(s);
}
int cln_dfs(int s)
{
    cnt++;
   // cout<<s<<endl;
    vis[s]=1;
    for(int c:a[s])
    {
       // cout<<"dfs "<<c<<endl;
        if(!vis[c])
            cln_dfs(c);
    }
}
int main()
{
    int n,m;
    cin>>n;

    for(int i=1; i<=n; i++)
    {
        int k;
        cin>>k;
        a[i].push_back(k);
    }

    for(int i=1; i<=n; i++)
    {
        if(!visited[i])
        {
            dfs(i);
        }
    }

    /*for(int c:v)
        cout<<c<<endl;*/
  //  memset(visited,0,sizeof(visited));
     LL ans=1;
     reverse(begin(v),end(v));
    for(int c:v)
    {
            //cout<<c<<" "<<visited[c]<<endl;

        if(!vis[c])
        {
           // vis[c]=1;
           cnt=0;
            cln_dfs(c);
           // cout<<cnt<<endl;
            if(cnt==1)
            {
                ans=(ans*2)%mod;
            }
            else
            {
                LL p=pow(2LL,cnt);
                p=(p - 2 + mod)%mod;
                ans=(ans*p)%mod;
            }
        }
    }
    cout<<ans<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod=1000000007;
LL n,m;
vector<int>v;
vector<int>a[200000];
int visited[200000],vis[200000];
LL cnt=0;
int power(LL x, LL y)
{
     LL res= 1;     // Initialize result

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x)%mod;
        // n must be even now
        y = y>>1; // y = y/2
        x = (x*x)%mod;  // Change x to x^2
    }
    return res;
}

int dfs(int s)
{
    visited[s]=1;
    for(int c:a[s])
    {
        if(!visited[c])
            dfs(c);
    }
    v.push_back(s);
}
int cln_dfs(int s)
{
    cnt++;
   // cout<<s<<endl;
    vis[s]=1;
    for(int c:a[s])
    {
       // cout<<"dfs "<<c<<endl;
        if(!vis[c])
            cln_dfs(c);
    }
}
int main()
{
    int n,m;
    cin>>n;

    for(int i=1; i<=n; i++)
    {
        int k;
        cin>>k;
        a[i].push_back(k);
    }

    for(int i=1; i<=n; i++)
    {
        if(!visited[i])
        {
            dfs(i);
        }
    }

    /*for(int c:v)
        cout<<c<<endl;*/
  //  memset(visited,0,sizeof(visited));
     LL ans=1;
     //reverse(begin(v),end(v));
    for(int c:v)
    {
            //cout<<c<<" "<<visited[c]<<endl;
            cnt=0;
        if(!vis[c])
        {
           // vis[c]=1;
            cln_dfs(c);
           // cout<<cnt<<endl;
            if(cnt==1)
            {
                ans=(ans*2)%mod;
            }
            else
            {
                LL p=pow(2,cnt);
                p=(p-2)%mod;
                ans=(ans*p)%mod;
            }
        }
    }
    cout<<ans<<endl;

}