Theory of two-photon spectroscopy of autoionizing states in helium and beryllium