31. Let $Z=\{1,2, \cdots, n\}, k$ be a positive integer, $\frac{n}{2} \leqslant k \leqslant n$, find the number of mappings $f: Z \rightarrow Z$ that satisfy the following conditions:
(i) $f^{2}=f$;
(ii) The image of $f(x)$, $f(Z)$, contains exactly $k$ distinct elements;
(iii) For each $y \in f(Z)$, there are at most two $x \in Z$ such that $f(x)=y$.
31. Take $k$ numbers from $Z$ to form the range $A=f(Z)$. Thus, for any $y \in A$, there exists $x \in Z$ such that $f(x)=y \in A$, hence $f(y)=f(f(x))=f(x)=y$. Therefore, for any $x_{1}, x_{2} \in A, x_{1} \neq x_{2}, f\left(x_{1}\right)=x_{1} \neq x_{2}=f\left(x_{2}\right)$. By the given condition (iii), the $n-k$ elements in $Z-A$ each correspond to one image in $A$ (different elements correspond to different images), so the number of such mappings is $C_{n}^{k} \cdot A_{k}^{n-k}=\frac{n!}{(n-k)!(2 k-n)!}$.