#include <iostream>
#include <fstream>
#include <cstdio>
#include <iomanip>
#include <utility>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int N = 500500;
const int mod = 1000000000;
int n, a[N];
long long ans = 0;
int main ()
{
scanf("%d", &n);
for (int i = 1; i <= n; ++ i) {
scanf("%d", &a[i]);
}
for (int i = 1; i <= n; ++ i) {
int mn = a[i], mx = a[i];
for (int j = i; j <= n; ++ j) {
mn = min(mn, a[j]);
mx = max(mx, a[j]);
long long p = (mn * 1ll * mx) % mod;
p = (p * (j - i + 1)) % mod;
ans += p;
if (ans >= mod) ans -= mod;
}
}
cout << ans;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8ZnN0cmVhbT4KI2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGlvbWFuaXA+CiNpbmNsdWRlIDx1dGlsaXR5PgojaW5jbHVkZSA8Y3N0ZGxpYj4KI2luY2x1ZGUgPGFsZ29yaXRobT4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKY29uc3QgaW50IE4gPSA1MDA1MDA7CmNvbnN0IGludCBtb2QgPSAxMDAwMDAwMDAwOwppbnQgbiwgYVtOXTsKbG9uZyBsb25nIGFucyA9IDA7CmludCBtYWluICgpCnsKCglzY2FuZigiJWQiLCAmbik7CgkKCWZvciAoaW50IGkgPSAxOyBpIDw9IG47ICsrIGkpIHsKCQlzY2FuZigiJWQiLCAmYVtpXSk7Cgl9CgoJZm9yIChpbnQgaSA9IDE7IGkgPD0gbjsgKysgaSkgewoJCWludCBtbiA9IGFbaV0sIG14ID0gYVtpXTsKCQlmb3IgKGludCBqID0gaTsgaiA8PSBuOyArKyBqKSB7CgkJCW1uID0gbWluKG1uLCBhW2pdKTsKCQkJbXggPSBtYXgobXgsIGFbal0pOwoJCQlsb25nIGxvbmcgcCA9IChtbiAqIDFsbCAqIG14KSAlIG1vZDsKCQkJcCA9IChwICogKGogLSBpICsgMSkpICUgbW9kOwoJCQlhbnMgKz0gcDsKCQkJaWYgKGFucyA+PSBtb2QpIGFucyAtPSBtb2Q7CgkJfQoJfQoKCWNvdXQgPDwgYW5zOwoKCXJldHVybiAwOwp9