THE outcome of Peru’s presidential election run-off on June 5th was supposed to be a foregone conclusion. Keiko Fujimori, the daughter of the former president, Alberto Fujimori, and the runner-up in the 2011 presidential campaign, won 40% of the first-round vote in April. Her closest rival, Pedro Pablo Kuczynski, trailed far behind with 21%. Polls leading up to the vote consistently gave Ms Fujimori a lead in the mid-single digits. But it appears that Mr Kuczynski, a 77-year old economist universally known by his initials, PPK, has eked out a remarkable come-from-behind victory. With 92.6% of the vote counted, the national election agency reported he had received 50.3%, edging out Ms Fujimori’s 49.7%.

Mr Kuczynski owes his probable triumph to three factors that broke in his favour. Most important was the grudging endorsements he received from left-wing leaders—above all that of Verónika Mendoza, the candidate of the left-wing Broad Front party, who finished third in the first round with 19% of the vote. Although Ms Mendoza is no fan of Mr Kuczynski’s liberal, centre-right economic agenda or ties to Wall Street, she was persuaded by his pledges to fortify the country’s democratic institutions, which Ms Fujimori’s father—who is now imprisoned for human-rights-abuses—routinely undermined as president in 1990-2000. For Ms Mendoza the spectre of allowing the Fujimori family to return to power made Mr Kuczynski the lesser evil. “To stop the advance of fujimorismo,” she said in a video message to her followers on May 30th, “the only option is to vote for PPK.” They seem to have taken her counsel to heart: Mr Kuczynski won seven regions in southern Peru that Ms Mendoza had carried in April. And the sum of Mr Kuczynski’s and Ms Mendoza’s support in the first round roughly equalled that of Ms Fujimori, closely matching the razor-thin run-off margin.

Mr Kuczynski also benefited from a late-breaking scandal involving one of Ms Fujimori’s closest political allies. Ina Peruvian television report in May, an official of America’s Drug Enforcement Administration confirmed that his agency was investigating Joaquín Ramírez, the general secretary of Ms Fujimori’s Popular Force party and her biggest financial backer. It took Ms Fujimori three days after the story aired to suggest that Mr Ramírez “step aside”. Although accusations against Mr Ramírez, who denies all wrongdoing, are nothing new—Peruvian prosecutors have been investigating him for two years—the memory of corruption during Alberto Fujimori’s government made the claims particularly damaging to his daughter.

Lastly, timing helped Mr Kuczynski. In the campaign ahead of the first-round vote, sentiment against the Fujimoris crested shortly before the election and began to recede before the polls opened. This time, anti-fujimorismo grew unabated until the very end. Had the campaign carried on a week longer, Mr Kuczynski’s momentum might have petered out.

The result is not yet official, and a final tally may take time to compile. But in a reflection of how far Peru’s democracy has come since the years when Alberto Fujimori sent troops to shut down Congress, Ms Fujimori did not claim she had won, nor did she question the accuracy of the count. She seemed to be preparing her supporters for a concession. “We’re proud to know that we came out with the backing of 50% of the population,” she said.

Nonetheless, securing Ms Fujimori’s acceptance of his legitimacy as president will be the least of Mr Kuczynski’s problems. Although he will enjoy a fairly robust economy—GDP grew by 4.4% during the year to March—he can expect a rough ride in Congress. At the same time that voters gave Ms Fujimori 40% of the first-round vote, they elected 73 members of her party, Popular Force, to the 130-seat legislature, a comfortable majority. Mr Kuczynski’s allies won just 18. Popular Force opposes many of his economic policies , such as cutting value-added tax and providing tax breaks to big companies. If history is any guide, Mr Kuczynski will have trouble rallying public opinion to pressure recalcitrant lawmakers to pass his agenda: every Peruvian president in recent memory has endured dismal approval ratings for most of their terms.

Even if Mr Kuczynski’s reform plans flounder, however, Peru looks rather good relative to its Latin American peers, many of which are mired in recession and political strife. And his defence of liberal democracy is a welcome antidote both to the sordid past of the Fujimori family and to the worrying autocratic misrule on display in nearby Venezuela.