In a startling example of how the NYPD bends the law for friends, an NYPD deputy inspector shamelessly wrote a memo on his own departmental stationery ordering a lieutenant to fix a traffic ticket for a lifelong pal, The Post has learned.

The lieutenant then ominously warned the Bronx cop who wrote the ticket “it would be in his best interest” to make it disappear, sources said.

But the scam unraveled after that cop asked the advice of another officer — who unbeknownst to him was being recorded by the Internal Affairs Bureau.

When the final act played out in Bronx Traffic Court, an audience that included several IAB members watched as the officer told the truth and the inspector’s hapless buddy was found guilty.

The inspector’s memo — obtained by The Post — proves the exploding scandal extends all the way to the upper echelons of the department, the source said.

“This just shows the culture of the job, how this inspector could nonchalantly send a memo on his own stationery to another boss,” he said.

Sources say as many as 40 cops could face criminal charges in cases being investigated by the Bronx DA’s Office, and up to 500 could face departmental disciplinary action.

The memo was written by Bronx Deputy Inspector Wayne Bax, 58, once a rising star whose career was sidetracked after an allegation that he perjured himself to cover up an illegal arrest in 1996. He was not charged in the previous case, but was transferred and never again promoted.

No charges have been filed against Bax or the lieutenant, Joseph On, pending the conclusion of the IAB probe.

The ticket Bax allegedly tried to fix had been issued to Andrew J. DiPaola Jr., 56, who was convicted yesterday of making an illegal left turn.

Bax faxed a memo to On of the 52nd Precinct, who passed the message on to the officer who’d written up DiPaola and subsequently asked his fellow cop for guidance.

Bax yesterday declined to comment.

DiPaola said he grew up with Bax, but denied asking him for any favors, saying he hadn’t seen his lifelong chum since the funeral of Bax’s wife, Marianne.

“Bax is squeaky clean and the last guy who would do something like that,” said DiPaola, who was driving a truck for Skyview Plumbing and Heating when he got the ticket.

DiPaola told The Post the company has a cozy relationship with the NYPD — even letting undercover cops use its trucks for surveillance — and he admitted asking cops he knew what could be done about the ticket.

“I talked to a few cops and asked them a few questions,” DiPaola said.

DiPaola said the cops he spoke to told him all he could do was make his case to the judge.

“They said they can’t do anything, just tell the judge your story and that’s it,” he said.

But the judge didn’t buy it, and DiPaola got two points on his license.