#pragma GCC optimize("O3", "unroll-loops")
// God Help me !!
#include <bits/stdc++.h>
using namespace std;
// #define watch(x) cout << (#x) << " is " << (x) << endl
#define FILES freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout)
#define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define FIXED cout << fixed << setprecision(20)
#define RANDOM srand(time(nullptr))
#define int long long
#define sz(a) (int)a.size()
#define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++)
#define sep(i,a,b) for(int i=(int)a;i>=(int)b;i--)
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(),a.end()
#define x first
#define y second
#define endl "\n"
#define n6 3000005
#define n3 3005
#define n5 300005
#define pi pair<int,int>
#define pii pair<int,pi>
#define dbg1(x) cout << #x << ": " << x << endl;
#define dbg2(x, y) cout << #x << ": " << x << " | " << #y << ": " << y << endl;
#define dbg3(x, y, z) cout << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define dbg4(a, b, c, d) cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define dbg5(a, b, c, d, e) cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define dbg6(a, b, c, d, e, f) cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define dbgv(x) cout << #x << " = " << x << endl
#define dbga(arr, len) {cout << #arr << " = "; for (int _ = 0; _ < len; _++)\
cout << arr[_] << " "; cout << endl;}
#define dbgi(it) {cout << #it << " = "; for (const auto& _ : it)\
cout << _ << " "; cout << endl;}
const int md = (int) 1e9 + 7;
inline void add(int &a, int b) {
a += b;
if (a >= md) a -= md;
}
inline void sub(int &a, int b) {
a -= b;
if (a < 0) a += md;
}
inline int mul(int a, int b) {
return (int) ((long long) a * b % md);
}
inline int power(int a, long long b) {
int res = 1;
while (b > 0) {
if (b & 1) {
res = mul(res, a);
}
a = mul(a, a);
b >>= 1;
}
return res;
}
void solve_util(int m, int &cnt, int &sum){
}
const int K = 27;
int st[n5][K];
int st2[n5][K];
int arr[n5];
int N;
int logi[n5];
void precompute_log(){
logi[1] = 0;
for (int i = 2; i <= n5; i++)
logi[i] = logi[i/2] + 1;
}
int range_sum(int L, int R){
long long sum = 0;
for (int j = K; j >= 0; j--) {
if ((1 << j) <= R - L + 1) {
sum += st[L][j];
L += 1 << j;
}
}
return sum;
}
void RMQ(){
for (int i = 0; i < N; i++)
{
st[i][0] = arr[i];
st2[i][0] = arr[i];
}
for (int j = 1; j <= K; j++)
for (int i = 0; i + (1 << j) <= N; i++){
st[i][j] = max(st[i][j-1], st[i + (1 << (j - 1))][j - 1]);
st2[i][j] = min(st2[i][j-1], st2[i + (1 << (j - 1))][j - 1]);
}
}
// int ans = 0;
int RMQutil(int L, int R){
int j = logi[R - L + 1];
int maxi = max(st[L][j], st[R - (1 << j) + 1][j]);
return maxi;
}
int RMQutil2(int L, int R){
int j = logi[R - L + 1];
int maxi = min(st2[L][j], st2[R - (1 << j) + 1][j]);
return maxi;
}
void solve(){
cin>>N;
int n =N;
rep(i,0,n-1)cin>>arr[i];
RMQ();
// dbga(arr,n);
int ans =0;
rep(i,0,n-1){
rep(j,i+1,n-1){
int maxi = RMQutil(i,j);
int mini = RMQutil2(i,j);
ans+=maxi-mini;
}
}
cout<<ans<<endl;
}
signed main()
{
FAST; FIXED; RANDOM;
int t=1;
precompute_log();
// cin>>t;
// time_t time_t1, time_t2;
// time_t1 = clock();
while(t--)
solve();
// time_t2 = clock();
// cout << "time taken :" << time_t2 - time_t1 << endl;
return 0;
}
I3ByYWdtYSBHQ0Mgb3B0aW1pemUoIk8zIiwgInVucm9sbC1sb29wcyIpCi8vIEdvZCBIZWxwIG1l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VdW0tdOwppbnQgYXJyW241XTsKaW50IE47CgppbnQgbG9naVtuNV07CnZvaWQgcHJlY29tcHV0ZV9sb2coKXsKICAgIGxvZ2lbMV0gPSAwOwogICAgZm9yIChpbnQgaSA9IDI7IGkgPD0gbjU7IGkrKykKICAgICAgICBsb2dpW2ldID0gbG9naVtpLzJdICsgMTsKfQoKCmludCByYW5nZV9zdW0oaW50IEwsIGludCBSKXsKICAgIGxvbmcgbG9uZyBzdW0gPSAwOwogICAgZm9yIChpbnQgaiA9IEs7IGogPj0gMDsgai0tKSB7CiAgICAgICAgaWYgKCgxIDw8IGopIDw9IFIgLSBMICsgMSkgewogICAgICAgICAgICBzdW0gKz0gc3RbTF1bal07CiAgICAgICAgICAgIEwgKz0gMSA8PCBqOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiBzdW07Cn0gCgoKdm9pZCBSTVEoKXsgIAogICAgCiAgICBmb3IgKGludCBpID0gMDsgaSA8IE47IGkrKykKICAgICAgICB7CiAgICAgICAgICAgIHN0W2ldWzBdID0gYXJyW2ldOwogICAgICAgICAgICBzdDJbaV1bMF0gPSBhcnJbaV07CiAgICAgICAgfQogICAgCiAgICBmb3IgKGludCBqID0gMTsgaiA8PSBLOyBqKyspCiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgKyAoMSA8PCBqKSA8PSBOOyBpKyspewogICAgICAgICAgICBzdFtpXVtqXSA9IG1heChzdFtpXVtqLTFdLCBzdFtpICsgKDEgPDwgKGogLSAxKSldW2ogLSAxXSk7CiAgICAgICAgICAgIHN0MltpXVtqXSA9IG1pbihzdDJbaV1bai0xXSwgc3QyW2kgKyAoMSA8PCAoaiAtIDEpKV1baiAtIDFdKTsKICAgICAgICB9CiAgICAKfQoKLy8gaW50IGFucyA9IDA7CmludCBSTVF1dGlsKGludCBMLCBpbnQgUil7CgogICAgaW50IGogPSBsb2dpW1IgLSBMICsgMV07CiAgICBpbnQgbWF4aSA9IG1heChzdFtMXVtqXSwgc3RbUiAtICgxIDw8IGopICsgMV1bal0pOwogICAgcmV0dXJuIG1heGk7CiAgICAKfQogCmludCBSTVF1dGlsMihpbnQgTCwgaW50IFIpewoKICAgIGludCBqID0gbG9naVtSIC0gTCArIDFdOwogICAgaW50IG1heGkgPSBtaW4oc3QyW0xdW2pdLCBzdDJbUiAtICgxIDw8IGopICsgMV1bal0pOwogICAgcmV0dXJuIG1heGk7Cn0KCnZvaWQgc29sdmUoKXsKICAgIGNpbj4+TjsKICAgIGludCBuID1OOyAKICAgIHJlcChpLDAsbi0xKWNpbj4+YXJyW2ldOwogICAgCiAgICBSTVEoKTsKICAgIC8vIGRiZ2EoYXJyLG4pOwogICAgaW50IGFucyA9MDsKICAgIHJlcChpLDAsbi0xKXsKICAgICAgICByZXAoaixpKzEsbi0xKXsKICAgICAgICAgICAgaW50IG1heGkgPSBSTVF1dGlsKGksaik7CiAgICAgICAgICAgIGludCBtaW5pID0gUk1RdXRpbDIoaSxqKTsKICAgICAgICAgICAgYW5zKz1tYXhpLW1pbmk7CiAgICAgICAgfQogICAgfQoKICAgIGNvdXQ8PGFuczw8ZW5kbDsKfQogCnNpZ25lZCBtYWluKCkKewogICAgRkFTVDsgRklYRUQ7IFJBTkRPTTsKICAgIGludCB0PTE7CiAgICBwcmVjb21wdXRlX2xvZygpOwogICAgLy8gY2luPj50OwogICAgLy8gdGltZV90IHRpbWVfdDEsIHRpbWVfdDI7CiAgICAvLyB0aW1lX3QxID0gY2xvY2soKTsKICAgIHdoaWxlKHQtLSkKICAgICAgICBzb2x2ZSgpOwogCiAgICAvLyB0aW1lX3QyID0gY2xvY2soKTsKICAgIC8vIGNvdXQgPDwgInRpbWUgdGFrZW4gOiIgPDwgdGltZV90MiAtIHRpbWVfdDEgPDwgZW5kbDsKICAgIHJldHVybiAwOwp9IA==