In TWRP, select Wipe and perform a factory reset. For a "clean flash," go to Advanced Wipe and select System, Data, Cache, and Dalvik. Flash the Firmware: Tap Install and select the Android 7.1.2 ROM zip file. Swipe to confirm the installation.
At least 60% charge to prevent the device from shutting down during the flash.
A full system backup is critical as this process wipes all data.
You will need the LineageOS 14.1 ROM zip and a compatible GApps (Google Apps) zip for Android 7.1. Step-by-Step Installation Guide
You must have TWRP (Team Win Recovery Project) or CWM installed to flash the ROM files.