רַבִּי יַנַּאי אוֹמֵר, אֵין בְּיָדֵינוּ לֹא מִשַּׁלְוַת הָרְשָׁעִים וְאַף לֹא מִיִּסּוּרֵי הַצַּדִּיקִים. רַבִּי מַתְיָא בֶן חָרָשׁ אוֹמֵר, הֱוֵי מַקְדִּים בִּשְׁלוֹם כָּל אָדָם. וֶהֱוֵי זָנָב לָאֲרָיוֹת, וְאַל תְּהִי רֹאשׁ לַשּׁוּעָלִים:
Rabbi Yannai said: it is not in our hands [to explain the reason] either of the security of the wicked, or even of the afflictions of the righteous. Rabbi Mathia ben Harash said: Upon meeting people, be the first to extend greetings; And be a tail unto lions, and not a head unto foxes.
אֶלָּא, הָכִי קָאָמַר לֵיהּ: צַדִּיק וְטוֹב לוֹ — צַדִּיק גָּמוּר, צַדִּיק וְרַע לוֹ — צַדִּיק שֶׁאֵינוֹ גָּמוּר, רָשָׁע וְטוֹב לוֹ — רָשָׁע שֶׁאֵינוֹ גָּמוּר, רָשָׁע וְרַע לוֹ — רָשָׁע גָּמוּר.
A righteous person is clearly not punished for the transgressions of his ancestors. Rather, it must be that God said to Moses as follows:
The righteous person who prospers is a completely righteous person whose actions are entirely good and whose reward is entirely good both in this world and in the World-to-Come.
The righteous person who suffers is one who is not a completely righteous person. Because he does have some transgressions, he is punished in this world so that he will receive a complete reward in the World-to-Come.
The wicked person who prospers is one who is not a completely wicked person. God rewards him in this world for the good deeds that he performed, so that he will receive a complete punishment in the World-to-Come.
Finally, the wicked person who suffers is a completely wicked person. Since he performed absolutely no mitzvot and deserves no reward, he receives only punishment both in this world and in the World-to-Come (Maharsha).