#include using namespace std; #define ll long long #define fastio ios::sync_with_stdio(0); cin.tie(0); signed main() { fastio int n; cin >> n; vector a(n + 5); vector pos(n + 5); for (int i = 0; i < n; i++) { cin >> a[i]; pos[a[i]] = i; } ll ans = 0; int l = pos[0], r = l; for (int i = 1; i < n; i++) { int p = pos[i]; if (p < l) { ans += i * (l - p) * (n - r); } else if (p > r) { ans += i * (l + 1) * (p - r); } r = max(r, p); l = min(l, p); } ans += n; cout << ans; return(0); }