BTW: Practice is closed (so I don’t have access to my things) plus I’m in vacations … but found this entry level linear solver in the laptop. Obviously you can modify it in order to work with any Matrix.
I just noticed I have a typo in my M matrix.
2,2 where I have -1 instead of 2:
your matrix with the 2 results in the following:
Since the rank is 4, the matrix you have is not solvable by this method.
I think there might be something wrong with the row reduce.