#include using namespace std; typedef long long LL; const int N=80010; int a[N]; int res[N]; stack stk; int n; LL ans; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; while (!stk.empty() && a[stk.top()] <= a[i]) { res[stk.top()] = i; stk.pop(); } stk.push(i); } for (int i = 1; i <= n; i++) { if (res[i] == 0) ans += n - i; else ans += res[i] - i - 1; } cout << ans; }