Solution (source code)

= Solution

Induction shows that every iterate $f^n$ is injective. Applying $f$ repeatedly gives
$$
X\supseteq f(X)\supseteq f^2(X)\supseteq\cdots.
$$
If $f^k(X)=f^{k+1}(X)$, applying $f$ inductively gives equality with every later image. Set $A=f^k(X)$. Then $f(A)=A$, so the restriction $f:A\to A$ is surjective, and it remains injective; hence it is bijective.