Small exoplanets of nearby M dwarf stars present the possibility to find and characterize habitable worlds within the next decade. TRAPPIST-1, an ultracool M dwarf star, was recently found to have seven Earth-sized planets of predominantly rocky composition. The planets e, f, and g can have a liquid water ocean on their surface given appropriate atmospheres of N2 and CO2. Particularly, climate models have shown that the planets e and f can sustain a global liquid water ocean, for ≥0.2 bar CO2 plus 1 bar N2, or ≥2 bars CO2, respectively. These atmospheres are irradiated by ultraviolet emission from the star's moderately active chromosphere, and the consequence of this irradiation is unknown. Here we show that chemical reactions driven by the irradiation can produce and maintain more than 0.2 bar O2 and 0.05 bar CO if the CO2 is ≥0.1 bar. The abundance of O2 and CO can rise to more than 1 bar under certain boundary conditions. Because of this O2-CO runaway, habitable environments on the TRAPPIST-1 planets entail an O2-and CO-rich atmosphere with coexisting O3. The only process that would prevent the runaway is direct recombination of O2 and CO in the ocean, a reaction that is facilitated biologically. Our results indicate that O2, O3, and CO should be considered together with CO2 as the primary molecules in the search for atmospheric signatures from temperate and rocky planets of TRAPPIST-1 and other M dwarf stars.