func_code_string: #include <bits/stdc++.h>   using namespace std;   const int Mod = 1e9 + 7;   int add(int a, int b) { return a + b >= Mod ? a + b - Mod : a + b; }   int dec(int a, int b) { return a - b < 0 ? a - b + Mod : a - b; }   int mul(int a, int b) { return 1ll * a * b % Mod; }   void Add(int &a, int b) { a = add(a, b); }   void Dec(int &a, int b) { a = dec(a, b); }   void Mul(int &a, int b) { a = mul(a, b); }   const int N = 1e5 + 50;   int n, a[N], pr[N], sf[N];   map<int, int> bin;   struct lll {     int a, b, c, bc, ac;   };   lll operator+(const lll &x, const lll &y) {     return (lll){add(x.a, y.a), add(x.b, y.b), add(x.c, y.c), add(x.bc, y.bc),                  add(x.ac, y.ac)};   }   namespace SGT {   const int N = ::N << 2;   lll t[N];   int tb[N];   int tc[N];   int tbc[N];   int len[N];   void up(int x) { t[x] = t[x << 1] + t[x << 1 | 1]; }   void build(int x, int l, int r) {     len[x] = r - l + 1;     if (l == r) return t[x].a = l - 1, void();     build(x << 1, l, ((l + r) >> 1));     build(x << 1 | 1, ((l + r) >> 1) + 1, r);     up(x);   }   void B(int x, int v) {     Add(tb[x], v);     Add(t[x].b, mul(len[x], v));     Add(t[x].bc, mul(v, t[x].c));     Add(tbc[x], mul(v, tc[x]));   }   void C(int x, int v) {     Add(tc[x], v);     Add(t[x].c, mul(len[x], v));     Add(t[x].bc, mul(v, t[x].b));     Add(tbc[x], mul(v, tb[x]));     Add(t[x].ac, mul(v, t[x].a));   }   void BC(int x, int v) {     Add(t[x].bc, v);     Add(tbc[x], v);   }   void down(int x) {     if (tb[x]) B(x << 1, tb[x]), B(x << 1 | 1, tb[x]), tb[x] = 0;     if (tc[x]) C(x << 1, tc[x]), C(x << 1 | 1, tc[x]), tc[x] = 0;     if (tbc[x]) BC(x << 1, tbc[x]), BC(x << 1 | 1, tbc[x]), tbc[x] = 0;   }   void mdfb(int x, int l, int r, int L, int R, int v) {     if (L <= l && r <= R) return B(x, v);     down(x);     if (L <= ((l + r) >> 1)) mdfb(x << 1, l, ((l + r) >> 1), L, R, v);     if (R > ((l + r) >> 1)) mdfb(x << 1 | 1, ((l + r) >> 1) + 1, r, L, R, v);     up(x);   }   void mdfc(int x, int l, int r, int L, int R, int v) {     if (L <= l && r <= R) return C(x, v);     down(x);     if (L <= ((l + r) >> 1)) mdfc(x << 1, l, ((l + r) >> 1), L, R, v);     if (R > ((l + r) >> 1)) mdfc(x << 1 | 1, ((l + r) >> 1) + 1, r, L, R, v);     up(x);   }   lll ask(int x, int l, int r, int L, int R) {     if (L > R) return (lll){0};     if (L <= l && r <= R) return t[x];     down(x);     if (R <= ((l + r) >> 1)) return ask(x << 1, l, ((l + r) >> 1), L, R);     if (L > ((l + r) >> 1)) return ask(x << 1 | 1, ((l + r) >> 1) + 1, r, L, R);     return ask(x << 1, l, ((l + r) >> 1), L, R) +            ask(x << 1 | 1, ((l + r) >> 1) + 1, r, L, R);   }   }  // namespace SGT   int main() {     cin >> n;     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int i = 1; i <= n; i++) {       pr[i] = bin[a[i]];       bin[a[i]] = i;     }     bin.clear();     for (int i = n; i; i--) {       sf[i] = bin[a[i]];       bin[a[i]] = i;       if (!sf[i]) sf[i] = n + 1;     }     SGT ::build(1, 1, n);     static int s1[N], s2[N];     int t1 = 0, t2 = 0, mx = 0, ans = 0;     for (int i = 1; i <= n; i++) {       mx = max(mx, pr[i]);       while (t1 && pr[s1[t1]] <= pr[i]) {         int h = dec(pr[i], pr[s1[t1]]);         SGT ::mdfb(1, 1, n, s1[t1 - 1] + 1, s1[t1], h);         --t1;       }       while (t2 && sf[s2[t2]] >= sf[i]) {         int h = dec(sf[i], sf[s2[t2]]);         SGT ::mdfc(1, 1, n, s2[t2 - 1] + 1, s2[t2], h);         --t2;       }       s1[++t1] = i, s2[++t2] = i;       SGT ::mdfb(1, 1, n, i, i, pr[i]);       SGT ::mdfc(1, 1, n, i, i, sf[i]);       lll z = SGT ::ask(1, 1, n, mx + 1, i);       Dec(ans, z.bc);       Dec(ans, mul(i + 1, z.a));       Add(ans, mul(i + 1, z.b));       Add(ans, z.ac);     }     cout << ans <<   n ;     return 0;   }   