Modulo Summations

Let $a_n$ be a sequence recursively defined by:$\quad a_1=1,\quad\displaystyle a_n=\biggl(\sum_{k=1}^{n-1}k\cdot a_k\biggr)\bmod n$. So the first $10$ elements of $a_n$ are: $1,1,0,3,0,3,5,4,1,9$. Let $f(N, M)$ represent the number...