Is a monoid commutative if $(ab)^2=a^2b^2$?

Let $M=\{0,1,2\}$ and $$a*b=\begin{cases}a&b=0\\b&\text{otherwise}\end{cases} $$


Here's a less ad hoc way of getting a counterexample. Consider the free monoid $F$ on $x$ and $y$. Every element of $F$ is a word in $x$ and $y$. Let $S\subset F$ be the set of words of length greater than $2$, and let $M$ be the quotient of $F$ by the equivalence relation that identifies all elements of $S$ together (it is easy to see this is a congruence relation). Then $(ab)^2=a^2b^2$ for all $a,b\in M$, since any instance of this with $a,b\neq 1$ must have both sides be words of length greater than $2$. However, it is not commutative because $xy\neq yx$. Explicitly $M=\{1,x,y,xy,yx,\infty\}$ where any product that would give a word of length greater than $2$ is $\infty$.

The motivating idea here is that if there is any counterexample, then the universal example would be a counterexample. That is, in $F$ modulo the congruence relation that identifies $(ab)^2$ and $a^2b^2$ for all $a,b\in F$, we would need $x$ and $y$ to commute. Now, this equivalence relation is complicated and hard to think about, but we can notice that it only ever identifies words that have length at least $4$ (except in the trivial cases where $a$ or $b$ is $1$). So we see we can use a larger equivalence relation that is easily verified to be a congruence, but which still does not identify $xy$ and $yx$.