The story proceeds from that of the prophet Balaam, in which he ascends the mountain of Pe'or, and makes sacrifices to God from atop it.
Yahweh orders Moses to gather the chiefs of the people and hang up the idolaters before Yahweh to turn away Yahweh's anger.