/*
*  <|--- <<_||_>> ---|>
*  <---  >>-\\-<<  --->
*  <|--- <<_||_>> ---|>
*/
// problem 1 --> Balanced Intervals
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
    tree_order_statistics_node_update> ordered_set;

#define el     '\n'
#define int long long
const int mod = 1000000000 + 7;

void solve() {
    int n;
    cin >> n;

    vector<int> a(n + 1);
    vector<vector<int> > pos(n + 1);
    set<int> bad;

    bad.insert(0);
    bad.insert(n + 1);

    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        if (a[i] > n) {
            bad.insert(i);
        } else {
            pos[a[i]].push_back(i);
        }
    }

    int total_balanced = 0;
    for (int v = n; v >= 1; --v) {
        int k = pos[v].size();

        for (int j = 0; j <= k - v; ++j) {
            int L_pos = pos[v][j];
            int R_pos = pos[v][j + v - 1];
            auto it = bad.upper_bound(L_pos);
            int B_next = *it;

            if (B_next < R_pos) {
                continue;
            }

            int B_L = *prev(it);
            int B_R = B_next;
            int prev_v_pos = (j > 0) ? pos[v][j - 1] : 0;
            int next_v_pos = (j + v < k) ? pos[v][j + v] : n + 1;

            int limit_L = max(B_L, prev_v_pos);
            int limit_R = min(B_R, next_v_pos);
            total_balanced += 1LL * (L_pos - limit_L) * (limit_R - R_pos);
        }

        for (int p: pos[v]) {
            bad.insert(p);
        }
    }

    cout << total_balanced << el;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cout.tie(nullptr), cin.tie(nullptr);

    int tt = 1;
    //cin >> tt;
    while (tt--) {
        solve();
    }
    return 0;
}
