File size: 1,056 Bytes
c4b0eef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
****************************************************************
****************************************************************
-> Coded by Stavros Chryselis				   
-> Visit my github for more solved problems over multiple sites 
-> https://github.com/StavrosChryselis			  
-> Feel free to email me at [email protected]	
****************************************************************
****************************************************************
*/

#include <iostream>
#include <cstring>

#define ll long long
#define MAXN 10000007
using namespace std;

const ll MOD = 1000000007;
ll memo[MAXN];

ll derangement(ll num)
{
	if (num <= 1)
		return 0;

	if (num == 2)
		return 1LL;

	if (memo[num])
		return memo[num];

	ll temp = derangement(num - 1) + derangement(num - 2);

	temp %= MOD;

	memo[num] = (num - 1) * temp;
	memo[num] %= MOD;

	return memo[num];
}

int main()
{
//	freopen("input.txt", "r", stdin);
	ios::sync_with_stdio(0);

	int T, N;

	cin >> T;

	while (T--)
	{
		cin >> N;
		cout << derangement(N) << "\n";
	}

	return 0;
}