Question on Matrix Transform Operations Given $e = Y - XB$, where
$
e = \begin{bmatrix}
e_1 \\
\vdots \\
e_n \\
\end{bmatrix}
$,
$
   Y= \begin{bmatrix}
    y_1 \\
    \vdots \\
    y_n \\
    \end{bmatrix}
$,
$
X= \begin{bmatrix}
1 & x_1 & x^2_1 \\
\vdots & \vdots & \vdots \\
1 & x_n & x^2_n \\
\end{bmatrix}
$, and
$
B = \begin{bmatrix}
a \\
b \\
c \\
\end{bmatrix}
$.
Define the scalar sum of the squares as
$$
\begin{equation}
SSQ = e^T e
\end{equation}
$$
Substitute $Y - XB$ for $e$.
$$
\begin{equation}
SSQ = (Y-XB)^T (Y-XB) \tag{2}
\end{equation}
$$
I have seen the expansion of $(2)$ written as
$$
\begin{equation}
SSQ = Y^TY - 2B^TX^TY + X^TB^TXB \tag{3}
\end{equation}
$$
I don't completely understand the expansion. If the expansion follows the FOIL method, I understand $Y^TY$ and $X^TB^TXB$.
I don't understand how the middle term is created.
I would have expected $-X^TB^TY-Y^TXB$.
I guess I don't understand why one can substitute $Y^T$ for $Y$, $X^T$ for $X$, and $B^T$ for $B$, which would give $(3)$.
Thanks for helping this novice amateur mathematician.

The transpose is contravariant, meaning $(AB)^T=B^TA^T$ is to be used. So, we have
$$Y^TY - B^TX^TY - Y^TXB +B^TX^TXB\,.$$
Now, $Y^TXB$ is a $1\times 1$ matrix, nonetheless, is the scalar product of vectors $Y$ and $XB$, but then it is symmetric as matrix, so we have
$$Y^TXB=(Y^TXB)^T=B^TX^TY\,.$$
