On the fate of gas in ultraluminous infrared galaxies at low and high redshift