The Review Plan II
Michael is very grateful for your last help of his review plan. Now he finds that it's interesting to do the review in a different way than ordinary students, he wants you to help him again.
The whole book he needs to review has N chapters, he wants to arrange exactly Ndays to take his review, and one chapter by each day.
But he does not want to read the ith(1 ≤ i ≤ N-1) chapter in the ith day or the(i+1)th day. And read the Nth chapter in the Nth day or in the first day is not acceptable too. Can you tell him how many different appropriate plans he could make ?
There are multiple test cases. For each test case there is a single line contains one integer N(1 ≤ N ≤ 100000), N is the number of the days and also the number of the chapters in the book.
Process to the end of input.
One line for each case. The number of the different appropriate plans module 1000000007.
2 4
0 2
For case 1, there is no such plan. For case 2, you can arrange the plan as (2, 3, 4, 1) or (3, 4, 1, 2).
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <stdio.h>
#include <queue>
#include <map>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <set>
#include <cmath>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 10;
ll f[MAXN];
ll mod_pow(ll a, ll n)
{
ll ans = 1;
while (n)
{
if (n & 1) ans = ans*a%MOD;
a = a*a%MOD;
n >>= 1;
}
return ans;
}
void init()
{
f[0] = 1;
for (int i = 1; i<MAXN; i++)
f[i] = (i*f[i - 1]) % MOD;
}
int main()
{
//freopen("in","r",stdin);
init();
int n;
while (cin >> n)
{
ll ans = f[n];
for (int i = 1; i <= n; i++)
{
ll v = f[2 * n - i - 1] * mod_pow(f[i - 1], MOD - 2) % MOD;
v = v*mod_pow(f[2 * n - 2 * i], MOD - 2) % MOD;
v = v * 2 * n%MOD;
v = v*mod_pow(i, MOD - 2) % MOD;
v = v*f[n - i] % MOD;
if (i % 2 == 0) ans = (ans + v) % MOD;
else ans = (ans - v + MOD) % MOD;
}
if (n == 1) ans = 0;
cout << ans << endl;
}
return 0;
}