\(\left(x^2+3x+1\right)^2-2\left(x^2+3x+1\right)\left(3x-1\right)+\left(3x-1\right)^2\)
\(\left(x^2+3x+1-\left(3x-1\right)\right)^2\)
\(\left(x^2+3x+1-3x+1\right)\)
\(\left(x^2+\left(3x-3x\right)+\left(1+1\right)\right)^2\)
\(\left(x^2+2\right)^2\)
\(=\left(x^2+3x+1-3x+1\right)^2=\left(x^2+2\right)^2=x^4+4x^2+4\)