#include <bits/stdc++.h>
using namespace std;

const long long mo = 1e9 + 7;

int a[1000007], n, q;
long long st[4000007], st_2[4000007], st_3[4000007];
long long lz[4000007];

int nxt[1000007], f[1000007];

inline void apply(int id, int len, long long x) {
    x %= mo;
    if (x < 0) x += mo;

    long long x2 = x * x % mo;
    long long x3 = x2 * x % mo;

    st_3[id] = (
        st_3[id]
        + 3LL * st_2[id] % mo * x
        + 3LL * st[id] % mo * x2
        + 1LL * len * x3
    ) % mo;

    st_2[id] = (
        st_2[id]
        + 2LL * st[id] % mo * x
        + 1LL * len * x2
    ) % mo;

    st[id] = (st[id] + 1LL * len * x) % mo;

    lz[id] += x;
    if (lz[id] >= mo) lz[id] -= mo;
}

void build(int id, int l, int r) {
    if (l == r) {
        st[id] = a[l] % mo;
        st_2[id] = 1LL * a[l] * a[l] % mo;
        st_3[id] = st_2[id] * a[l] % mo;
        return;
    }

    int g = (l + r) >> 1;

    build(id << 1, l, g);
    build(id << 1 | 1, g + 1, r);

    st[id] = (st[id << 1] + st[id << 1 | 1]) % mo;
    st_2[id] = (st_2[id << 1] + st_2[id << 1 | 1]) % mo;
    st_3[id] = (st_3[id << 1] + st_3[id << 1 | 1]) % mo;
}

void down(int id, int l, int r) {
    if (l == r || lz[id] == 0) return;

    int g = (l + r) >> 1;

    apply(id << 1, g - l + 1, lz[id]);
    apply(id << 1 | 1, r - g, lz[id]);

    lz[id] = 0;
}

void up(int id, int l, int r, int u, int v, long long x) {
    if (l > v || r < u) return;

    if (l >= u && r <= v) {
        apply(id, r - l + 1, x);
        return;
    }

    down(id, l, r);

    int g = (l + r) >> 1;

    up(id << 1, l, g, u, v, x);
    up(id << 1 | 1, g + 1, r, u, v, x);

    st[id] = (st[id << 1] + st[id << 1 | 1]) % mo;
    st_2[id] = (st_2[id << 1] + st_2[id << 1 | 1]) % mo;
    st_3[id] = (st_3[id << 1] + st_3[id << 1 | 1]) % mo;
}

long long get(int id, int l, int r, int u, int v) {
    if (l > v || r < u) return 0;

    if (l >= u && r <= v) {
        return st[id];
    }

    down(id, l, r);

    int g = (l + r) >> 1;

    return (
        get(id << 1, l, g, u, v)
        + get(id << 1 | 1, g + 1, r, u, v)
    ) % mo;
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;

    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }

    for (int i = n; i > 0; i--) {
        nxt[i] = f[a[i]];
        f[a[i]] = i;
    }

    for (int i = 1; i <= n; i++) {
        f[a[i]] = 0;
    }

    for (int i = 1; i <= n; i++) {
        if (!f[a[i]]) {
            up(1, 1, n, i, n, 1);
        }

        f[a[i]] = 1;
    }

    long long ans = 0;

    for (int i = 1; i <= n; i++) {
        ans += st_3[1];

        ans %= mo;

        up(1, 1, n, i, n, -1);

        if (nxt[i] != 0) {
            up(1, 1, n, nxt[i], n, 1);
        }
    }

    cout << ans;

    return 0;
}