Background information
Let $\beta$ be the inverse temperature 1/T, and $H$ be the Hamiltonian.
$H = H_0 + H_I$, where $H_0$ is the free Hamiltonian.
Also $S(\beta) = e^{\beta H_0}e^{-\beta H}$
Let $\phi_H(\tau)$ be a field in Heisenberg picture, and $\phi$ in Schrodinger picture and $\phi_I(\tau)$ in interaction picture.
$$\phi_H(\tau) = e^{\tau H}\phi e^{-\tau H} $$
Then, $\phi_H(\tau) = e^{\tau H}e^{-\tau H_0}\phi_I e^{\tau H_0} e^{-\tau H} = S^{-1}(\tau)\phi_I(\tau)S(\tau)$
Now, let me come to the actual Question:
In the book "Finite Temperature Field theory" by Ashok Das (University of Rochester), the author comes up with the following derivation for the Green's function. Here, Tr is the trace, and Pr is the Time ordering operator for $0 \le \tau \le \beta$
$$G_{\beta}(\tau, \tau') = \frac{Tr e^{-\beta H} Pr \left [ S^{-1}(\tau)\phi_I(\tau) S(\tau)S^{-1}(\tau')\phi_I(\tau')^{\dagger}S(\tau') \right ]}{Tr e^{-\beta H}}\tag{1}$$
$$= \frac{Tr e^{-\beta H_0}S(\beta) Pr \left [ S^{-1}(\tau)\phi_I(\tau) S(\tau)S^{-1}(\tau')\phi_I(\tau')^{\dagger}S(\tau')\right ] }{Tr e^{-\beta H}}\tag{2}$$
$$= \frac{Tr e^{-\beta H_0}Pr\left [ S(\beta) S^{-1}(\tau)\phi_I(\tau) S(\tau)S^{-1}(\tau')\phi_I(\tau')^{\dagger}S(\tau')\right ]}{Tr e^{-\beta H}}\tag{3}$$
$$= \frac{Tr e^{-\beta H_0}Pr \left [ \phi_I(\tau) \phi_I(\tau')S(\beta)\right ] }{Tr e^{-\beta H)}}\tag{4}$$
Now how does he arrive at the last equation (4) from equation (3)? How do all those $S(\tau)$ and $S(\tau')$ terms disappear or cancel out?