The House of Representatives voted Thursday in favor of a new trade deal for North America, delivering a hard-fought victory to President Trump and House Speaker Nancy Pelosi (D-Calif.) a day after Democrats impeached the president.

The legislation passed on an overwhelming bipartisan vote of 385 to 41, with a large majority of Democrats approving the deal. The bill’s success came despite intense partisanship on most other fronts in Washington. But these distractions appeared to create cover for negotiators to finalize a trade deal that Trump and Democrats would support.

The revised pact upended the Democratic Party’s long-standing skepticism toward massive trade bills such as the North American Free Trade Agreement, which the new deal would replace. In a further reordering of the politics of trade, the U.S.-Mexico-Canada Agreement has the endorsement of major labor unions including the AFL-CIO. The Senate is expected to act on the package next year.

The outcome is the result of months of frenzied yet secretive negotiations that took place without Pelosi or Trump ever speaking directly, though the new trade rules could be presented as top political accomplishments for both leaders. To circumvent the frayed relationship between the president and the House speaker, Democrats worked directly with U.S. Trade Representative Robert E. Lighthizer, a globalization skeptic who agreed to make numerous adjustments to gain their support.

On Thursday, when Trump was in the White House berating Democrats, Lighthizer stood in the House gallery, waving to lawmakers as the deal sailed through.

“We stuck to it. And so we’ve arrived at this ‘it can never happen’ moment," House Ways and Means Committee Chairman Richard E. Neal (D-Mass.) said in an interview. “I think it is a template for the future.”

The new U.S.-Mexico-Canada Agreement, or USMCA, was signed a year ago by Trump and the leaders of Canada and Mexico to replace the 25-year-old NAFTA, which Trump campaigned against in 2016 and lambasted as terrible for American workers.

That view was shared by many Democrats and union members who blamed NAFTA for the decline in U.S. manufacturing, making it a rare issue on which Trump found more common ground with Democrats than with Republicans.

Republicans, before Trump became president, had traditionally been much more supportive of “free trade,” or the idea that companies should be able to sell products internationally without facing import barriers. Trump has challenged that idea, saying he is in favor of “free and fair trade.” He has moved to impose tariffs on imports from numerous countries in an effort to pressure companies to relocate jobs to the United States.

“We now have a wonderful deal, and if we didn’t have a good deal, do you know what I would have done?” Trump told supporters Wednesday night at a rally in Michigan. "I would’ve have put tariffs on both countries and that would have been it, right?”

Even after being signed by the leaders of the United States, Canada and Mexico, the USMCA still required approval by the legislatures in all three countries.

That gave Pelosi the opportunity to leverage Trump’s desire for a win on trade, and, with Neal, she engaged forcefully on the issue. She appointed a nine-member group in June that spent months meeting with Lighthizer in various locations around the Capitol, exchanging proposals and working through impasse after impasse. Last month, Pelosi took over the talks and with Neal launched intense negotiations with Lighthizer and with AFL-CIO President Richard Trumka, whose support Pelosi was determined to obtain.

House Democrats, who viewed the initial deal as unacceptable, achieved substantial changes. They jettisoned pharmaceutical protections sought by drug companies and added provisions ensuring higher labor standards in Mexico, quicker dispute resolution, higher environmental standards and stronger enforcement of all elements of the deal.

Trumka’s support was not assured until the final days of talks. As the fall progressed, some moderate and freshman Democrats grew increasingly eager to pass the deal so they could show their constituents they could deliver despite the partisanship gripping Washington. They were under pressure in their districts from ads paid for by the U.S. Chamber of Commerce and visits by Vice President Pence and other administration officials. But Trumka visited the Capitol and urged Democrats to hold out for more. Finally, on the eve of the vote, Trumka released a letter to lawmakers urging them to back the new agreement, declaring that while imperfect, “it does provide a new standard from which to improve upon in future trade negotiations.”

“Rejecting it would leave the deeply flawed existing NAFTA in place for the foreseeable future,” Trumka added.

Indeed, Trump’s threats to withdraw the United States from NAFTA entirely hung over the negotiations, even though opinions differed as to whether it was a step he could legally take. And while Democrats were reluctant to credit the president, who had little if any involvement with the negotiations that took place on Capitol Hill, Republicans were quick to point out that the renegotiated deal ultimately resulted from Trump’s decision to seize on the trade issue during his run for the presidency.

“One lone presidential primary candidate in 2015 stood up and said NAFTA is no good and we need to rip it up,” said Rep. Jason T. Smith (R-Mo.).

Still, a number of Republicans were uncomfortable with the changes negotiated by Democrats, including the exclusion of a 10-year protection period for a costly class of drugs called “biologics” that had been part of the original deal. After holding out for months, the White House agreed to remove the protections in a final concession during the last weekend of talks. For Democrats, that moment vindicated their months of negotiations while also helping to lock down support from Mexican officials who had opposed the pharmaceutical protections all along and nearly bolted at the 11th hour over new labor rules also inserted at the very end.

It was a rare defeat for the powerful pharmaceutical industry, which withdrew its support for the deal after that change was made.

Senate Majority Leader Mitch McConnell (R-Ky.) told reporters last week that the deal was “not as good as I had hoped.”

Pelosi exulted as she described the deal to fellow Democrats in a closed-door meeting last week, telling attendees, “We ate their lunch,” according to a Democratic official who spoke on the condition of anonymity to describe the private remarks.

Yet the deal left some Democrats, labor unions and environmental groups wanting more. Even as the AFL-CIO, the Teamsters and the United Steelworkers threw their support behind the deal, some other unions remained opposed. The International Association of Machinists and Aerospace Workers, a major AFL-CIO affiliate, wrote to lawmakers warning that the USMCA would not stanch U.S. job losses.

The USMCA is the first multicountry trade deal to be considered by Congress since the United States negotiated a free-trade agreement with a group of Central American nations in 2005. Since then, trade has turned into a torturous issue for the Democratic Party. In the 2016 campaign, Hillary Clinton was forced to renounce her support for a major Pacific Rim trade deal that President Barack Obama had hoped to get through Congress. Obama and Pelosi also battled dramatically over trade when he was president and she was minority leader, making her alliance with Trump on a new version of NAFTA all the more striking.

Even as lawmakers of both parties praised the deal Thursday, Republicans criticized Pelosi for failing to act on it more rapidly. The administration had hoped to pass the deal months before now.

“The only reason Nancy Pelosi ever brought this up is because she impeached the president last night,” House Minority Leader Kevin McCarthy (R-Calif.) said Thursday on Fox Business Network. “She could not go home with impeachment being the case.”

Democrats insisted all along that they would not be rushed, even as Trump sought to increase pressure on Pelosi in recent weeks through angry, barbed remarks on Twitter. The tactic did not seem to work, though, and Democrats held out until most of their concerns were addressed.

Overall, the deal is meant to modernize trade rules among the three countries while also making it harder for other nations to slip products into North America tariff-free.

For example, the deal would require cars or trucks to have 75 percent of their components manufactured in Canada, Mexico or the United States to be moved between countries without tariffs. And more of the work must be done by workers making at least $16 an hour, significantly higher than many workers in Mexico currently earn. The precise impact of this change is unclear, as critics have warned it could push up the cost of motor vehicles.

The pact includes a number of other changes. It requires Canada to open up its milk market to U.S. farmers, increases intellectual property and environmental protections, and creates new safeguards against currency manipulation. It also creates new rules for e-commerce, an area of trade that has boomed since NAFTA was enacted in 1994.

The economic impact of the new trade deal is hard to measure. The White House has said it will lead to the creation of many jobs and new businesses, but other estimates have been more circumspect. For example, the International Trade Commission estimated that the changes would lead to the creation of 50,000 new manufacturing jobs, far short of the millions of factory posts that were eliminated after NAFTA was enacted in 1994.

In the end, it is a deal that Trump and congressional Democrats hope will provide campaign fodder heading into the 2020 election.

“We left a little stuff for the union because we figured to get it signed, we will give a little bit and we did it, and then we have one great deal, and now you have the Democrats trying to take credit for this deal and that is okay,” Trump said Wednesday night in Michigan. “Whatever it takes.”