  We prove the linearity and injectivity of two maps $\phi_1$ and $\phi_2$ on
certain subsets of $M_n$ that satisfy
$\operatorname{tr}(\phi_1(A)\phi_2(B))=\operatorname{tr}(AB)$. We apply it to
characterize maps $\phi_i:\mathcal{S}\to \mathcal{S}$ ($i=1, \ldots, m$)
satisfying $$\operatorname{tr} (\phi_1(A_1)\cdots
\phi_m(A_m))=\operatorname{tr} (A_1\cdots A_m)$$ in which $\mathcal{S}$ is the
set of $n$-by-$n$ general, Hermitian, or symmetric matrices for $m\ge 3$, or
positive definite or diagonal matrices for $m\ge 2$. The real versions are also
given.
