51 Nod 1556计算(默慈金数的应用)
#include<bits/stdc++.h>
#define mod 1000000007
using namespace std;
typedef long long ll;
ll moni[1000005];
ll exgcd(ll a, ll b, ll &x, ll &y)
{
if (b == 0)
{
x = 1;
y = 0;
return a;
}
ll r = exgcd(b, a % b, x, y);
ll t = x % mod;
x = y % mod;
y = ((t - a / b * y) % mod + mod) % mod;
return r;
}
ll inv[1000005];
void init()
{
inv[1] = 1;
for(int i = 2; i < 1000002; i ++)
inv[i] = (mod - mod / i) * 1ll * inv[mod % i] % mod;
moni[1]=1;
moni[2]=2;
for(int i=3;i<=1000000;i++)
moni[i]=((((2*i+1)*moni[i-1]%mod+1ll*3*(i-1)*moni[i-2]%mod)%mod)*inv[i+2]%mod)%mod;
}
ll ans[1000005];
int main()
{
#ifndef ONLINE_JUDGE
//freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
init();
int n;
cin>>n;
ans[1]=1;
ans[2]=2;
for(int i=3;i<=n;i++)
ans[i]=(1ll*ans[i-1]*3%mod-moni[i-2]+mod)%mod;
cout<<ans[n]<<endl;
return 0;
}