Problem:
Score : 1000 points

Problem Statement

How many infinite sequences a_1, a_2, ... consisting of {{1, ... ,n}} satisfy the following conditions?

The n-th and subsequent elements are all equal. That is, if n \leq i,j, a_i = a_j.

For every integer i, the a_i elements immediately following the i-th element are all equal. That is, if i < j < k\leq i+a_i, a_j = a_k.

Find the count modulo 10^9+7.

Constraints

1 \leq n \leq 10^6

Input

Input is given from Standard Input in the following format:

n

Output

Print how many sequences satisfy the conditions, modulo 10^9+7.

Sample Input 1

2

Sample Output 1

4

The four sequences that satisfy the conditions are:

1, 1, 1, ...

1, 2, 2, ...

2, 1, 1, ...

2, 2, 2, ...

Sample Input 2

654321

Sample Output 2

968545283

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		new Main().run();
	}

	long MODULO = 1_000_000_000 + 7;

	void run() {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long[] f = new long[N + 1];
		long[] cum = new long[N + 1];
		Arrays.fill(f, -Integer.MAX_VALUE / 16);
		Arrays.fill(cum, -Integer.MAX_VALUE / 16);
		f[1] = N;
		cum[1] = N;
		for (int i = 2; i <= N; ++i) {
			f[i] = f[i - 1];
			f[i] = (f[i] + 1L * (N - 1) * (N - 1) % MODULO) % MODULO;
			if (i > 3) {
				f[i] = (f[i] + cum[i - 3]) % MODULO;
			}
			f[i] = (f[i] + Math.min(0, i - 3) - (i - N - 1) + 1) % MODULO;
			cum[i] = (cum[i - 1] + f[i]) % MODULO;
		}
		System.out.println(f[N]);
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}