Near-infrared spectroscopy of M dwarfs. I. CO molecule as an abundance indicator of carbon