A ballooning perturbation in a toroidally rotating tokamak is expanded by square-integrable eigenfunctions of an eigenvalue problem associated with ballooning modes in a static plasma. A special weight function is chosen such that the eigenvalue problem has only the discrete spectrum. The eigenvalues evolve in time owing to toroidal rotation shear, resulting in a countably infinite number of crossings among them. The crossings cause energy transfer from an unstable mode to the infinite number of stable modes; such a transfer works as the stabilization mechanism of the ballooning mode. A simple analytic formula is derived for estimating the toroidal rotation shear required to stabilize the ballooning mode.