Let $f(n)$ be the largest positive integer $x$ less than $10^9$ such that the last $9$ digits of $n^x$ form the number $x$ (including leading zeros), or zero if no such integer exists.
For example:
Find $\sum_{2 \le n \le 10^6}f(n)$.