A Manhattan judge imposed a 40-year prison sentence on a Canadian man who admitted to plotting ISIS-inspired bomb attacks on Times Square and city subways.

Citing Abdulrahman El Bahnasawy’s mental health and addiction issues federal Judge Richard Berman sentenced the 20-year-old to less than the life sentence sought by prosecutors.

Still, the man’s mother cried out, “You have no justice! This is a sick boy! This is crazy! This is not justice!” before two court officers escorted her out of the courtroom.

El Bahnasawy pleaded guilty to the crimes in October 2016 after he and two accomplices were brought down by an undercover agent posing as an ISIS supporter to whom they revealed their plot.

Lawyers for El Bahnasawy argued for a lenient sentence that would allow him to receive treatment since he has been diagnosed with bipolar disorder and addicted to drugs since he was 14.

“The United States Bureau of Prisons cannot provide the treatment that this young man needs,” said lawyer Andrew Frisch.

The attorney argued that El Bahnasawy no longer sympathizes with ISIS since receiving mental health treatment.

El Bahnasawy asked the judge for a second chance.

“I am continually changing from the person I used to be. I am nowhere near my mentality from before,” Bahnasawy said.

In prison he said, “There are drugs and I can’t always stop from relapsing but I try my best.”

Prosecutor George Turner, arguing for a life sentence, said El Bahnasawy’s “issues only make him more volatile, more of a risk.”

Judge Berman said he had to impose significant prison time because, “We can’t be sure that the past is not a prologue for the future and it’s too big of a risk.”

Berman said the sentence, while stiff, still gave “Mr. Bahnasawy a future and an opportunity to turn his life around.”

The trio of conspirators — including Talha Haroon, a U.S. citizen, and Russell Salic, of the Philippines — were brought down after communicating about their plans to an undercover agent who was posing as an ISIS supporter.

El Bahnasawy pinpointed locations for attacks on a subway map and even bought bomb materials that he transported from Canada to the U.S. He said he wanted to “create the next 9/11,” according to prosecutors.

Haroon was arrested in Pakistan and Salic was arrested in the Philippines. Both remain incarcerated abroad pending extradition proceedings.