How do I store my contacts so I can retrieve them after a factory reset?
Aren't your contacts automatically synced with your Google account? Htc one m8 usually has Andorid 4.4 so you should check settings>accounts and check if your google account syncs your contact info automatically. You should double check it by logging to your google account on a computer to see if it's working.

Link to contacts: www.google.com/contacts