#include #include #include "testlib.h" using namespace std; using ll = long long; using ull = unsigned long long; #define rep(i,n) for(int i=0; i<(n); i++) int main(){ registerValidation(); int N = inf.readInt(1,200000); inf.readEoln(); vector A(N); rep(i,N){ if(i!=0) inf.readChar(' '); A[i] = inf.readInt(0,N-1); } inf.readEoln(); inf.readEof(); vector P(N,-1); rep(i,N) P[A[i]] = i; rep(i,N) ensure(P[i] != -1); vector L = P; vector R = P; rep(i,N-1) L[i+1] = min(L[i],L[i+1]); rep(i,N-1) R[i+1] = max(R[i],R[i+1]); ll ans = 0; rep(i,N) ans += 1ll * (L[i]+1) * (N-R[i]); cout << ans << "\n"; return 0; } struct ios_do_not_sync{ ios_do_not_sync(){ ios::sync_with_stdio(false); cin.tie(nullptr); } } ios_do_not_sync_instance;