No, your formula is not wrong.
You have to be aware that a typical mistake when starting with this kind of sums is to make confusion about the meaning of the index $n$.
One has to decide if $n$ is labeling an eigenstate o an eigenvalue.
Your formula $\mathcal{N}\left(\lambda\right)=\sum_{n=1}^{M}\delta\left(\lambda-\lambda_{n}\right)$ is not obviously wrong if $n$ is the index of the states (which implicitly your case, once you say that $M$ is the number of eigenstates). Actually the formula works as it is, even in the case of degeneracy, the only effect of a degeneracy (of eigenvalues) being the presence of two or more delta's with the same argument. But this is harmless and gives the required result for the sum rule.
A formula like $\mathcal{N}\left(\lambda\right)=\sum_{n=1}^{M_l}g_{n}\delta\left(\lambda-\lambda_{n}\right)$, with $g_{n}$ indicating the degeneracy of the $n-th$ eigenvalue, works as well, but notice that it is a sum over the $M_l \neq M$ eigenvalues. A more transparent way of writing this sum would be
$$
\mathcal{N}\left(\lambda\right)=\sum_{\lambda_n}g(\lambda_n)\delta\left(\lambda-\lambda_{n}\right)
$$
where the fact that one is summing over the eigenvalues is more evident.