$$
\begin{array}{l}
\text { Four, (20 points) Let } a_{1}=1, \\
a_{n+1}=2 a_{n}+n^{2}\left(1+3^{n}\right)(n=1,2, \cdots) \text {. }
\end{array}
$$

Find the general formula for the term $a_{n}$.
Consider the following "auxiliary problem":
Let $x_{1}=1, x_{n+1}=2 x_{n}+n^{2}(n=1,2, \cdots)$.
Find the general term $x_{n}$.
$$
\text { Let } \begin{aligned}
& x_{n+1}+s(n+1)^{2}+t(n+1)+r \\
= & 2 x_{n}+n^{2}+s(n+1)^{2}+t(n+1)+r \\
= & 2\left[x_{n}+\frac{s+1}{2} n^{2}+\left(s+\frac{t}{2}\right) n+\frac{s+t+r}{2}\right],
\end{aligned}
$$

where $s, t, r$ are undetermined constants.
Let $\left\{\begin{array}{l}s=\frac{s+1}{2}, \\ t=s+\frac{t}{2}, \\ r=\frac{s+t+r}{2} .\end{array}\right.$ Solving, we get $\left\{\begin{array}{l}s=1, \\ t=2, \\ r=3 .\end{array}\right.$
Thus, $x_{n+1}+(n+1)^{2}+2(n+1)+3$
$$
=2\left(x_{n}+n^{2}+2 n+3\right) \text {. }
$$

Therefore, $\left\{x_{n}+n^{2}+2 n+3\right\}_{n \geqslant 1}$ is a geometric sequence with the first term 7 and common ratio 2. Hence,
$$
\begin{array}{l}
x_{n}+n^{2}+2 n+3=2^{n-1}\left(x_{1}+1^{2}+2 \times 1+3\right) \\
=7 \times 2^{n-1} .
\end{array}
$$

Thus, $x_{n}=7 \times 2^{n-1}-n^{2}-2 n-3(n=1,2, \cdots)$.
Now, let's return to the original problem.
Let $a_{n}=x_{n}+b_{n}$.
Substituting into $a_{n+1}=2 a_{n}+n^{2}+n^{2} \times 3^{n}$, we get
$$
x_{n+1}+b_{n+1}=2 x_{n}+2 b_{n}+n^{2}+n^{2} \times 3^{n} \text {. }
$$

Thus, $b_{n+1}=2 b_{n}+n^{2} \times 3^{n}\left(b_{1}=a_{1}-x_{1}=0\right)$.
Dividing both sides by $3^{n}$, we get
$$
\begin{array}{l}
\frac{b_{n+1}}{3^{n}}=\frac{2}{3} \cdot \frac{b_{n}}{3^{n-1}}+n^{2}(n=1,2, \cdots) . \\
\text { Let } c_{n}=\frac{b_{n}}{3^{n-1}}\left(c_{1}=0\right) .
\end{array}
$$

Similarly, using the method of undetermined coefficients, we get
$$
\begin{array}{l}
c_{n+1}-3(n+1)^{2}+18(n+1)-45 \\
=\frac{2}{3}\left(c_{n}-3 n^{2}+18 n-45\right) .
\end{array}
$$

Therefore, $\left\{c_{n}-3 n^{2}+18 n-45\right\}_{n \geqslant 1}$ is a geometric sequence with the first term -30 and common ratio $\frac{2}{3}$. Hence,
$$
c_{n}=3 n^{2}-18 n+45-30\left(\frac{2}{3}\right)^{n-1} \text {. }
$$

Thus, $b_{n}=3^{n-1} c_{n}$
$$
=3^{n}\left(n^{2}-6 n+15\right)-15 \times 2^{n} \text {. }
$$

Therefore, $a_{n}=x_{n}+b_{n}$
$$
=-23 \times 2^{n-1}+3^{n}\left(n^{2}-6 n+15\right)-n^{2}-2 n-3 .
$$