func_code_string: #include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 5, mod = 1e9 + 7;   int a[N], st[N][20], pos[N][20];   int seg_query(int l, int r) {     int k = log2(r - l + 1);     if (st[l][k] > st[r - (1 << k) + 1][k])       return pos[l][k];     else       return pos[r - (1 << k) + 1][k];   }   long long f[N];   signed main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n - 1; ++i)       scanf( %d , a + i), st[i][0] = a[i], pos[i][0] = i;     a[n] = n, st[n][0] = pos[n][0] = n;     for (int j = 1; j <= 17; ++j)       for (int i = 1; i + (1 << j) - 1 <= n; ++i)         if (st[i][j - 1] > st[i + (1 << (j - 1))][j - 1])           st[i][j] = st[i][j - 1], pos[i][j] = pos[i][j - 1];         else           st[i][j] = st[i + (1 << (j - 1))][j - 1],           pos[i][j] = pos[i + (1 << (j - 1))][j - 1];     long long ans = 0;     for (int i = n - 1; i >= 1; --i) {       int k = seg_query(i + 1, a[i]);       f[i] = f[k] + n - i - (a[i] - k);       ans += f[i];     }     cout << ans;   }   