1 //It is made by Awson on 2017.10.3
2 #include <set>
3 #include <map>
4 #include <cmath>
5 #include <ctime>
6 #include <queue>
7 #include <stack>
8 #include <vector>
9 #include <cstdio>
10 #include <string>
11 #include <cstring>
12 #include <cstdlib>
13 #include <iostream>
14 #include <algorithm>
15 #define LL long long
16 #define Max(a, b) ((a) > (b) ? (a) : (b))
17 #define Min(a, b) ((a) < (b) ? (a) : (b))
18 #define sqr(x) ((x)*(x))
19 #define lowbit(x) ((x)&(-(x)))
20 #define count COUNT
21 using namespace std;
22 const int N = 300000;
23 const LL MOD = 19260817;
24 void read(LL &x) {
25 char ch; bool flag = 0;
26 for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == ‘-‘)) || 1); ch = getchar());
27 for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
28 x *= 1-2*flag;
29 }
30
31 int n;
32 struct tt {
33 LL val;
34 int pos, rank;
35 }a[N+5];
36 LL sum[N+5], c[N+5], ans;
37 bool comp(const tt &a, const tt &b) {
38 return a.val < b.val;
39 }
40 bool accomp(const tt &a, const tt &b) {
41 return a.pos < b.pos;
42 }
43
44 void add(int x, LL key) {
45 for (; x <= n; x += lowbit(x)) c[x] = (c[x]+key)%MOD;
46 }
47 LL count(int x) {
48 LL cnt = 0;
49 for (; x; x -= lowbit(x)) cnt = (cnt+c[x])%MOD;
50 return cnt;
51 }
52 void work() {
53 scanf("%d", &n);
54 for (int i = 1; i <= n; i++) {
55 read(a[i].val); a[i].pos = i;
56 }
57 sort(a+1, a+n+1, comp);
58 a[1].rank = 1;
59 for (int i = 2; i <= n; i++)
60 a[i].rank = a[i-1].rank+(a[i].val != a[i-1].val);
61 sort(a+1, a+n+1, accomp);
62 for (int i = 1; i <= n; i++) {
63 sum[i] = count(a[i].rank-1);
64 add(a[i].rank, a[i].val);
65 }
66 memset(c, 0, sizeof(c));
67 LL cnt = 0;
68 for (int i = n; i >= 1; i--) {
69 LL tmp = count(a[i].rank);
70 tmp = (cnt+MOD-tmp)%MOD;
71 ans = (ans+sum[i]*tmp%MOD*a[i].val)%MOD;
72 add(a[i].rank, a[i].val);
73 cnt = (cnt+a[i].val)%MOD;
74 }
75 printf("%lld\n", ans);
76 }
77 int main() {
78 work();
79 return 0;
80 }