Solution (source code)

= Solution

The <binomial series> begins
$$
(1+ax)^n=1+nax+\frac{n(n-1)}2a^2x^2+O(x^3).
$$
Equating coefficients gives $na=-2$ and $n(n-1)a^2/2=3$. Since $na\ne0$, substitute $a=-2/n$ into the second equation to obtain $2(n-1)/n=3$, so $n=-2$ and $a=1$. Therefore
$$
\boxed{a=1,\qquad n=-2.}
$$
Indeed $(1+x)^{-2}=1-2x+3x^2-\cdots$ for $|x|<1$.