func_code_string: #include <bits/stdc++.h>   using namespace std;   const int N = 1000005;   int n, a[N], b[N], lst[N], pre[N], suf[N], pr[N], sf[N], cn[N], sta[N], top,       ans;   long long s[N][4], tag[N][4];   inline long long S2(const int x) { return (x + 1ll) * x >> 1; }   inline long long S(const int l, const int r) { return S2(r) - S2(l - 1); }   inline void pd(const int rt, const int l, const int r, const int x,                  const int i) {     s[rt][2] = s[rt][i ^ 1] * x, s[rt][i] = (r - l + 1ll) * x, tag[rt][i] = x,     !i ? s[rt][3] = S(l, r) * x : 0;   }   inline void pd(const int rt, const int l, const int r) {     const int mid = l + r >> 1, ls = rt << 1, rs = ls | 1;     tag[rt][0] ? pd(ls, l, mid, tag[rt][0], 0), pd(rs, mid + 1, r, tag[rt][0], 0),         tag[rt][0] = 0 : 0;     tag[rt][1] ? pd(ls, l, mid, tag[rt][1], 1), pd(rs, mid + 1, r, tag[rt][1], 1),         tag[rt][1] = 0 : 0;   }   inline void upd(const int rt, const int l, const int r, const int u,                   const int v, const int x, const int i) {     if (l >= u && r <= v) return pd(rt, l, r, x, i);     const int mid = l + r >> 1, ls = rt << 1, rs = ls | 1;     pd(rt, l, r);     u <= mid ? upd(ls, l, mid, u, v, x, i), 0 : 0,         v > mid ? upd(rs, mid + 1, r, u, v, x, i), 0 : 0;     s[rt][0] = s[ls][0] + s[rs][0], s[rt][1] = s[ls][1] + s[rs][1],     s[rt][2] = s[ls][2] + s[rs][2], s[rt][3] = s[ls][3] + s[rs][3];   }   inline long long que(const int rt, const int l, const int r, const int u,                        const int v, const int i) {     if (u > v) return 0;     if (l >= u && r <= v) return s[rt][i];     const int mid = l + r >> 1, ls = rt << 1, rs = ls | 1;     pd(rt, l, r);     if (v <= mid) return que(ls, l, mid, u, v, i);     if (u > mid) return que(rs, mid + 1, r, u, v, i);     return que(ls, l, mid, u, mid, i) + que(rs, mid + 1, r, mid + 1, v, i);   }   int main() {     scanf( %d , &n);     register int i, j;     for (i = 1; i <= n; ++i) scanf( %d , &a[i]), b[i] = a[i];     std::sort(b + 1, b + 1 + n), *b = std::unique(b + 1, b + 1 + n) - b - 1;     for (i = 1; i <= n; ++i)       a[i] = std::lower_bound(b + 1, b + 1 + *b, a[i]) - b,       pre[i] = lst[a[i]] + 1, lst[a[i]] = i;     for (i = 1; i <= *b; ++i) lst[i] = n + 1;     for (i = n; i; --i) suf[i] = lst[a[i]] - 1, lst[a[i]] = i;     for (i = n, top = 0; i; --i) {       while (top && pre[i] > pre[sta[top]]) --top;       pr[i] = top ? sta[top] - 1 : n, sta[++top] = i;     }     for (i = n, top = 0; i; --i) {       while (top && suf[i] < suf[sta[top]]) --top;       sf[i] = top ? sta[top] - 1 : n, sta[++top] = i;     }     for (i = n, j = n; i; --i) {       upd(1, 1, n, i, pr[i], pre[i], 0), upd(1, 1, n, i, sf[i], suf[i], 1),           ++cn[a[i]];       while (cn[a[i]] > 1) --cn[a[j]], --j;       ans = (ans + que(1, 1, n, i, j, 3) + que(1, 1, n, i, j, 1) * i -              S(i, j) * i - que(1, 1, n, i, j, 2)) %             1000000007;     }     printf( %d , ans < 0 ? ans + 1000000007 : ans);     return 0;   }   