That is why the ruling party threw all its efforts into the campaign. Publicly supported news media, which has been completely subordinated to Law and Justice, brutally attacked the opposition throughout the campaign. It especially targeted the agrarian Polish People’s Party, calling for its “elimination from the political scene.”

This attack on the Polish People’s Party proved to be a grave political error. It precludes the possibility of cooperation between the two parties — effectively depriving Mr. Kaczynski of his only potential coalition partner and uniting the opposition.

The entire ministerial cabinet was out on the campaign trail. The candidates promised new metro lines, or new residential developments, seemingly from thin air. Law and Justice also issued threats, even suggesting that Warsaw, the capital, would be cut off from subsidies from the national government if the city failed to elect the ruling party’s candidate for mayor. At the end it resorted to the strategy that had proved successful in Hungary: raising the specter of a flood of refugees who would rape, attack and plunder Poland. But this backfired as well, boosting turnout for opposition candidates.

Indeed, the opposition owes its strong showing to an exceptionally mobilized electorate. There were long lines outside of polling places for the first time since 1989. Voter turnout broke the post-1989 record, reaching 55 percent (as opposed to 47 percent four years ago).

Law and Justice committed several fundamental errors. It turns out that even populism can be overdone. The ruling party’s propaganda evolved into ostentatious lies. To add insult to injury, Zbigniew Ziobro, the minister of justice, ordered the Constitutional Tribunal to examine the constitutionality of European Union agreements, which many voters saw as a first step toward a potential “Polexit.” That might be a move from the populist playbook, but it’s a mistake: Support for European integration in Poland exceeds 80 percent, the highest in Europe.

It was also a mistake to designate Prime Minister Mateusz Morawiecki as the leader of the campaign. A former banker, he proved ineffective in connecting to voters in villages and small towns.

It is difficult to overstate the ramifications of these recent elections, and Law and Justice’s failure to win them. The opposition has had its first success in three years, demonstrating that it knows how to win, and now has wind in its sails.