Solution (source code)

= Solution

Write the <matrix trace> in <Einstein summation convention>:
$$
\operatorname{tr}(AB)=\sum_{i,j}A_{ij}B_{ji}=\sum_{j,i}B_{ji}A_{ij}=\boxed{\operatorname{tr}(BA)}.
$$
The entries are <scalars>, so their multiplication commutes, and the two summed indices may be relabelled. The argument works for square <matrices> of any finite size, and also for rectangular $A,B$ of compatible opposite shapes.