There is a well known idea that when the Jews left Egypt during the Exodus, they were on the "forty-ninth level of tumah". Many explanations are given for this and it is expounded upon at length by a number of great commentators and other writers.
However, whenever I've seen this concept brought up, it is always with a vague reference or none at all. I've seen it introduced with "...ידוע הוא" or "...אמרו חכמינו ז"ל" or "...נכתב בספרים הקדושים" or the like, without any direct quote as to where this comes from. A search on Bar Ilan doesn't find anything earlier than around the 17th century that talks about this idea; certainly not any midrashic sources.
Where does this concept come from? I'm looking for its earliest source, preferably a midrashic or Talmudic one.