\[ \int x^2 (1-a x)^{-1-\frac {1}{2} n (1+n)} (1+a x)^{-1-\frac {1}{2} (-1+n) n} \, dx \]
Optimal antiderivative \[ \frac {\left (a x +1\right )^{\frac {\left (1-n \right ) n}{2}} \left (-a n x +1\right ) \left (-a x +1\right )^{-\frac {n \left (1+n \right )}{2}}}{a^{3} n \left (-n^{2}+1\right )} \]
command
integrate(x^2*(-a*x+1)^(-1-1/2*n*(1+n))*(a*x+1)^(-1-1/2*(-1+n)*n),x, algorithm="giac")
Giac 1.9.0-11 via sagemath 9.6 output
\[ \text {could not integrate} \]
Giac 1.7.0 via sagemath 9.3 output
\[ -\frac {a^{3} n x^{3} e^{\left (-\frac {1}{2} \, n^{2} \log \left (a x + 1\right ) - \frac {1}{2} \, n^{2} \log \left (-a x + 1\right ) + \frac {1}{2} \, n \log \left (a x + 1\right ) - \frac {1}{2} \, n \log \left (-a x + 1\right ) - \log \left (a x + 1\right ) - \log \left (-a x + 1\right )\right )} - a^{2} x^{2} e^{\left (-\frac {1}{2} \, n^{2} \log \left (a x + 1\right ) - \frac {1}{2} \, n^{2} \log \left (-a x + 1\right ) + \frac {1}{2} \, n \log \left (a x + 1\right ) - \frac {1}{2} \, n \log \left (-a x + 1\right ) - \log \left (a x + 1\right ) - \log \left (-a x + 1\right )\right )} - a n x e^{\left (-\frac {1}{2} \, n^{2} \log \left (a x + 1\right ) - \frac {1}{2} \, n^{2} \log \left (-a x + 1\right ) + \frac {1}{2} \, n \log \left (a x + 1\right ) - \frac {1}{2} \, n \log \left (-a x + 1\right ) - \log \left (a x + 1\right ) - \log \left (-a x + 1\right )\right )} + e^{\left (-\frac {1}{2} \, n^{2} \log \left (a x + 1\right ) - \frac {1}{2} \, n^{2} \log \left (-a x + 1\right ) + \frac {1}{2} \, n \log \left (a x + 1\right ) - \frac {1}{2} \, n \log \left (-a x + 1\right ) - \log \left (a x + 1\right ) - \log \left (-a x + 1\right )\right )}}{a^{3} n^{3} - a^{3} n} \]