Define $f:A \to A$ where $A$ contains $n$ distinct elements. How many functions exist such that $ \forall x \in A, f^m(x)=x$, $(m<n)$ (and $m$ is prime to avoid the mistake pointed out in the comments) where $f^m$ represents the $m^{th}$ composition of $f(x)$?
The key idea I recognized here was that there are only two ways in which this rule can be followed; assigning elements in the domain and range identically or to create "loops" of size $m$ i.e. for example, making $a_1$ map to $a_2$ which then maps to $a_3$ and so on until $a_m$ maps to $a_1$.
Now, I began to consider cases where I took $0,1,2...\lfloor \dfrac{n}{m} \rfloor$ loops while assigning the remaining elements identically;
When we assign all elements identically, only $1$ such function exists.
When one loop is formed, the number of functions becomes $\binom {n}{n-m}.(m-1)!$ (using the idea of cyclic permutations).
When two loops are formed, we must first form two groups by $\dfrac{(2m)!}{2!m!^2}$. Now the total number of functions becomes $\binom {n}{n-2m}(m-1)!^2\dfrac{(2m)!}{2!m!^2}$.
So, now we see a pattern forming here which means I can express the entire sum of all such functions as
$$S=\sum_{k=0}^{\lfloor n/m \rfloor} \dfrac{\binom{n}{n-km}(m-1)!^k(mk)!}{k! (m!)^k}$$
which simplifies to $$S=n!\sum_{k=0}^{\lfloor n/m \rfloor}\left(k!(n-mk)!m^k \right)^{-1}$$
I've never really come across the sums of reciprocal factorials (other than simple telescopic sums) so I'm not quite sure if there is a closed form for such a sum. However, if anyone has any ideas on evaluating this sum or can direct me to a related question on MSE that I missed, I would be grateful.
Furthermore, my other question is how do I extend this argument of "loops" to counting the number of functions when $m>n$?