“Sarah was a hundred years and twenty years and seven years, these were the years of Sarah” (Bereishis 23:1). Rashi comments that from the unusual phraseology we learn that all her years were good.
“Abraham was a hundred years and seventy years and five years” (ibid. 25:7). Rashi again explains this as meaning that all his years were good.
A serious problem with this, asks the Ramban, arises when we read the following text: “Ishmael lived a hundred years and thirty years and seven years” (25:17). Certainly we cannot say that all his years were good! And although Ishmael did teshuva (repented) this was just before he died, so how can we say that ALL his years were good?
Perhaps this can be understood by quoting the words of the Rambam (Teshuva 7:4): “A repenter should never feel that he is so far from the ways of the righteous due to the sins he has done, for this is not so. He is beloved to G-d as if he has never sinned. And more so, for he has tasted the taste of sin and overpowered it …”
What the Rambam is telling us is that the years of sin have all gone into the righteous makeup which the repenter presently holds.
The Talmud says (Yoma 86b) that when one repents, one’s sins convert to merits. In other words, one’s sins serve as the building blocks for one’s present righteous behavior. If one never repents then one dies with one’s sins, but if he does then retroactively one’s sins are counted as virtues, for they, and what it took to overcome them are all part of what that person is about.
So perhaps we can say that although Ishmael was sinful all his life, because he decided to repent towards the end, all his years became good years retroactively.
So on Ishmael too it can be said that he lived a hundred years and thirty years and seven years — all of them good years!