Hadith 4:549

Narrated Abdullah:

Allah's Apostle, the true and truly inspired said, "(as regards your
creation), every one of you is collected in the womb of his mother for
the first forty days, and then he becomes a clot for an other forty
days, and then a piece of flesh for an other forty days. Then Allah
sends an angel to write four words: He writes his deeds, time of his
death, means of his livelihood, and whether he will be wretched or
blessed (in religion). Then the soul is breathed into his body. So a
man may do deeds characteristic of the people of the (Hell) Fire, so
much so that there is only the distance of a cubit between him and it,
and then what has been written (by the angel) surpasses, and so he
starts doing deeds characteristic of the people of Paradise and enters
Paradise. Similarly, a person may do deeds characteristic of the
people of Paradise, so much so that there is only the distance of a
cubit between him and it, and then what has been written (by the
angel) surpasses, and he starts doing deeds of the people of the
(Hell) Fire and enters the (Hell) Fire."