Contextual Counting: A Mechanistic Study of Transformers on a Quantitative Task