A problem regarding derangement Recently I'm facing a problem on derangement.
The problem I'm facing is asked in the link: number of derangements

An answer is also given in that link.
But I can't understand the answer.

Can anyone tell me the solving way with better explanation and great details.I again asked the question in the following.

A derangement of $n$ numbers is a permutation of those numbers in which none of the numbers appears in its original place. 

For example, the numbers $\{1,2,3\}$ can be deranged into $\{2,3,1\}$ and $\{3,1,2\}$. We can modify this slightly for $n$ numbers that are not necessarily distinct by saying that no number in the derangement can be in the place that a number of the same value was in in the original ordering. So the numbers $\{1,1,2,2,3\}$ could be deranged into $\{2,2,1,3,1\}$, $\{2,2,3,1,1\}$, $\{2,3,1,1,2\}$, and $\{3,2,1,1,2\}$.

How can I find out the number of above derangement if a set like above is given?
For the number of derangements of $n$ distinct objects, the formula is:
$$D(n)=n!\left(1-\frac{1}{1!}+\frac{1}{2!}-\frac{1}{3!}+\frac{1}{4!}-\cdots+(-1)^n\frac{1}{n!}\right)$$
For the number of derangements of $n$ distinct objects, the formula is:
$$D(n)=n!\left(1-\frac{1}{1!}+\frac{1}{2!}-\frac{1}{3!}+\frac{1}{4!}-\cdots+(-1)^n\frac{1}{n!}\right)$$