Forward-backward analysis of the photon-number evolution in a cavity