President Enrique Pena Nieto at the United Nations last month. (Eduardo Munoz/Reuters)

Back in 2012, Mexico was a mess, seemingly stuck in a death-cycle of dysfunction that looked even worse than the one gripping Washington today. The country’s Congress was deadlocked, and its many problems were spinning out of control. Since 2006, the war on drugs had claimed 60,000 lives. Life expectancy was the lowest among nations in the Organization for Economic Cooperation and Development. Corruption was leaching away 10 percent of gross domestic product, and vast monopolies were smothering the economy (companies run by just one man, Carlos Slim, accounted for more than one-third of the stock exchange). Oil production, on which the government depended for a third of its budget, had dropped by a quarter in just 10 years. Things were so bad that in 2008, the Pentagon warned that Mexico risked “a rapid and sudden collapse.”

The 2012 presidential election didn’t promise much improvement. Its victor was a notorious 46-year-old philanderer named Enrique Peña Nieto, a scion of the same party that had introduced many of the problems in the first place. His nickname on the campaign trail seemed to capture the nation’s expectations — women called him “bombón,” or “sweetie” — and Ashley Madison splashed his face on its billboards. In the years since his election, his many missteps (including corruption allegations, his mishandling of several mass disappearances linked to the country’s security forces and his unfathomable decision to invite Donald Trump for a visit in August) have led many Mexicans, and Americans, to write him off.

Yet dismissing Peña Nieto’s presidency is a big mistake. The first two years of this underrated bombón’s tenure were among the most productive in Mexico’s history. In short order, he managed to win over some of his bitterest political enemies and then join forces with them to bust open Mexico’s smothering monopolies, liberalize its rusting energy sector, restructure its failing schools, modernize its banking laws and much more. To appreciate the scale of these accomplishments, try to imagine the U.S. Congress passing immigration, tax, banking and campaign finance reform — at the same time.

Peña Nieto’s greatest success was something many governments (including our own) can only dream of: shattering the political gridlock that had paralyzed his country for years. And that’s what makes his story worth studying, despite the many subsequent blunders. While it would be hard for the United States to apply his lessons directly — the two countries’ political systems are structured very differently — Mexico’s achievement remains a crucial reminder in this divisive election year: Even the most venomous political rivalries can be overcome, and the worst political stalemates can be broken. Here’s how.

Back in 2000, Mexico finally shook off 71 years of one-party rule and became a true democracy. Rather than use their new freedoms to fight for reform, however, Mexico’s three main parties — the long-dominant PRI (Institutional Revolutionary Party), the center-right PAN (National Action Party) and the left-leaning PRD (Party of the Democratic Revolution) — used them to fight one another. Congress deadlocked, and things got so bitter that between 2006 and 2012, the PRD’s leader refused to even shake the president’s hand. In the absence of government action, all the country’s big problems — drugs, poverty, crime, corruption — kept getting worse.

But the crisis had one silver lining: By 2012, frustrated voters finally decided they’d had enough. In that year’s election, they rejected the incumbent PAN in humiliating fashion, giving it just 26 percent of the vote. Although Peña Nieto’s PRI won, it just barely scraped by, with a narrow 39 percent plurality. These results forced politicians such as Jésus Zambrano, a leader of the PRD (which also did badly that year), to reluctantly accept that their constant obstructionism had “cost us a lot in the eyes of society,” as he told me when I visited in late 2014 to report on these developments for my book, “The Fix,” about how governments solve in­trac­table problems. So when, shortly after Peña Nieto’s election, Zambrano cautiously approached him to explore cooperation, the president-elect leapt at the opening.

High as the stakes were, they still weren’t enough to guarantee success; several previous attempts to collaborate had failed. Indeed, this one probably would have, too, but for the ingenious way Peña Nieto handled the ensuing talks. First, he kept the meetings small — just nine participants, three from each party — and confidential. That intimacy, plus a lot of shared meals and rounds of tequila, encouraged the rivals to see each other as actual people. Meanwhile, the secrecy (there was never a single leak over several months of talks) built trust and helped ward off pressure from special interests.

To facilitate horse-trading, the participants put their entire legislative wish lists on the table at the start. Then they determined that all decisions would be unanimous and that “nothing is agreed until all is agreed,” as another negotiator, Sen. Santiago Creel of the PAN, told me.

Another crucial emollient was the PRI’s willingness to compromise. It ultimately gave up several cherished priorities — its long-standing objection to electoral reform, for instance — thereby convincing its former rivals to do the same.

Finally, Peña Nieto was very shrewd when it came to sequencing. To build momentum, he put the issues everyone agreed on at the top of the agenda. He also consented to pass some of the other parties’ pet reforms before his own. What that did, the president told me, was “armor plate” the deal by showing all parties that they couldn’t get what they wanted unless everyone stuck to the plan.

Amazingly, it all worked. When Congress opened for business at the start of 2013, the Pact for Mexico (as it came to be known) was declared, the truce held, and Mexico’s parties stopped fighting and started legislating. Over the next 18 months, they together passed 85 major reforms, with support from lawmakers averaging 80 percent. Not only did they crack open Mexico’s monopolies, reinvigorate its oil sector, take on its powerful teachers’ unions and revamp its ineffective tax laws, they even passed a new junk-food tax to combat Mexico’s fast-growing diabetes epidemic. (What made this list still more impressive is that many of the changes had to be passed twice, first as constitutional amendments and then as implementing legislation.)

The upside of marriages of convenience is that they enable the newlyweds to do things together they’d never manage on their own. The downside, of course, is they tend not to last. The pact was no exception: In August 2014, as the parties geared up for midterm elections, Peña Nieto formally announced the deal’s conclusion.

But the legacy of Mexico’s grand bargain lives on. First, it has revolutionized the country’s politics. As Creel put it, the pact created “a new culture, in which you’re not a traitor if you sit down and talk.” Or as Aurelio Nuño, a PRI negotiator who now serves as education secretary, told me: “Before the pact, you had polarization, gridlock, something very similar to U.S. politics today. Now relations between the parties are radically different.” Meanwhile, by unshackling the Mexican economy, promoting competition, improving education and easing foreign investment, the reforms have set the stage for serious growth, especially once oil prices, so critical to Mexico’s economy, start to recover.

Unfortunately for Mexico — and for Peña Nieto’s poll numbers, which have fallen to 26 percent — that growth has yet to arrive. Ordinary Mexicans aren’t seeing much improvement, and they blame the president. What’s more, a number of states and unions are fighting to block the education reforms, which impose strict new standards on teachers. And the drop in oil prices and the general global slowdown have hurt the country badly. But none of that is really Peña Nieto’s fault (unlike the various scandals that he’s since stumbled into, for which he does bear the blame).

The fact that the pact has been slow to bear fruit should come as no surprise. The thing about structural reforms is that their benefits always take time to materialize — which is why few politicians dare attempt them. That only makes the courage shown by Peña Nieto and his partners all the more impressive. Whatever his faults, and they are many, there’s no denying that Mexico’s president is brave.

Indeed, that bravery points to the first of Mexico’s lessons for the rest of us: People matter. As do guts. The pact’s negotiators shared an unusual ability to face reality and deal with it responsibly, setting ideology and party doctrine aside at great risk to their careers.

Second, severe crises have an amazing way of concentrating leaders’ minds, while sweeping away the obstacles that ordinarily block reform, creating an enormous opportunity for leaders bold enough to seize it.

As for the final lesson, it’s the simplest of all: Never abandon hope. When I asked Juan Pardinas, the head of Mexican think tank IMCO, what he thought the story’s moral was, he told me this: “If [a few years ago] you’d asked ordinary Mexicans, or even the people who negotiated the pact, whether they thought something like this could happen here, they would have said no. We went through 15 years of frustration. But our lesson is that the impossible can happen. It happened. Sometimes you really can find water in the middle of the desert.”

That’s a message Americans desperately need to hear right now.

Twitter: @j_tepperman

Read more from Outlook and follow our updates on Facebook and Twitter.