Source Code
#include <bits/stdc++.h>
using namespace std;
#define oo 1000000010
#define mod 1000000007
const int N = 1000010;

int n;

int fres = 1;

int fact[N];

inline int fastpower(int num,int po){
	fres = 1;
	while(po > 0){
		if(po & 1)
			fres = (long long)fres * num % mod;
		po >>= 1;
		num = (long long)num * num % mod;
	}
	return fres;
}


inline int nck(int n,int k){
	if(k > n) return 0;
	return (long long)fact[n] * fastpower((long long)fact[k] * fact[n - k] % mod , mod - 2) % mod;
}


int main(){
	fact[0] = 1;
	for(int i = 1;i < N;i++) fact[i] = (long long)i * fact[i - 1] % mod;
	scanf("%d",&n);
	//0 -> 4
	//1 -> 2
	//2 -> 1
	int lastmx = 0;
	int mx = 2;	

	int all = 7;
	int ans = n + 2;

	int sum = 1;

	int sumOfMx = 1;


	for(int cur , i = 1;i <= n;i++){

		sum += sumOfMx;
		if(sum >= mod) sum -= mod;

		sumOfMx += mx + 1;
		if(sumOfMx >= mod) sumOfMx -= mod;


		sum += all;
		if(sum >= mod) sum -= mod;

		cur = (long long)(n - i + 1) * sum % mod;
		ans += cur;
		if(ans >= mod) ans -= mod;


		lastmx = mx;

		all += (long long)3 * (mx + 1) % mod;
		if(all >= mod) all -= mod;
		mx += 2;
		all += 3;
		if(all >= mod) all -= mod;

	} 

	cout << ans << endl;
	return 0;
}
Copy
ID Product Kilani
GNU G++17
11 ms
4.8 MB
Accepted