Example 7 (Derangement Problem) A permutation $\left\{a_{1}, a_{2}, \cdots, a_{n}\right\}$ of the set $\{1,2, \cdots, n\}$ is called a derangement if $a_{i} \neq i(i=1,2, \cdots, n)$. Find the number of derangements $D_{n}$.
Solution: Clearly, $D_{1}=0, D_{2}=1$.
For $n \geqslant 3$, let $\left\{a_{1}, a_{2}, \cdots, a_{n}\right\}$ be any derangement of $\{1,2, \cdots, n\}$. Then $a_{1}$ can take any of the other $n-1$ numbers except 1. Suppose $a_{1}=k(k \neq 1)$, then the derangements can be divided into two categories based on whether the $k$-th position of $a_{k}$ is 1:
(1) If $a_{k}=1$, the number of such derangements is the number of derangements of $n-2$ elements, so there are $D_{n-2}$;
(2) If $a_{k} \neq 1$, then this derangement is a permutation of the elements $1, 2, \cdots, k-1, k+1, \cdots, n$ in the second to the $n$-th positions, where 1 is not in the $k$-th position, and the other elements are not in their own marked positions. This permutation is equivalent to a derangement of the $n-1$ elements $2, 3, \cdots, n$, so there are $D_{n-1}$ such derangements. Therefore, when $k$ takes all values from $2, 3, \cdots, n$, we have
$$
D_{n}=(n-1)\left(D_{n-1}+D_{n-2}\right)(n \geqslant 3)
$$

Using the recursive iteration method, we can directly derive the formula for $D_{n}$, as follows:
Let $E_{n}=\frac{D_{n}}{n!}$, then (1) can be transformed into $E_{n}=\left(1-\frac{1}{n}\right) E_{n-1}+\frac{1}{n} E_{n-2}$
So $E_{n}-E_{n-1}=\left(-\frac{1}{n}\right)\left(E_{n-1}-E_{n-2}\right)$. Repeatedly using (2), and noting that $E_{1}=0, E_{2}=$ $\frac{1}{2}$, for $n \geqslant 2$, we have $E_{n}-E_{n-1}=\left(-\frac{1}{n}\right)\left(-\frac{1}{n-1}\right)\left(E_{n-2}-E_{n-1}\right)=\cdots=$ $\left(-\frac{1}{n}\right)\left(-\frac{1}{n-1}\right) \cdots\left(-\frac{1}{3}\right)\left(E_{2}-E_{1}\right)=\frac{(-1)^{n}}{n!}$, from which we get $E_{n}=\frac{(-1)^{n}}{n!}+$ $\frac{(-1)^{n-1}}{(n-1)!}+\cdots+\frac{(-1)^{2}}{2!}$. Therefore, $D_{n}=n!\left(1-\frac{1}{1!}+\frac{1}{2!}-\cdots+(-1)^{n} \cdot \frac{1}{n}\right)$.