#include using namespace std; #define rep(i,n) for(int i=0; i<(n); i++) #define INF ((1LL<<62)-(1LL<<31)) #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() typedef long long ll; typedef pair pl; //const ll mod=1000000007; int main() { int n; cin >> n; vector p(n); rep(i,n) { cin >> p[i]; p[i]--; } reverse(all(p)); vector flag(n,false); ll ans=0; rep(i,n) { if(p[i]==n-1) { if(flag[p[i]]) ans-=n-i; else flag[p[i]]=true; } else { ans+=n-i; flag[p[i]+1]=true; } } cout << ans << endl; return 0; }