import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; class Main { class BIT { int n; long[] v; public BIT(int n_) { n = n_; v = new long[n + 1]; } void add(int k, int val) { while (k <= n) { v[k] += val; k += k & -k; } } long sum(int k) { long ret = 0; while (k >= 1) { ret += v[k]; k -= k & -k; } return ret; } long sum(int l, int r) { return sum(r - 1) - sum(l - 1); } } void run() { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int[] M = new int[N]; SegmentTree seg1 = new SegmentTree(N); SegmentTree seg2 = new SegmentTree(N); SegmentTree seg3 = new SegmentTree(N); long ans = 0; for (int i = 0; i < N; ++i) { M[i] = sc.nextInt(); --M[i]; if (i < M[i]) { // 同じ方向 ans += seg1.query(M[i], N); seg1.add(M[i]); } else if (i > M[i]) { // 同じ方向 ans += seg2.query(M[i], i + 1); // 内側方向 ans += seg1.query(M[i], N); seg2.add(M[i]); } else seg3.add(M[i]); } // 動かない猫との挨拶をカウント for (int i = 0; i < N; ++i) { if (i != M[i]) { ans += seg3.query(Math.min(i, M[i]), Math.max(i, M[i]) + 1); } else { ans += seg3.query(i, i + 1) - 1; } } PrintWriter pw = new PrintWriter(System.out); pw.println(ans); pw.close(); } class SegmentTree { int n; int[] sum; public SegmentTree(int n_) { n = 1; while (n < n_) { n *= 2; } sum = new int[2 * n - 1]; } void add(int k) { k += n - 1; ++sum[k]; while (k > 0) { k = (k - 1) / 2; sum[k] = sum[2 * k + 1] + sum[2 * k + 2]; } } int query(int a, int b) { return query(0, n, a, b, 0); } int query(int l, int r, int a, int b, int k) { if (r <= a || b <= l) return 0; else if (a <= l && r <= b) return sum[k]; else { return query(l, (l + r) / 2, a, b, 2 * k + 1) + query((l + r) / 2, r, a, b, 2 * k + 2); } } } void tr(Object... objects) { System.out.println(Arrays.deepToString(objects)); } public static void main(String[] args) throws FileNotFoundException { new Main().run(); } }