It could have been far worse for Trudeau | Sebastien St-Jean/AFP via Getty Images How Justin Trudeau survived He may be diminished, but he will remain Canada’s prime minister.

MONTREAL — Justin Trudeau chose his enemies well — and secured a little help from a friend named Barack Obama. He was contrite following a racism scandal, waited out a corruption probe and banked on the appeal of his liberal policies and the positive press from his trade tangling with Donald Trump.

The result: Trudeau may be diminished, personally and politically — but he’s still prime minister of Canada.

Monday’s elections saw the once-ascendant heir to Canadian political royalty taken down a peg, with his Liberals losing their majority.

He’ll now lead a minority government that will have to cut deals to advance any of its priorities — a fate his late father, Pierre Trudeau, also endured in the 1970s. Such governments normally last about two years. Some deals — like the new U.S.-Mexico-Canada Agreement — will be winnable. Some dynamics — like dealing with the conservative Western reaches of Canada — will be difficult.

Trudeau became the first Canadian leader in decades to lose the popular vote and win the election. He did it with a far more efficient vote distribution that pulled in scores more seats in Ontario, while Conservatives ran up the score in the less-populous west.

It could have been far worse for Trudeau.

After winning election on a wave of millennial support in 2015, Trudeau sailed through the first part of his term. He legalized marijuana and euthanasia, introduced a carbon tax and cut middle-class taxes while employment soared to historic highs. His status as a global progressive rock star scared off his domestic rivals, who struggled to find opponents who might unseat him: Several prominent Conservatives and New Democrats either retired from politics or passed on leadership campaigns, as the common wisdom in Canada had Trudeau as a slam-dunk for a second term.

But two events sent his numbers sinking.

First was a trip to India marred by an embarrassing security gaffe and a passion for traditional outfits that drew mockery and criticism. Second was a scandal in which he and members of his entourage repeatedly pressed the country’s attorney general to drop corruption charges on a Montreal engineering company.

Trudeau’s poll numbers never fully recovered from that scandal early this year. Then came September — and a Time Magazine bombshell showing him in brownface two decades ago, followed by more photos and videos of Trudeau in racist makeup.

'This is unbelievable'

Trudeau wasn’t blindsided; in fact, Liberals knew the story was coming two weeks in advance. They had been hearing from contacts in Vancouver that TIME Magazine was asking around about old photographs of Trudeau.

One party official said they knew that when it landed it would break any campaign momentum and smother Trudeau in scandal for several days — and perhaps even fatally damage his campaign.

In the frenzy of staff calls leading up to the bomb drop, Trudeau made clear he would apologize profusely, and repeatedly.

Still, allies were deeply disappointed.

Green Party Leader Elizabeth May was so taken aback by the Trudeau photos that she spent several minutes of an interview with POLITICO that night with her laptop on, waiting for the prime minister to deliver his initial apology on board his campaign plane.

“I actually am fond of him, but this is unbelievable,” she said in the living room of the Vancouver home of her husband, Green Party MP candidate John Kidder.

But the controversy dissipated almost as quickly as it had appeared — particularly in electorally influential Quebec — seemingly doing little to dent Liberals’ standing in public opinion polls.

The party official opined that one reason the scandal showed no obvious effect on Trudeau’s poll numbers was that few Canadians believe he’s actually a racist — Trudeau’s track record includes support for drastically increased immigration levels and promoting numerous visible minorities to high-level Cabinet positions.

Still, the Liberals’ early-campaign strategy of digging up dirt on Conservatives via old social media posts “was thrown out the window,” Conservative MP Erin O’Toole said. “It was impossible for them to do that because these stories really put the Liberals on [the] defensive, and the prime minister’s sort of authenticity was being questioned."

With a major campaign tactic sidelined, O’Toole said, Conservative Leader Andrew Scheer was better able to deliver his party's’ message without having to beat back those sorts of attacks on a daily basis.

Tying Scheer to Harper

Trudeau’s team had decided early on, however, not to focus on Scheer, but on more divisive conservative figures, like former Conservative Prime Minister Stephen Harper. The Liberal team noted that Scheer stirred no passion among the voters critical to their reelection. In particular, progressives and suburban swing voters, all vital to the Liberal coalition in 2015, lacked strong impressions of Scheer, a barely known, blandly inoffensive rival.

Their incessant digs at Harper, their slogan, “ Choose Forward” and Trudeau’s constant pleas for voters to look ahead, not backward, were based on a simple realization that they needed a better foil than Scheer.

Liberals said Scheer erred in running an aggressively partisan campaign, à la Harper. “Canadians rejected [Harper] in 2015 — overwhelmingly,” said one high-level Liberal staffer.

Scheer’s platform, released late in the campaign, confirmed plans for budget cuts, allowing Liberals to accelerate their comparisons to the unpopular budget-cutting premier of Ontario, Doug Ford.

“At the beginning of the campaign [Scheer] was still relatively unknown,” Liberal strategist Amanda Alvaro notes. “There wasn’t a lot of recall. But if you used names like ‘Harper,’ and ‘Ford,’ there was an emotional reaction.”

The strategy "was a bit of going back to the last thing Canadians recalled in terms of what a Conservative government looked like. The cuts, the austerity. Then [we added] something current with Ford — how services were disappearing in Ontario," she said.

Obama helped — and so did Trump

American politicians stir passions in Canada, too — and Trudeau’s Liberals worked hard to pull U.S. politics into their campaign narrative.

When Obama tweeted his support for Trudeau on October 18 — an endorsement facilitated by former White House strategist David Axelrod — it prompted cheers in the Liberal Party war room in downtown Ottawa. Party staffers weren’t just celebrating an endorsement — they were celebrating a contrast they’d spent months trying to set up.

In the Liberal campaign script, they were defenders of the small-L liberal international order, heirs to the popular Democratic U.S. president. Conservative rivals, in the Trudeau narrative, were a local branch-plant in the global conservative movement increasingly typified by a nationalist U.S. president Canadians don’t like nearly as much as Obama: Donald Trump.

Senior Trudeau cabinet members subtweeted Trump. Trudeau scolded recent U.S. abortion laws during a visit by Vice President Mike Pence.

One senior Liberal said, perhaps partly in jest, that all he wanted after the Obama endorsement was for Trump to come out and endorse Scheer.

In the end, Trump said almost nothing about Canada’s election, save for answering a reporter's question about Trudeau’s blackface scandal.

(Late Monday night, Trump tweeted his congratulations to Trudeau.)

Any edge the Conservatives had may have slipped away during the final days of the campaign.

Former Tory adviser Tim Powers said the early call for Trudeau Monday night wasn't a surprise. Trudeau campaigned well during the last week on stump, he said, and the Conservatives' handling of a Globe and Mail report that their party had hired a political consulting firm to disparage the populist People's Party of Canada didn't help their cause the weekend before Canadians cast their ballots.

What comes next

In the end, Trudeau has outdone his father. His father, a legendary four-term prime minister, eked out a two-seat squeaker in his second term. The son will have a more comfortable cushion of several dozen seats.

The outcome reflects voter disenchantment with Canada's two major parties. In a rare phenomenon, the Conservatives and Liberals remained mired in the low 30s, stuck in a statistical tie through the campaign, right up to Monday’s vote. The only dramatic movement came in Quebec, where, true to form, the province’s fickle voters turned suddenly and ultimately gave the separatist Bloc Quebecois major gains in Parliament, severely denting one of the firewalls Trudeau had been counting on.

The parliamentary configuration might make it relatively easy for Trudeau to govern despite not holding a majority. That’s because his Liberals hold so many seats that they can win any vote in the House of Commons with support from just one of the three main opposition parties.

Still, Western Canada is a wasteland for the next government, with the country deeply divided. A progressive group of parties that favor carbon pricing are resolutely dominant, and the oil-producing conservative West is mostly powerless at the federal level. That split was epitomized by the defeat of a towering figure in Canadian politics, Ralph Goodale, who was first elected in 1974 and was the last Liberal standing in Saskatchewan. He lost, and the governing party has no representation left in the province.

Lauren Gardner reported from Regina, Saskatchewan.