If while praying, thoughts came to one’s mind and he started having an erection but when he realizes he stops thinking and the erection stops. Is the prayer still valid?

Prayers are valid but will not benefit you spiritually