It’s not a gender thing. It’s a phonological thing. Words like an, am, a’ end in either n, m, or just an apostrophe depending on the next sound:
- am before bilabials (p, b, f¹, m),
- a’ before lenited mh, bh, ph, ch, gh,
- an otherwise.
Here an is not an article, but part of the preposition (ann an just means ‘in’) and it does not cause lenition, so in this meaning you will always see either an or am (before p, b, f, or m).
But the definitive article an (‘the’) and third-person-plural possessive pronoun an (‘their’) behave similarly, and the article can lenite the following sound, becoming a’.
¹ Historically in Gaelic the f sound was /ɸ/, made with both lips (like /p/ but without fully blocking the flow of air between the lips), not with upper teeth and a lower lip, like English /f/.