"And now, behold, I go to my people" (Numbers 24:14) — that is to say: I know that my portion is with you. Whatever you will lose, I too will lose. "Come, I will advise you" (Numbers 24:14) — and what counsel did he give him? Make markets and seat harlots in them. He said: They love harlotry, and since the women will withhold themselves and not listen to them unless they worship idolatry and bow down to it, and the Holy One, blessed be He, hates idolatry and hates lewdness, He will be angry at them and will slay among them. And from where do you say that he gave this counsel to Balak? For thus Moses our teacher says in the Repetition of the Torah: "Behold, these caused the children of Israel, through the word of Balaam" (Numbers 31:16). And further he said: That of which you are afraid of them — do not fear them, for their God has already commanded and said to them, "Do not harass Moab" (Deuteronomy 2:9); but at the end of days they are destined to rule over them.
Balaam's Parting Counsel to Balak and the Snare of Peor
Midrash Aggadah, Numbers 24:14
ועתה הנני הולך לעמי. כלומר אני יודע שחלקי עמכם מה אתם תאבדו אף אני אאבד: לכה איעצך. ומה עצה נתן לו, עשה שווקים והושיב בהם זונות, אמר הם אוהבים את הזנות ומתוך שימנעו הנשים שלא ישמעו להם אם לא יעבדו ע"ז וישתחוו לה, והקב"ה שונא ע"ז ושונא זמה ויכעוס עליהם ויהרג בהם, ומנין אתה אומר שהוא נתן את העצה הזאת לבלק, שכן משה רבינו אומר במשנה תורה הן הנה היו לבני ישראל בדבר בלעם (במדבר לא טז), ועוד אמר מה שאתה מתיירא מהם אל תירא מהם, שכבר צוה אלהיהם ואמר להם אל תצר את מואב וגו' (דברים ב' ט'), אבל באחרית הימים עתידים לשלוט בהם: