OTTAWA - Justin Trudeau remains prime minister but the Liberals will need the support of at least one party to maintain a minority government in a country bitterly divided from a bruising 40-day election campaign.

With results still trickling in early Tuesday, the Liberals had 157 seats - 13 short of the 170 needed for a majority in the 338-seat House of Commons.

A resurgent Bloc Quebecois scooped up 32 seats, dashing Liberal hopes of making gains in Quebec that could have ensured a second consecutive majority mandate.

Despite a strong campaign by NDP Leader Jagmeet Singh, his party was leading or elected in just 25 seats and nearly wiped out in Quebec, the province that only eight years ago delivered an orange wave that pushed the NDP into official Opposition status for the first - and so far, only- time. Singh's tally fell almost 20 seats short of the party's haul in 2015, which was deemed bad enough by rank and file New Democrats to warrant firing leader Tom Mulcair.

Nevertheless, Monday's result leaves Singh potentially in the driver's seat, with New Democrats holding the balance of power.

Trudeau, whose Liberals entered the campaign with 177 seats, will need the support of either the NDP or the Bloc to command the confidence of the House of Commons, the first test of which will come within weeks on a throne speech to open a new session of Parliament.

And that could well exacerbate divisions that deepened over the course of the campaign and were apparent in the Liberals' being shut out entirely in Alberta and Saskatchewan. The two oil-producing provinces went almost solid blue, delivering every seat to the Conservatives except for Edmonton Strathcona - an NDP seat the party hung onto.

Indeed, Andrew Scheer's Conservatives were slightly ahead in the popular vote overall. But with so much of their vote concentrated in the two western provinces, the party fell short of the Liberals' tally, taking 121 ridings.

Trudeau can likely count on both the NDP and the Bloc, as well as the three Greens elected Monday, to continue with the Liberal carbon tax, which is reviled in Alberta and Saskatchewan and which Scheer had promised to repeal. Indeed, the three progressive opposition parties will probably push Trudeau to go further, faster in the fight against climate change.

However, Singh has signalled the NDP will fight plans to expand the Trans Mountain Pipeline to carry Alberta oilsands crude to the British Columbia coast, en route to overseas markets. Trudeau's government purchased the pipeline for $4.5 billion to ensure the expansion project would proceed, a decision that cost the Liberals support among progressive voters but won him no thanks in Alberta.

The Liberals owed their lead over the Tories largely to Ontario. The country's largest province delivered 79 seats to the Liberals, compared to 36 for the Conservatives and just six for the NDP.

In Quebec, the Liberals were down five to 35 seats, just ahead of the Bloc Quebecois with 32. The Conservatives were leading in 10 and the NDP in just one.

The Liberals dominated in Atlantic Canada. Although they couldn't replicate the 2015 sweep of all 32 seats in the region, they took a respectable 26 seats, the Conservatives four, the NDP one. In something of a surprise, the Greens took one: Fredericton.

Five Liberal cabinet ministers in the Atlantic region held on: Newfoundland and Labrador's Seamus O'Regan, New Brunswick's Dominic LeBlanc and Ginette Petitpas Taylor, Nova Scotia's Bernadette Jordan and Prince Edward Island's Lawrence MacAulay.

However, Diane Lebouthillier in Quebec's Gaspesie-Les-Iles-de-la-Madeleine was in a tight race against the Bloc Quebecois's Guy Bernatchez. Veteran Liberal Ralph Goodale, a stalwart of Liberal cabinets under three different prime ministers, went down in Regina, as did Natural Resources Minister Amarjeet Sohi in Edmonton.

The Green party, which had hoped for a big breakthrough in this election, had three seats, adding only the one in Fredericton to two it already had on Vancouver Island.

Maxime Bernier, who formed the People's Party of Canada after narrowly losing the Conservative leadership to Scheer, lost his own Quebec seat in Beauce.

Neither Trudeau nor Scheer seemed able to generate much enthusiasm throughout the campaign, which frequently devolved into mudslinging and misrepresentations of one another's policies and records.

Trudeau was embarrassed during the opening week of the campaign when it was revealed that he had at least three times in the past dressed up in black- or brownface. The revelation undercut his image as a champion of diversity and inclusion. He was also plagued with unsubstantiated rumours and fake reports, spread on social media, about his conduct as a teacher at a Vancouver private school.

The lingering cloud from last winter's SNC-Lavalin affair also hung over the Liberal campaign, casting doubt on Trudeau's ethics and his credentials as a feminist. His nemesis in that affair - former attorney general Jody Wilson-Raybould who quit cabinet amid allegations Trudeau and his officials had inappropriately pressured her to stop the prosecution of the Montreal engineering giant - succeeded late Monday in her attempt to win re-election as an independent in Vancouver-Granville.

Her former colleague, Jane Philpott, who quit cabinet in solidarity with Wilson-Raybould, went down to defeat in the Ontario riding of Markham-Stouffville.

Scheer was dogged throughout the campaign by questions about his personal beliefs about abortion and same-sex marriage and repeatedly insisted that he would not reopen debate on either issue should he become prime minister. However, doubt remained whether he would allow Conservative backbenchers to initiate legislation to restrict access to abortions.

Conservative fortunes in Ontario were dragged down by the unpopularity of Progressive Conservative Premier Doug Ford, whom Trudeau linked to Scheer at every opportunity.

And Conservative hopes in Quebec took a beating after Scheer put in what was widely considered a dismal performance in the first French-language leaders' debate. Scheer was also hit in the dying days of the campaign with reports that his party had hired an outside consulting firm to conduct a “seek and destroy” campaign against Bernier.

This report by The Canadian Press was first published on Oct. 21, 2019