Erasing iPhone and backup
Yes, the contacts remain in iCloud. However, once you reset the iPhone to factory settings, the iPhone is no longer logged in to your iCloud account which means that there are no contacts appearing on the iPhone whatsoever. 

You can further log in to your iCloud account on your computer via a web browser (icloud.com) and erase the contacts from there if you wish.