I have to calculate the invariant factor decomposition of $M$ the $\mathbb Q[x]-$module with generator $e_1$ and $e_2$ and the relation $$x^2e_1+(x+1)e_2=0$$ $$(x^3+2x+1)e_1+(x^2-1)e_2=0.$$ So, I need to calculate the Smith normal form of the matrix $$\begin{pmatrix}x^2&x^3+2x+1\\ x+1& x^2-1\end{pmatrix}.$$
But in the calculation, we have that $$\begin{pmatrix}x^2&x^3+2x+1\\ x+1& x^2-1\end{pmatrix}\underset{(*)}{\to} \begin{pmatrix}-x&3x+1\\ x+1&x^2-1\end{pmatrix} \to ...\to \begin{pmatrix}1&0\\ 0& (x+1)^3\end{pmatrix},$$ but I don't understand the step $(*)$. Why $$(x^2,x+1,x^3+2x+1)=(-x)\ \ ?.$$
Other question By the way, in my correction it's written that $M\cong \mathbb Q[x]/(x+1)^3$. Shouldn't it be $M\cong \mathbb Q[x]\oplus \mathbb Q[x]/(x+1)^3$ ?
Algorithm added

