This should be the answer of this question.
Correct me if I am wrong.
ll n;
cin>>n;
vector<int> v(n+1);
for(int i=1; i<=n; i++){
cin>>v[i];
}
map<int,int> mp; ll ans=0;
for(int i=1; i<=n; i++){
mp[v[v[v[i]]]]++;
// ans=max(ans,mp[v[v[v[i]]]]);
}
for(auto it: mp){
ll x=it.ss;
ans+=(x)*(x-1)/2;
}
cout<<ans<<'\n';