I'm working through a book (Fourier Series) by Georgi P. Tolstov and I cannot figure out what method the author uses to solve the following problem. I solved it using integration by parts, but the specific form of the author's answer is useful (I got $-4n$, which I confirmed using Wolfram Alpha). But the author gives the solution as $(-1)^n \frac{4}{n^2}$, which is a form more useful for Fourier analysis. But I cannot follow the steps involved:
$a_n = \frac{2}{\pi} \int_0^{\pi} x^2 \cos nx \ dx$
= $- \frac{4}{\pi n} \int_0^{\pi} x \sin nx \ dx$
= $\frac{4}{\pi n^2} \lbrack x \cos nx \rbrack_{x=0}^{x=\pi} - \frac{4}{\pi n^2} \int_0^{\pi} \cos nx \ dx$
=$\frac{4}{n^2} \cos n\pi$
= $(-1)^n \frac{4}{n^2}$
Any direction (especially step by step) would be greatly appreciated.