Group theory exercise from "Rational Points on Elliptic Curves" 
Let $A$ be an abelian group and for every $m \geq 1$, let $A_m$ be the set of elements $P$ of $A$ such that $mP=0$. Now, suppose that $A$ has order $M^2$ and that for every integer $m$ dividing $M$, the subgroup $A_m$ has order $m^2$. Prove that $A$ is the direct product of two cyclic groups of order $M$. 

This is an exercise from the book "Rational Points on Elliptic Curves" that I could not solve. I've tried using the fundamental theorem on finitely generated abelian groups, but without much sucess. Any help? Thanks!

Consider the invariant factor decomposition of $A$ as $$(\mathbf{Z}/n_1\mathbf{Z})\oplus\cdots\oplus(\mathbf{Z}/n_k\mathbf{Z}),\qquad n_1\mid \cdots\mid n_k,\qquad n_1\cdots n_k=M^2$$
Since it is easy to check that $A$ cannot be isomorphic to $\mathbf{Z}/M^2\mathbf{Z}$, we have $k>1$.
Then $n_1^2\mid M^2$, and thus $n_1\mid M$. Since  $|A_m|=m^2$ for every $m\mid M$, we must have $k=2$, because $|A_{n_1}|$ gets a contribution of a factor of $n_1$ from each piece of the decomposition. So now we know that $A$ is isomorphic to
$$(\mathbf{Z}/n_1\mathbf{Z})\oplus(\mathbf{Z}/n_2\mathbf{Z}),\qquad n_1\mid n_2,\qquad n_1n_2=M^2$$
From this, we see that
$$n_1=p_1^{a_1}\cdots p_k^{a_k},\qquad n_2=p_1^{a_1+2t_1}\cdots p_k^{a_k+2t_k}\cdot r^2$$
for some primes $p_i$, and where $t_i\geq 0$ and $r$ is a number coprime to the $p_i$'s with $r\mid M$.
But then clearly we have $|A_r|=r$, since $\mathbf{Z}/n_1\mathbf{Z}$ has no elements that are $r$-torsion, so in order to also have $|A_r|=r^2$ we must have $r=1$.
If we had that $t_i\geq 1$, then with $m=p_1^{a_1+t_1}$ (which divides $M$) we have $|A_m|=p_1^{a_1}\cdot p_1^{a_1+t_1}$, which is not $m^2$. Therefore every $t_i=0$.
Thus, we've shown that $n_1=n_2$, so that $n_1n_2=M^2$ implies $n_1=n_2=M$.
