## 634. Find the Derangement of An Array

In combinatorial mathematics, a derangement is a permutation of the elements of a set, such that no element appears in its original position.

There's originally an array consisting of `n`

integers from 1 to `n`

in ascending order, you need to find the number of derangement it can generate.

Also, since the answer may be very large, you should return the output mod 10^{9} + 7.

**Example 1:**

Input:3Output:2Explanation:The original array is [1,2,3]. The two derangements are [2,3,1] and [3,1,2].

**Note:**

`n`

is in the range of [1, 10^{6}].

## Rust Solution

```
struct Solution;
impl Solution {
fn find_derangement(n: i32) -> i32 {
let n = n as usize;
let mut dp = vec![0; n + 1];
dp[0] = 1;
dp[1] = 0;
for i in 2..=n {
dp[i] = (i - 1) * (dp[i - 1] + dp[i - 2]) % 1_000_000_007;
}
dp[n] as i32
}
}
#[test]
fn test() {
let n = 3;
let res = 2;
assert_eq!(Solution::find_derangement(n), res);
}
```

Having problems with this solution? Click here to submit an issue on github.