"And Balaam saw that it was good in the eyes of the Lord to bless Israel, and he did not go, as time after time" (Numbers 24:1), and so forth. And why "as time" twice ["as time after time"]? Rather, that wicked one had gone on two prior occasions, for he said: Perhaps the Lord will chance upon me, that He will be pleased with my sacrifices, so that He will permit me to curse them. "And he set his face toward the wilderness" (Numbers 24:1) — he said: I will recall the deed of the calf that they made in the wilderness.
Why Balaam Set His Face Toward the Wilderness
Midrash Aggadah, Numbers 24:1
וירא בלעם כי טוב בעיני ה' לברך את ישראל ולא הלך כפעם בפעם וגו'. ולמה שתי פעמים כפעם, אלא אותו הרשע הלך בשתי פעמים שאמר אולי יקרה ה' לקראתי שירצה בקרבנותי כדי שיניחני לקללם: וישת אל המדבר פניו. אמר אזכיר מעשה העגל שעשו במדבר: