#include #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; int main() { int n; cin >> n; vector a(n); rep(i, n) cin >> a[i]; vector pos(n); rep(i, n) pos[a[i]] = i; ll ans = 0; int l = n, r = -1; rep(k, n) { l = min(l, pos[k]); r = max(r, pos[k]); ans += ll(l+1)*(n-r); } cout << ans << '\n'; return 0; }