func_code_string: #include <bits/stdc++.h>   using namespace std;   namespace FGF {   const int N = 1e5 + 5, mo = 1e9 + 7, inv2 = (mo + 1) / 2;   int a[N], b[N], pre[N], lst[N], nxt[N], sum[N << 2][4], ta[N << 2][2], n, st[N],       tp, mi[N], mx[N], ans, cnt[N];   void pushtag(int ro, int l, int r, int x, int o) {     sum[ro][3] = 1ll * x * sum[ro][o ^ 1] % mo;     sum[ro][o] = 1ll * (r - l + 1) * x % mo;     ta[ro][o] = x;     if (!o) sum[ro][2] = 1ll * (r + l) * (r - l + 1) % mo * inv2 % mo * x % mo;   }   void pushdown(int ro, int l, int r) {     for (int i = 0; i <= 1; i++)       if (ta[ro][i])         pushtag(ro << 1, l, ((l + r) >> 1), ta[ro][i], i),             pushtag(ro << 1 | 1, ((l + r) >> 1) + 1, r, ta[ro][i], i),             ta[ro][i] = 0;   }   void pushup(int ro) {     for (int i = 0; i < 4; i++)       sum[ro][i] = (sum[ro << 1][i] + sum[ro << 1 | 1][i]) % mo;   }   void updat(int ro, int l, int r, int L, int R, int x, int o) {     if (L <= l && r <= R) return pushtag(ro, l, r, x, o);     pushdown(ro, l, r);     if (L <= ((l + r) >> 1)) updat(ro << 1, l, ((l + r) >> 1), L, R, x, o);     if (R > ((l + r) >> 1)) updat(ro << 1 | 1, ((l + r) >> 1) + 1, r, L, R, x, o);     pushup(ro);   }   int query(int ro, int l, int r, int L, int R, int o) {     if (L <= l && r <= R) return sum[ro][o];     pushdown(ro, l, r);     return ((L <= ((l + r) >> 1) ? query(ro << 1, l, ((l + r) >> 1), L, R, o)                                  : 0) +             (R > ((l + r) >> 1)                  ? query(ro << 1 | 1, ((l + r) >> 1) + 1, r, L, R, o)                  : 0)) %            mo;   }   void work() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]), b[i] = a[i];     sort(b + 1, b + n + 1);     for (int i = 1; i <= n; i++) a[i] = lower_bound(b + 1, b + n + 1, a[i]) - b;     for (int i = 1; i <= n; i++) pre[i] = lst[a[i]] + 1, lst[a[i]] = i;     for (int i = n; i; i--) lst[i] = n + 1;     for (int i = n; i; i--) nxt[i] = lst[a[i]] - 1, lst[a[i]] = i;     for (int i = n; i; i--) {       while (tp && pre[i] > pre[st[tp]]) tp--;       mx[i] = tp ? st[tp] - 1 : n, st[++tp] = i;     }     tp = 0;     for (int i = n; i; i--) {       while (tp && nxt[i] < nxt[st[tp]]) tp--;       mi[i] = tp ? st[tp] - 1 : n, st[++tp] = i;     }     for (int i = n, j = n; i; i--) {       updat(1, 1, n, i, mx[i], pre[i], 0), updat(1, 1, n, i, mi[i], nxt[i], 1);       cnt[a[i]]++;       while (cnt[a[i]] > 1) cnt[a[j]]--, j--;       ans = (ans + 1ll * i * query(1, 1, n, i, j, 1) % mo +              query(1, 1, n, i, j, 2) -              1ll * i * (i + j) % mo * (j - i + 1) % mo * inv2 % mo -              query(1, 1, n, i, j, 3)) %             mo;     }     printf( %d n , (ans + mo) % mo);   }   }  // namespace FGF   int main() {     FGF::work();     return 0;   }   