LIMA, Peru (AP) _ The inmates ran Canto Grande prison - staging plays with proletariat heroes, decorating the walls with portraits of Mao, even making bombs in the craft shop and building defense barriers.

On May 6, police finally challenged the Shining Path inmates in a five-day battle that left dozens dead at the facility in the outskirts of the capital.

The Canto Grande episode illustrates how a highly disciplined, fanatical rebel organization has taken advantage of Peru's corruption and inefficiency and challenged the government.

It also underlined President Alberto Fujimori's determination to crush the rebels as part of his military-backed crackdown that began April 5.

Many of the Maoist group's top leaders were held in the maximum security prison. Poorly paid guards not only lacked the motivation to confront the prisoners, they also are believed to have sold weapons to them.

Internal security documents from the penal system's files made available to The Associated Press describe several incidents in which police delivered drugs and guns. Several inmates were wounded or killed in shootouts before the crackdown.

''The police have brought in firearms, short machine guns, and drugs,'' reads a typical entry. Other entries record, matter-of-factly, prisoner casualties.

Police who entered the prison last week found that their floor plans did not correspond to the interior of the cellblock. The prisoners had built new walls and zig-zag corridors to defend themselves.

Shining Path rebels ''have always been very good teachers,'' said Carlos Ivan Degregori, director of the Institute for Peruvian Studies. ''The jails, for example, are like a university for their members.''

''A Shining Path member who lands in jail has the possibility in prison to continue intense courses on Marx, Lenin, Mao ...,'' he said.

Amid the chaos and poor living conditions in Canto Grande, the nearly 600 Shining Path inmates kept their own schedules for eating, sleeping, cleaning, work and study.

They set up a shoe repair shop and had an oven to bake their own bread. They painted murals of their idols on the prison yard walls and held marches and plays celebrating their Maoist convictions.

In photographs published July 1991 by the newsmagazine Caretas, murals of Marx, Mao and Guzman look down on rebels acting out a Maoist drama about the defeat of the revisionists by the proletariat.

Prison crafts shops used by the Shining Path produced more than eating utensils. Police displayed crossbows, homemade bombs, crude knives and blow darts.

The government said 36 rebels and two policemen were killed. Rebel sympathizers claim more than 100 of their comrades died. Several rebel leader were among those confirmed dead.

A state of emergency was declared in the prison in 1987 by then-President Alan Garcia. He ordered police to take control from the National Penitentiary Institute. But penal system authorities continued to administer the jail.

The rebels apparently were able to use the competing authorities to their own advantage.

After the uprising, police said relatives of the inmates had smuggled weapons and explosives into the cellblock. But penal system investigators said police themselves had been bribed to let in the contraband.

A past prison director at Canto Grande filed a complaint in court against a police major who allegedly permitted the delivery of bricks and cement to Canto Grande without permission, informants said. No action was taken against him.

More than 25,000 people have died in political violence since the Shining Path launched its armed insurgency in May 1980.

Shining Path founder Abimael Guzman is a former philosophy professor from the University of Huamanga in Ayacucho, located 230 miles southeast of Lima.

Known to his followers as ''President Gonzalo,'' Guzman's adaptation of Mao to Peru's social circumstances is the foundation of the Shining Path.

Subversion experts say that despite the presence of many Shining Path leaders in Canto Grande, the prison did not function as a command post.

Instead, it became a valuable training center, cementing the beliefs of ''President Gonzalo's'' followers.