import java.util.Arrays;

public class Euler733 {
    static final int kSeqMod = 10000019;
    static final int kAnswerMod = 1000000007;

    static int[] generateSequence(int n) {
        int[] a = new int[n];
        long cur = 1;
        for (int i = 0; i < n; ++i) {
            cur = (cur * 153L) % kSeqMod;
            a[i] = (int) cur;
        }
        return a;
    }

    static class FenwickPair {
        int n;
        int[] cnt;
        int[] sum;

        FenwickPair(int n) {
            this.n = n;
            this.cnt = new int[n + 1];
            this.sum = new int[n + 1];
        }

        static int addMod(int a, int b) {
            int s = a + b;
            if (s >= kAnswerMod) {
                s -= kAnswerMod;
            }
            return s;
        }

        int[] query(int idx) {
            int c = 0;
            int s = 0;
            while (idx > 0) {
                c += cnt[idx];
                if (c >= kAnswerMod)
                    c -= kAnswerMod;
                s += sum[idx];
                if (s >= kAnswerMod)
                    s -= kAnswerMod;
                idx -= idx & -idx;
            }
            return new int[] { c, s };
        }

        void update(int idx, int addCnt, int addSum) {
            while (idx <= n) {
                cnt[idx] = addMod(cnt[idx], addCnt);
                sum[idx] = addMod(sum[idx], addSum);
                idx += idx & -idx;
            }
        }
    }

    public static String solve() {
        int n = 1000000;
        int[] a = generateSequence(n);

        int[] sorted = a.clone();
        Arrays.sort(sorted);

        int uniqueCount = 0;
        for (int i = 0; i < n; i++) {
            if (i == 0 || sorted[i] != sorted[i - 1]) {
                sorted[uniqueCount++] = sorted[i];
            }
        }
        int[] uniqueSorted = Arrays.copyOf(sorted, uniqueCount);

        int[] rank = new int[n];
        for (int i = 0; i < n; ++i) {
            rank[i] = Arrays.binarySearch(uniqueSorted, a[i]) + 1;
        }

        int m = uniqueSorted.length;
        FenwickPair fw0 = new FenwickPair(m);
        FenwickPair fw1 = new FenwickPair(m);
        FenwickPair fw2 = new FenwickPair(m);

        int answer = 0;

        for (int i = 0; i < n; ++i) {
            int r = rank[i];
            int x = a[i];

            int c1 = 1;
            int s1 = x;

            int[] pref1 = fw0.query(r - 1);
            int c2 = pref1[0];
            int s2 = (int) ((pref1[1] + (long) x * c2) % kAnswerMod);

            int[] pref2 = fw1.query(r - 1);
            int c3 = pref2[0];
            int s3 = (int) ((pref2[1] + (long) x * c3) % kAnswerMod);

            int[] pref3 = fw2.query(r - 1);
            int c4 = pref3[0];
            int s4 = (int) ((pref3[1] + (long) x * c4) % kAnswerMod);

            answer += s4;
            if (answer >= kAnswerMod) {
                answer -= kAnswerMod;
            }

            fw0.update(r, c1, s1);
            fw1.update(r, c2, s2);
            fw2.update(r, c3, s3);
        }

        return Integer.toString(answer);
    }

    public static void main(String[] args) {
        System.out.println(solve());
    }
}
