“I have seen everything in the days of my vanity; there is a righteous person who perishes in his righteousness, and there is a wicked person who endures in his wickedness” (Ecclesiastes 7:15). “I have seen everything in the days of my vanity.” They asked Shmuel HaKatan, what is the meaning of what is written: “There is a righteous person who perishes in his righteousness”? He said to them: ‘It is revealed and known before the One Who spoke and the world came into being that this righteous person is destined to be corrupted.
The Holy One blessed be He says: I will remove him [from the world] while he is still in his state of righteousness, as it is stated: “There is a righteous person who perishes in his righteousness.”’ “And there is a wicked person who endures in his wickedness.” As long as a person is alive, the Holy One blessed be He anticipates his repentance; once he dies the hope for his [repentance] is lost, as it is stated: “When a wicked person dies, hope is lost” (Proverbs 11:7). [This is analogous] to a group of robbers who were incarcerated in prison.
One of them dug a tunnel and they all fled. One of them remained there and did not flee. When the jailer came, he began striking him with a stick. He said to him: ‘Luckless and unfortunate one, you had a tunnel before you and you did not flee?’
So too, in the future, the Holy One blessed be He will say to the wicked: ‘You had [the opportunity for] repentance before you, and you did not repent?’ Consequently, “the eyes of the wicked will fail” (Job 11:20). Due to three matters, Rabbi Yoshiya said that the Holy One blessed be He is patient with the wicked in this world: Perhaps they will repent, or perform mitzvot for which the Holy One blessed be He will reward them in this world, or perhaps righteous children will emerge from them.
We see that He was patient with Aḥaz, and Hezekiah emerged from him; with Amon, [and] Josiah emerged from him; [with] Shimi, [and] Mordekhai emerged from him.