#include using namespace std; #define REP(i,n) for (int i=0;i<(n);i++) #define REP2(i,m,n) for (int i=m;i<(n);i++) typedef long long ll; typedef long double ld; const ll MOD = 1000000007; int N, K; pair LR[30303]; pair RL[30303]; int same[30303]; void solve() { int N; cin >> N; int x = 0, y = 0, z = 0; REP2(i, 1, N+1) { int m; cin >> m; if (i < m) LR[x++] = make_pair(i, m); else if (m < i) RL[y++] = make_pair(m, i); else same[z++] = i; } int ans = 0; sort(LR, LR+x); sort(RL, RL+y); REP(i, x) REP2(j, i+1, x) { if (LR[j].second <= LR[i].second) ans++; } REP(i, y) REP2(j, i+1, y) { if (RL[j].second <= RL[i].second) ans++; } REP(i, x) REP(j, z) { if (LR[i].first <= same[j] && same[j] <= LR[i].second) ans++; } REP(i, y) REP(j, z) { if (RL[i].first <= same[j] && same[j] <= RL[i].second) ans++; } REP(i, x) REP(j, y) { if (LR[i].first <= RL[j].second && LR[i].second >= RL[j].first) ans++; } cout << ans << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); }