B RAZILIANS FACE an awful choice. One candidate in the presidential run-off on October 28th is Jair Bolsonaro, a seven-term congressman who venerates dictators and guns, goads police to kill suspected criminals, threatens to banish opponents and belittles women, blacks and gays. His rival is Fernando Haddad, the nominee of the leftist Workers’ Party (PT). Its 13 years in power, from 2003 to 2016, ended in a self-inflicted economic depression and revelations that the party encouraged bribery on an unprecedented scale, in part to prolong its hold on power.

Dilma Rousseff, a PT president, was impeached in 2016 for hiding the true size of the budget deficit. Crime continued to rise after she left office. Nearly 64,000 people were murdered in Brazil last year, a record number. Understandably, Brazilians are enraged. They now look poised to elect Mr Bolsonaro, a populist with authoritarian instincts, as their president.

That such a man will probably lead Latin America’s largest country is a tragedy. If it happens, it will be because Brazil’s political class has failed the country’s people. Some of the corruption was orchestrated by the PT, but almost all parties took part in it. Today’s crime and economic stagnation are a consequence of a dangerously indebted state that is at once too big and too feeble to provide adequate policing, education and other public services. Nearly all politicians share the blame for that.

Mr Bolsonaro’s probable election will pose a new challenge: ensuring that a president with autocratic impulses does not subvert Brazil’s democracy. It is critical that politicians of all ideologies rise to the occasion. But they cannot forget about the old problems. If Mr Bolsonaro puts forward good ideas for fixing the economy and controlling corruption, he should get help.

Brazil is a relatively young democracy; dictatorial rule ended in 1985. But it is not a weak one. Although Brazilians view congress as a corruption-ridden collection of rent-a-parties, it is not a rubber stamp. It has impeached two presidents in the democratic era and can provide a vital check on Mr Bolsonaro. The judiciary has shown its independence over the past four years through the Lava Jato (Car Wash) investigations. These have implicated scores of politicians and led to the jailing of the PT' s leader, Luiz Inácio Lula da Silva, a former president. The press is already challenging Mr Bolsonaro, which is why, like Donald Trump, he accuses it of spreading fake news.

Such institutions can thwart some of Mr Bolsonaro’s worst plans. He wants the police to have “carte blanche” to kill. But the main police forces are under the authority of the 27 states. Their governors must reject his trigger-happy philosophy. Congress can stop him from carrying out his threat to stuff the supreme court with pliant judges. Mr Bolsonaro’s proposal to withdraw from the Paris climate agreement and his eagerness to promote development in the Amazon, where rates of deforestation appear to be rising, should alarm the world. Congress and activist groups can stand in his way.

Not all Mr Bolsonaro’s ideas are bad. He has shown more interest than the PT in solving Brazil’s main economic problems (see article). If he is serious about reforming the costly pension system, which threatens Brazil’s financial stability, and eliminating useless rules, congress should co-operate. (Though that is a big “if”.)

The worst effects of a Bolsonaro presidency may be hardest to contain. Already he has damaged Brazil’s democratic culture by praising the former dictatorship, choosing as his running-mate a retired general who has justified military coups under some circumstances, and insinuating that political opponents are enemies of the state. He probably does not intend to be a dictator. But his corrosive rhetoric may make Brazilians more receptive to autocracy in the future. To confront that, Brazil needs an opposition that defends democratic norms and an army determined to remain scrupulously apolitical.

In their despair, Brazilians are about to reject a discredited party in favour of a political adventurer with repellent ideas. That is unlikely to turn out well. Lawmakers, judges, journalists and civil servants will have to work hard to limit the damage.