Thanks for @shuhua points out some potential error, I would check it later. And he posts O(1) space version In his post @shuhua post
In Wikipedia, there is a clean example Wikipedia Link
I will shared my understanding about this example
Suppose that there are n people who are numbered 1, 2, ..., n. Let there be n hats also numbered 1, 2, ..., n. We have to find the number of ways in which no one gets the hat having same number as their number. Let us assume that the first person takes hat i. There are n − 1 ways for the first person to make such a choice. There are now two possibilities, depending on whether or not person i takes hat 1 in return:
 Person i does not take the hat 1. This case is equivalent to solving the problem with n − 1 persons and n − 1 hats: each of the remaining n − 1 people has precisely 1 forbidden choice from among the remaining n − 1 hats (i's forbidden choice is hat 1).
 Person i takes the hat 1. Now the problem reduces to n − 2 persons and n − 2 hats.
Firstly, We define dp[i] represents number of derangemen in permutation(1...n)
Secondly, Suppose We know the value about dp[1]...dp[n1]. We want to compute dp[i]
For example, At the beginning, there be n hats numbered 1..n, and n peroson numbered 1..n
Hats : 1, 2, 3, 4, 5, 6, 7, .... n
Person : 1, 2, 3, 4, 5, 6, 7, ... n
If first person take hat i (!= 1)
and now
Hats : 1, 2, 3, 4, .. i  1, i + 1, .... n
Person : 2, 3, 4, ..i  1, i, i + 1.. n
There are now two possibilities, depending on whether or not person i takes hat 1

If person i takes hat 1
Hats : 2, 3, 4, .. i 1, i + 1, ... n
Person : 2, 3, 4, .. i  1, i + 1, ...n
and above problem equivalent to the subproblem dp[i2], Because there are 2 person take hat of different index . 
If person i doesn't takes hat 1. This problem equivalent to subproblem dp[i1]. You can assume person i is person 1, he doesn't takes hat 1.
Hats : 1, 2, 3, 4, ... i  1, i + 1, ..n
Person : "i", 2, 3, 4, ... i 1, i + 1, .. n

After that, The first person can takes
i range from (2...n) = n1 choice
.
So. We get the recursion formula dp[i] = (dp[i1] + dp[i2]) * (n1)
Reference code
class Solution {
public:
int findDerangement(int n) {
if(n == 1)
return 0;
vector<unsigned long long> dp(n + 1);
dp[2] = 1;
for(int i = 3; i <= n; ++i){
dp[i] = (dp[i1] + dp[i2]) * (i  1) % mod;
}
return static_cast<int>(dp[n]);
}
private:
const int mod = pow(10, 9) + 7;
};