  Aims. We aim to search and characterize inflows and outflows of molecular gas in four ultraluminous infrared galaxies (ULIRGs) at $z\sim0.2-0.3$ and one distant QSO at $z=6.13$.   Methods. We use Herschel PACS and ALMA Band 7 observations of the hydroxyl molecule (OH) line at rest-frame wavelength 119 $\mu$m which in absorption can provide unambiguous evidence for inflows or outflows of molecular gas in nuclear regions of galaxies. Our study contributes to double the number of OH observations of luminous systems at $z\sim0.2-0.3$, and push the search for molecular outflows based on the OH transition to $z\sim6$.   Results. We detect OH high-velocity absorption wings in three of the four ULIRGs. In two cases, IRAS F20036-1547 and IRAS F13352+6402, the blueshifted absorption profiles indicate the presence of powerful and fast molecular gas outflows. Consistent with an inside-out quenching scenario, these outflows are depleting the central reservoir of molecular gas at a similar rate than the intense star formation activity. In the case of the starburst-dominated system IRAS 10091+4704, we detect an inverted P-Cygni profile that is unique among ULIRGs and indicates the presence of a fast ($\sim400$ km s$^{-1}$) inflow of molecular gas at a rate of $\sim100~M_{\odot}~{\rm yr}^{-1}$ towards the central region. Finally, we tentatively detect ($\sim3\sigma$) the OH doublet in absorption in the $z=6.13$ QSO ULAS J131911+095051. The OH feature is blueshifted with a median velocity that suggests the presence of a molecular outflow, although characterized by a modest molecular mass loss rate of $\sim200~M_{\odot}~{\rm yr}^{-1}$. This value is comparable to the small mass outflow rates found in the stacking of the [CII] spectra of other $z\sim6$ QSOs and suggests that ejective feedback in this phase of the evolution of ULAS J131911+095051 has subsided. 
Molecular Gas Inflows and Outflows in Ultraluminous Infrared Galaxies at   $z\sim0.2$ and one QSO at $z=6.1$