Show that : $\sum\limits_{\sigma \in S_n} (\mbox{number of fixed points of } \sigma)^2= 2 n!$

You can use Burnside's lemma. $\text{Fix}(\pi)^2$ is the number of elements fixed by $\pi$ acting on $[n]^2$, where $[n] = \{ 1, 2, ... n \}$, so Burnside's lemma tells you that

$$\frac{1}{n!} \sum_{\pi \in S_n} \text{Fix}(\pi)^2$$

is the number of orbits of $S_n$ acting on $[n]^2$. But there are, by inspection, two such orbits: the ordered pairs $(a, b)$ where $a \neq b$, and the ordered pairs $(a, a)$.

(Exercise: generalize this argument to figure out $\displaystyle \frac{1}{n!} \sum_{\pi \in S_n} \text{Fix}(\pi)^k$.)


We can use basic probability theory. Take a random permutation. Let $X_i=1$ if $i$ is a fixed point of the permutation, and let $X_i=0$ otherwise. Then the number of fixed points is $X_1+\cdots+X_n$. Square. We get $$\sum_i X_i^2+\sum_{(i,j), i\ne j} X_iX_j.$$ Now by the linearity of expectation, $$E((X_1+\cdots +X_n)^2)=\sum_iE(X_i^2)+\sum_{(i,j), i\ne j}E(X_iX_j).\tag{1}$$ We have $\Pr(Xi=1)=\frac{1}{n}$ and $\Pr(X_iX_j=1)=\frac{1}{n(n-1)}$.

It follows that $E((X_1+\cdots+X_n)^2)=2$. For our sum, multiply by the number $n!$ of permutations.

Remark: Another instance of a mean proof.


Here is an answer using Derangements. Purely combinatoric.

For any set of $k$ elements there are $\mathcal{D}(n-k)$ permutations that fix those $k$ elements and derange the others. There are $\binom{n}{k}$ ways to choose the $k$ elements. So the sum of the square of the number of fixed points is $$ \begin{align} \sum_{k=0}^nk^2\binom{n}{k}\mathcal{D}(n-k) &=\sum_{k=0}^nk(k-1)\binom{n}{k}\mathcal{D}(n-k)\\ &+\sum_{k=0}^nk\binom{n}{k}\mathcal{D}(n-k)\\ &=\sum_{k=0}^nn(n-1)\binom{n-2}{k-2}\mathcal{D}((n-2)-(k-2))\\ &+\sum_{k=0}^nn\binom{n-1}{k-1}\mathcal{D}((n-1)-(k-1))\\[6pt] &=n(n-1)(n-2)!\\[12pt] &+n(n-1)!\\[12pt] &=2n!\tag{1} \end{align} $$ for $n\ge2$. We've used formula $(2)$ from the cited answer: $$ n!=\sum_{k=0}^n\binom{n}{k}\mathcal{D}(n-k)\tag{2} $$ For $n=1$, $n(n-1)(n-2)!+n(n-1)!=1$ and for $n=0$, the sum is $0$.


Generalization

Qiaochu asked a question that I had actually thought about, but had missed a fine point: How does this generalize for summing other powers of the number of fixed points? For this, we will use Stirling Numbers of the Second Kind (whose defining equation is below in red) and equation $(2)$ above. $$ \newcommand{\stirtwo}[2]{\left\{{#1}\atop{#2}\right\}} \begin{align} \sum_{k=0}^n\color{#C00000}{k^p}\binom{n}{k}\mathcal{D}(n-k) &=\sum_{k=0}^n\color{#C00000}{\sum_{j=0}^p\stirtwo{p}{j}\binom{k}{j}j!}\binom{n}{k}\mathcal{D}(n-k)\\ &=\color{#00A000}{\sum_{k=0}^n}\sum_{j=0}^p\stirtwo{p}{j}\binom{n}{j}j!\color{#00A000}{\binom{n-j}{k-j}\mathcal{D}((n-j)-(k-j))}\\ &=\sum_{j=0}^p\stirtwo{p}{j}\binom{n}{j}j!\color{#00A000}{(n-j)!}\\ &=\sum_{j=0}^n\stirtwo{p}{j}n!\tag{3} \end{align} $$ For $n\ge p$, $(3)$ can be simplified to $$ \sum_{j=0}^n\stirtwo{p}{j}n!=\mathrm{B}_pn!\tag{4} $$ where $\mathrm{B}_p=\displaystyle\sum_{j=0}^p\stirtwo{p}{j}$ are the Bell Numbers.