SAO PAULO, Brazil — Barbarity and progress, apocalypse and hope. Carandiru and Sorocaba represent the extremes of the national drama that is Brazil's prison system.

The Carandiru House of Detention, Latin America's largest prison with about 7,000 inmates, was the scene of a nightmare in 1992. When rioting inmates barricaded themselves in a cellblock, police attacked. Commandos in ninja masks pulled inmates from under their bunks and executed them, shouting, "You're going to hell!" The raiders set dogs on survivors and made them run a gauntlet naked, splashing through pools of blood. The carnage left 111 inmates dead.

A commander of the raid, Col. Ubiratan Guimaraes, was later elected to Congress. The number he used to identify himself on the ballot was a macabre reminder of the massacre at Carandiru: 111.

Guimaraes was indicted this year for the killings after his congressional term ended, but his election in 1994 suggested that crime-weary Brazilians had little sympathy for inmates. So it was logical to fear the worst in December 1997, when a standoff occurred at another Sao Paulo state facility, the Sorocaba maximum-security prison. Two dozen inmates armed with pistols and clubs took more than 700 hostages, mostly visiting women and children.

This time, however, a four-man crisis team negotiated for three days, whittling down the number of hostages and mutinous inmates. On New Year's Eve, a specially trained squad stormed in and liberated the remaining hostages without killing anyone. About 100 such incidents have been resolved peacefully since the state negotiating team was created in 1996.

"The prison population gets the message," said Joao Benedicto de Azevedo Marques, state director of prisons. "They know there will be no violence, but we will not back down."

Seven years after the Carandiru massacre, Sao Paulo state continues to endure a prison crisis. It is a microcosm of the crisis in Brazil and the rest of Latin America. Correctional facilities are repositories of the accumulated woes crippling the region's justice systems. From Brazil to Venezuela to Paraguay, potential Carandirus boil over with overcrowding, squalor, corruption, and violence on the part of both inmates and authorities.

But in Brazil, a mix of traditional and unorthodox reforms offers glimmers of progress on a gloomy horizon. Federal officials realize that a time bomb is ticking, and they are trying to defuse it. They are more than doubling the prison system's capacity, building a record 52 new penitentiaries--21 of them in Sao Paulo state--creating an academy for guards, computerizing inmate rolls and expanding the uses of parole and probation.

"This is the start of the change of penitentiary culture," said Sergio Seabra, chief of Brazil's federal prison agency. "This is shock treatment."

Smaller-scale initiatives in Sao Paulo state, with a population slightly larger than California's and 40% of Brazil's inmates, have demonstrated that honesty, innovation and common sense are just as important as bigger budgets.

"These are simple, obvious things we have done," said Nagashi Furukawa, a judge who created a model jail and recently became a special advisor to Sao Paulo's governor. "They did not cost a lot of money. It is a question of changing mentalities among the inmates, the government, the community."

Crime, Courts Add to Need for Change

The urgent need for change stems from the Brazilian system's size--Latin America's largest with about 170,000 inmates--combined with skyrocketing crime and antiquated, overwhelmed courts.

In Brazilian prisons and jails, hardened killers live alongside accused purse snatchers. Lack of space forces inmates to sleep on top of hole-in-the-floor toilets or in hammocks fashioned out of blankets. Daily escapes and riots make guards so fearful that they refuse to enter cells to tend to the sick.

"Appalling" conditions are "in great part due to an absence of political will to remedy them," Human Rights Watch concluded in a report this year. "Some of the most extreme cruelties, such as summary executions . . . can in no way be attributed to meager public resources."

In Paraiba state, human rights researchers found an inmate who had spent almost four years behind bars awaiting trial. In Amazonas, convicts were not released upon completing sentences. In Sao Paulo, authorities shut down a hospital ward after discovering that several paraplegic inmates had died and that others were neglected except for primitive medical attention from fellow convicts.

Throughout Brazil, overcrowding of prisons turns jails and police lockups into de facto penitentiaries. Many convicts spend years in facilities intended only for short stays and staffed by the civil police, the force that handles criminal investigations.