WASHINGTON — President Donald Trump quietly signed legislation Wednesday that imposes new limits on his ability to lift sanctions against Russia, saying parts of the measure are unconstitutional but he was signing it for the "sake of national unity."

The signing brought a protest from Russia, whose prime minister accused Trump of showing "total weakness." The new law marks a significant surrender by Trump to congressional efforts to limit his discretion in handling relations with Moscow.

Trump, who has held lavish ceremonies to herald far less consequential documents, signed this bill into law without cameras, sending out two statements later, one of which laid out the administration's legal arguments and the other expressing Trump's personal objections to the new law.

"As president, I can make far better deals with foreign countries than Congress," he wrote.

He had little choice about signing the bill. Both houses of Congress, signaling skepticism of his overtures toward Russian President Vladimir Putin, approved the measure almost unanimously. That all but guaranteed any veto would be overridden.

The law prevents American companies from investing in many energy projects that are funded by Russian government interests. It also toughens sanctions against Iran and North Korea.

And it prevents Trump from unilaterally lifting the sanctions, giving Congress an extended period of time to review any presidential action that tries to upend or significantly change existing sanctions.

Russian Prime Minister Dmitry Medvedev denounced the measure as a "full-scale trade war" and an end to "the hope that our relations with the new American administration would improve," while personally mocking Trump.

"The Trump administration has shown its total weakness by handing over executive power to Congress in the most humiliating way," Medvedev wrote on Facebook.

Sen. Charles E. Schumer of New York, the top Democrat in the Senate, took his own shot on Twitter, calling the bill "a model for the future" that shows both parties can work together "to rein in" Trump "when he veers off track."

The law marks an unusual move by Congress to tie the president's hands on foreign policy.

Trump did not want to surrender that authority, and in his legal statement accompanying the bill signing, he laid the groundwork for potentially challenging the law down the road.

Trump called some parts of the law "clearly unconstitutional," although he said he would "expect to honor" its key provisions.

"While I favor tough measures to punish and deter aggressive and destabilizing behavior by Iran, North Korea and Russia, this legislation is significantly flawed," he wrote.

Some parts of the law "displace the President's exclusive constitutional authority to recognize foreign governments" while others exceed Congress' authority by imposing time limits on the executive branch, the signing statement said.

Trump wrote he would nonetheless honor the law's requirement that he submit to a congressional review before terminating any sanctions. He pledged to enforce the law "in a manner consistent with the President's constitutional authority to conduct foreign relations."

John Bies, who served eight years in the Justice Department during the Obama administration, said the objections in Trump's statement appeared fairly standard, even if their tone was "a little blunter and more direct" than prior administrations might have used. But he did not expect it would affect how Trump enforces the new law.

Walter Dellinger, who headed the Office of Legal Counsel in the Department of Justice during the Clinton administration, agreed, calling the rhetoric "somewhat exaggerated" but standard and "unlikely to diminish the effectiveness of the law."

In a separate news release, in which Trump made the remark about national unity, he lashed out at Congress in more colloquial terms than in the formal signing statement.

He said the new law would make it harder for the U.S. to "strike good deals for the American people, and will drive China, Russia and North Korea much closer together."

He also drew a sharp distinction among the three countries sanctioned by the law.

On Russia, he said, "we hope there will be cooperation between our two countries on major global issues so that these sanctions will no longer be necessary."

By contrast, referring to Iran and North Korea, he spoke of a "clear message" that "the American people will not tolerate their dangerous and destabilizing behavior."

Illustrating the gap between the White House and congressional Republican leaders on the issue, House Speaker Paul D. Ryan, R-Wis., made no such distinction, saying the "sanctions directly target the destructive and destabilizing activities of Iran, Russia and North Korea."

The new law is "a powerful message to our adversaries that they will be held accountable for their actions," Ryan said.

Trump took a characteristic shot at Congress, noting lawmakers' failure to repeal the Affordable Care Act "after seven years of talking," and contrasting his history of having "built a truly great company worth many billions of dollars."

The votes in Congress, 98-2 in the Senate and 419-3 in the House, were strong signs lawmakers do not trust Trump to stand up to Putin, whom Trump repeatedly has praised, amid the widening federal investigation into possible coordination last year between Trump's presidential campaign and Moscow.

Even before Trump signed the measure into law, its passage sparked a harsh reaction in Moscow.

Putin announced last week the United States would need to shed 755 personnel, including U.S. diplomats, from its embassy and consulates in Russia. President Barack Obama expelled 35 Russian diplomats, said to be spies, from the United States in December.

Trump has yet to voice objections to Putin's actions, though members of his administration have.