A person who has lived a life of goodness and integrity faces severe consequences if they suddenly abandon their moral path to pursue wickedness. The primary approach among commentators is that the resulting death is a direct punishment for the new wrongs committed [רש״י, ביאור שטיינזלץ].
However, there is a deeper layer to this judgment. The punishment is not merely a reaction to the new sins in isolation. Instead, the individual is judged on a combination of two factors: their previous righteousness and their current corruption. The sharp transition from a high moral standing to a life of sin greatly amplifies the severity of the offense. The person is held accountable for the glaring gap between their noble past and their flawed present. Interestingly, this same logic applies in reverse. When a wicked person turns their life around and repents, the very fact that they were once wicked magnifies the greatness of their newly chosen good deeds [מלבי״ם].
Looking at the punishment from another perspective reveals a surprising angle. The fact that the formerly righteous person dies while committing evil is not exclusively a harsh penalty. In a certain sense, it is actually considered a benefit. By dying at that moment, the person is stopped from continuing down a dark path and accumulating even more sins on their record [מצודת דוד].