#include <bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long b[1000012], t, n, m, k, ccnt,
    pos = 0, sum = 0, minn2 = INT_MAX, sum2, cnt = 0, cnt1 = 0, cnt2 = 0, x, y,
    cnt3 = 0, cnt4 = 0, cnt5 = 0, cnt6 = 0, cnt7 = 0, a[1000005], a2[1000005],
    x1, y7, x2, y2, x3, y3, bbb;
long long c[1000005], z, d, l2, r2, temcnt, le[1000005], ri[1000005],
    ok[1000005], cl, cr, gan, fst, pre[1000005], mxl[1000005], mxr[1000005],
    uu[100005], vv[100005];
long long pos1 = 0, pos2 = 0, tem, tem2, l, r, ans, mod = 998244353, tw[100005],
          th[100005], ee, loc[1000005], te[100005], vis[1000005], ne[200005],
          pr[200005];
long long minn = LONG_MAX, maxx2, posb, mid;
long long c10, c11, c20, c21, dp[100005][11], predp[100005][11], ans1, ans2,
    maxx, dis[155][155], edge[155][155], matrix1[105][105], matrix2[105][105],
    matrix3[105][105], dist[155];
bool ia, ib;
string s1, s, s2, s3, t1, t2, ss[105];
char ch, ch2, cc2[1005][1005];
vector<long long> v[1000005], depth[100005];
pair<long long, long long> p[1000005];
pair<long long, long long> p1, p2;
map<long long, long long> mp;
long long fpow(long long aa, long long bb) {
  if (bb == 0) {
    return 1;
  } else if (bb < 0) {
    return 0;
  }
  long long kk1 = aa;
  long long kk2 = 1;
  while (bb > 1) {
    if (bb % 2) {
      kk2 *= kk1;
      kk2 %= mod;
    }
    kk1 *= kk1;
    kk1 %= mod;
    bb = bb / 2;
  }
  return (kk1 * kk2) % mod;
}
long long co(long long x, long long y) {
  long long ans = 1;
  long long ans2 = 1;
  if (x == y || y == 0) {
    return 1;
  } else {
    for (long long i = x - y + 1; i <= x; i++) {
      ans = ans * i;
      ans = ans % mod;
    }
    for (long long i = 1; i <= y; i++) {
      ans2 = ans2 * i;
      ans2 = ans2 % mod;
    }
    ans2 = fpow(ans2, mod - 2);
    ans2 = (ans2 * ans) % mod;
    return ans2;
  }
}
long long summ(long long x) {
  long long ans = 0, pos = 0;
  long long maxx;
  x++;
  long long xx = x;
  for (long long i = 0;; i++) {
    if (tw[i] > x) {
      maxx = i;
      break;
    }
  }
  for (long long i = maxx - 1; i >= 0; i--) {
    if (xx >= tw[i]) {
      th[i] = 1;
      xx -= tw[i];
    } else {
      th[i] = 0;
    }
  }
  for (long long i = maxx - 1; i >= 0; i--) {
    if (th[i]) {
      ans += v[i][pos];
      ans %= mod;
      pos = 2 * pos + 2;
    } else {
      pos *= 2;
    }
  }
  return ans;
}
void modify(long long x, long long m) {
  v[0][x] += m;
  v[0][x] %= mod;
  for (long long i = 1; i <= cnt2; i++) {
    v[i][x / 2] += m;
    v[i][x / 2] %= mod;
    x /= 2;
  }
}
signed main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  mod = 1000000007;
  tw[0] = 1;
  for (long long i = 1; i <= 60; i++) {
    tw[i] = tw[i - 1] * 2;
  }
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    if (!vis[a[i]]) {
      pre[i] = 1;
    } else {
      pre[i] = vis[a[i]];
    }
    vis[a[i]] = i;
    p[i - 1].first = a[i];
    p[i - 1].second = i;
  }
  sort(p, p + n);
  cnt = n + 1;
  cnt2 = 0;
  for (long long i = 0; i <= n; i++) {
    v[0].push_back(0);
  }
  while (cnt > 1) {
    cnt2++;
    for (long long i = 0; i < cnt / 2; i++) {
      v[cnt2].push_back(0);
    }
    if (cnt % 2) {
      v[cnt2].push_back(0);
    }
    cnt = (cnt + 1) / 2;
  }
  ans = 0;
  for (long long i = 0; i < n; i++) {
    x = ((summ(p[i].second) - summ(pre[p[i].second] - 1)) + mod) % mod;
    if (ok[a[p[i].second]] == 0) {
      x++;
      ok[a[p[i].second]] = 1;
    }
    x *= a[p[i].second];
    x %= mod;
    ans += x;
    ans %= mod;
    modify(p[i].second, x);
  }
  cout << ans;
}

### Prompt

Please create a solution in Cpp to the following problem:

Sereja has a sequence that consists of n positive integers, a1, a2, ..., an. 

First Sereja took a piece of squared paper and wrote all distinct non-empty non-decreasing subsequences of sequence a. Then for each sequence written on the squared paper, Sereja wrote on a piece of lines paper all sequences that do not exceed it.

A sequence of positive integers x = x1, x2, ..., xr doesn't exceed a sequence of positive integers y = y1, y2, ..., yr, if the following inequation holds: x1 ≤ y1, x2 ≤ y2, ..., xr ≤ yr.

Now Sereja wonders, how many sequences are written on the lines piece of paper. Help Sereja, find the required quantity modulo 1000000007 (109 + 7). 

Input

The first line contains integer n (1 ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106).

Output

In the single line print the answer to the problem modulo 1000000007 (109 + 7).

Examples

Input

1
42


Output

42


Input

3
1 2 2


Output

13


Input

5
1 2 3 4 5


Output

719

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long b[1000012], t, n, m, k, ccnt,
    pos = 0, sum = 0, minn2 = INT_MAX, sum2, cnt = 0, cnt1 = 0, cnt2 = 0, x, y,
    cnt3 = 0, cnt4 = 0, cnt5 = 0, cnt6 = 0, cnt7 = 0, a[1000005], a2[1000005],
    x1, y7, x2, y2, x3, y3, bbb;
long long c[1000005], z, d, l2, r2, temcnt, le[1000005], ri[1000005],
    ok[1000005], cl, cr, gan, fst, pre[1000005], mxl[1000005], mxr[1000005],
    uu[100005], vv[100005];
long long pos1 = 0, pos2 = 0, tem, tem2, l, r, ans, mod = 998244353, tw[100005],
          th[100005], ee, loc[1000005], te[100005], vis[1000005], ne[200005],
          pr[200005];
long long minn = LONG_MAX, maxx2, posb, mid;
long long c10, c11, c20, c21, dp[100005][11], predp[100005][11], ans1, ans2,
    maxx, dis[155][155], edge[155][155], matrix1[105][105], matrix2[105][105],
    matrix3[105][105], dist[155];
bool ia, ib;
string s1, s, s2, s3, t1, t2, ss[105];
char ch, ch2, cc2[1005][1005];
vector<long long> v[1000005], depth[100005];
pair<long long, long long> p[1000005];
pair<long long, long long> p1, p2;
map<long long, long long> mp;
long long fpow(long long aa, long long bb) {
  if (bb == 0) {
    return 1;
  } else if (bb < 0) {
    return 0;
  }
  long long kk1 = aa;
  long long kk2 = 1;
  while (bb > 1) {
    if (bb % 2) {
      kk2 *= kk1;
      kk2 %= mod;
    }
    kk1 *= kk1;
    kk1 %= mod;
    bb = bb / 2;
  }
  return (kk1 * kk2) % mod;
}
long long co(long long x, long long y) {
  long long ans = 1;
  long long ans2 = 1;
  if (x == y || y == 0) {
    return 1;
  } else {
    for (long long i = x - y + 1; i <= x; i++) {
      ans = ans * i;
      ans = ans % mod;
    }
    for (long long i = 1; i <= y; i++) {
      ans2 = ans2 * i;
      ans2 = ans2 % mod;
    }
    ans2 = fpow(ans2, mod - 2);
    ans2 = (ans2 * ans) % mod;
    return ans2;
  }
}
long long summ(long long x) {
  long long ans = 0, pos = 0;
  long long maxx;
  x++;
  long long xx = x;
  for (long long i = 0;; i++) {
    if (tw[i] > x) {
      maxx = i;
      break;
    }
  }
  for (long long i = maxx - 1; i >= 0; i--) {
    if (xx >= tw[i]) {
      th[i] = 1;
      xx -= tw[i];
    } else {
      th[i] = 0;
    }
  }
  for (long long i = maxx - 1; i >= 0; i--) {
    if (th[i]) {
      ans += v[i][pos];
      ans %= mod;
      pos = 2 * pos + 2;
    } else {
      pos *= 2;
    }
  }
  return ans;
}
void modify(long long x, long long m) {
  v[0][x] += m;
  v[0][x] %= mod;
  for (long long i = 1; i <= cnt2; i++) {
    v[i][x / 2] += m;
    v[i][x / 2] %= mod;
    x /= 2;
  }
}
signed main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  mod = 1000000007;
  tw[0] = 1;
  for (long long i = 1; i <= 60; i++) {
    tw[i] = tw[i - 1] * 2;
  }
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    if (!vis[a[i]]) {
      pre[i] = 1;
    } else {
      pre[i] = vis[a[i]];
    }
    vis[a[i]] = i;
    p[i - 1].first = a[i];
    p[i - 1].second = i;
  }
  sort(p, p + n);
  cnt = n + 1;
  cnt2 = 0;
  for (long long i = 0; i <= n; i++) {
    v[0].push_back(0);
  }
  while (cnt > 1) {
    cnt2++;
    for (long long i = 0; i < cnt / 2; i++) {
      v[cnt2].push_back(0);
    }
    if (cnt % 2) {
      v[cnt2].push_back(0);
    }
    cnt = (cnt + 1) / 2;
  }
  ans = 0;
  for (long long i = 0; i < n; i++) {
    x = ((summ(p[i].second) - summ(pre[p[i].second] - 1)) + mod) % mod;
    if (ok[a[p[i].second]] == 0) {
      x++;
      ok[a[p[i].second]] = 1;
    }
    x *= a[p[i].second];
    x %= mod;
    ans += x;
    ans %= mod;
    modify(p[i].second, x);
  }
  cout << ans;
}

```