"The Singing Detective" has more to do with singing than detecting but relatively little to do with either. What it has to do with is guilt, fear, paranoia, recrimination and pain, and the blessings that go along with the sheer curse of a creative imagination.

A six-part BBC production imported by WETA, "Detective" sprang from the mind of Dennis Potter, who created the similarly mordant serial "Pennies From Heaven" a few years ago. As it was in "Heaven," characters in "Detective" have a tendency to break into song at unexpected moments, lip-synching recordings of vintage tunes that range from "The Very Thought of You" to "The Teddy Bears' Picnic." Even though Potter has used this before, it doesn't seem a stale gimmick.

Indeed, "The Singing Detective" is like nothing you've ever seen before even if you think you have.

Channel 26 will air the serial two episodes at a time, tonight at 9, tomorrow at 10 and Wednesday at 9. It would have made more sense to show one a night for six nights -- perhaps even at the same time each night -- but the programming patterns at that station have always been on the inscrutable side.

"Detective" is inscrutable for quite a while, too. At first it appears one can look forward to spending six hours with a disfigured man suffering from an unsightly skin disease as he rants, raves and hallucinates from his hospital bed. And come to think of it, that is pretty much the scheme of things.

And yet the way the fellow drifts into and out of fantasies, the way characters wander into and out of the various story lines, and the impudent device of the abrupt musical interludes keep one gripped and guessing. Director Jon Amiel plays the time games skillfully and maintains a fascinatin' rhythm all the way through.

Michael Gambon, who portrays the stricken author of the detective stories and, in dreamlike sequences, the detective himself (both stuck with the preordaining moniker of Philip Marlow, spelled without the "e"), manages the most graceful and unself-conscious kind of tour de force. He's a master of every warped moment, whether confined to his hospital bed, parrying with the staff psychiatrist, or wandering through the detective story with the occasional song in his heart.

No one should tune in expecting genteel or tidy "Masterpiece Theatre" fare. "The Singing Detective" means to be outrageous at times, and is. Our poor, overcooked, fitfully foul-mouthed hero, blistered from top to bottom, must be greased now and then by an angelic, doe-eyed nurse (Joanne Whalley), and his attempts to prevent himself from becoming sexually excited fail.

An extremely naked and very dead woman is pulled out of the Thames -- repeatedly, as the author's new detective story takes, and changes, shape in his head -- and in one conjured fantasy, a villain (Patrick Malahide) watches himself in a mirror while having intercourse. Old Philip keeps returning to memories of young Philip spying on a lovemaking couple in the woods, near the tree in whose uppermost branches he takes refuge from the dirty old world.

Janet Suzman, though she gets star billing, doesn't appear until the end of Part Two, and then briefly, as Marlow's wife, whom he seems to hate bitterly. But then he's the sort who would even love bitterly. The time he spends recovering from his dermatological horror is also spent sorting out, identifying and finally exorcising demons.

Potter's dialogue is wickedly sharp, whether parodying the patois of literary gumshoes ("the air was like an Eskimo's mother-in-law -- bitter and icy") or offering the occasional political aside, as when author Marlow contemplates the other uses to which he might have put his writing talent: "speeches for Mrs. Thatcher, obituaries -- or is that the same thing?"

From his bed in the ward, Marlow lashes out at the world, braying and mooning and revealing a cynicism bordering on misanthropy. And yet you feel for him, partly because Gambon plays him without any groveling for sympathy, partly because even in his wild ramblings he keeps making sense. "Minute by minute we make our own lives; that is the point," he says in existential summation.

In "Pennies From Heaven," the lugubrious tale of two Depression-era waifs, Potter relied on the song gimmick perhaps too heavily. It became repetitious, and the story didn't develop so much as lurch from tune to tune. In "Singing Detective," the songs are used more sparingly, and most of the time they catch one by surprise and enhance Marlow's perspective.

A team of doctors who gather at the foot of his bed and rattle on in gibberishy medicalese is transformed in Marlow's mind into a vocal group dishing up "Dem Bones." Soldiers on a wartime train become the Mills Brothers and, moments later, the Ink Spots.

Philip is played as a moody, introspective 10-year-old by Lyndon Davies, who looks a great deal -- uncannily so, in some shots -- like Bill Moyers. An unsavory incident from the boy's school days involving a graphic expression of displeasure left on the teacher's desk is protracted beyond all reason, and other scenes drag on well past a tolerable length.

Yet the story remains compulsively engrossing, in part because one never really does know what is around the next corner of the maze. Characters from the book in Marlow's head might suddenly begin speaking their punctuation as well as their dialogue; two trench-coated toughs protest the fact that they haven't even been given names, much less raisons d'etre ("we're padding," one complains) and precipitate a hilarious shoot-out in the ward.

"The Singing Detective" is a comedy of misery, scrupulously bleak and audacious, yet given to moments of affecting, melancholy lyricism, as when an infirm, elderly man suddenly breaks into "It Might as Well Be Spring" from his bed and Marlow flashes back to his weak-willed father singing it for a barful of wartime customers who obligingly join in.

At moments like this, one realizes there've been few other television entertainments that have had moments like this. "The Singing Detective" is dark, twisted, melodic and momentous.