On Wednesday, something surprising and strange happened in Brazil: The president, Dilma Rousseff, appointed former President Luiz Inácio Lula da Silva, her political mentor, to her own Cabinet.

That same day, a Brazilian judge ordered the government to release a wiretap recording of Rousseff — yes, the government is listening in on its own president — speaking to Lula, as he's commonly known.

The recording seemed to reveal that Rousseff had appointed Lula to save him from prosecution in a multibillion-dollar corruption scandalinvolving the state-run oil company Petrobras. (Under Brazilian law, ministers go to special courts that have historically never prosecuted them.)

Subsequently, a separate judge ordered a pause on Lula's appointment, suggesting it may have been illegal.

This is just the latest House of Cards–style twist in the Petrobras scandal — the largest corruption scandal, in dollar terms, in any democracy ever.

"Guys," he told prosecutors, "if I speak, the republic is going to fall"

The scandal is shaking the Brazilian political establishment to its very foundations — and it comes amid a recession, as well as a largely separate effort by Rousseff's political rivals to impeach her over unrelated financial improprieties.

Millions of Brazilians have taken to the streets in protest. The Brazilian political system is paralyzed, and could well be thrown into further chaos if Rousseff is ousted.

But in the long term, some observers say that the Petrobras investigation is a good thing. Brazil has been plagued by severe corruption for much of its modern history. This scandal could signal that the era of impunity may be ending. Until just six years ago, no sitting politician had ever been successfully prosecuted on corruption charges.

What we're seeing today could potentially be epochal: one of the world's largest countries finally coming to terms with one of its biggest problems. But that's the long, optimistic view. For now, it's a huge mess.

Here are the basics of what's happening, why it matters, and how this became such a big deal.

How the Petrobras scandal worked

Between about 2004 and 2014, the state-run energy firm Petrobras — which is Brazil's largest company and one of the largest corporations in the world — engaged in one of the most astonishing corruption schemes ever to be uncovered. That Petrobras employees and their co-conspirators thought they could get away with it speaks to just how bad corruption in Brazil had become.

Nobody knows who exactly came up with the scheme. But it was developed during the commodities boom of the 2000s, when oil prices were high, and involved three main groups of players: leaders at Petrobras, top executives at Brazil's major construction companies, and Brazilian politicians.

It worked in four steps:

Construction executives secretly created a cartel to coordinate bids on Petrobras contracts and systematically overcharge the company. A select group of Petrobras employees turned a blind eye, allowing the construction companies to charge Petrobras outrageous sums. The construction executives then pocketed the proceeds from these inflated contracts and rewarded their partners inside Petrobras with big bribes. Some of the proceeds also got sent to friendly politicians, as either personal gifts or donations to their campaigns. Because Petrobras is partially owned by the state, politicians can install people as executives — who then turn around and reward that politician with a bribe.

Huge sums of money, according to the New York Times, would be "hand-delivered by an elderly gentleman who flew around the world with bricks of cash, shrink-wrapped and strapped beneath thigh-high socks and a Spanx-like vest." Sometimes bribes would be distributed in the form of "Rolex watches, $3,000 bottles of wine, yachts, helicopters and prostitutes."

All in all, somewhere upward of $5.3 billion changed hands as part of this scheme.

Why corruption is so bad in Brazil

It seems astonishing that anyone involved in the Petrobras scheme thought they could get away with something this large. But corruption has long been widespread in Brazil. To understand how that came to be, you need to go back a bit — to the first days of the country's founding.

The Portuguese began colonizing the area we now know as Brazil during the early 1500s, and before long it became the hub for the Atlantic slave trade, dwarfing even the colonies in what would become the United States. Portuguese colonists used slaves to grow sugar and mine gold, bringing those colonists enormous wealth — and developing what would become a deeply entrenched caste system.

By the time Brazil declared independence, in 1822, the caste system had hardened along clear lines — the white elite was fabulously wealthy, while the darker-skinned slaves and laborers were deeply impoverished.

The country formally abolished slavery only in 1888 — the last country to do so in the Western world. But class hierarchies remained. The Brazilian elite retained their advantages, and used their wealth to entrench their own power in society. One common method they developed for doing this was by bribing government officials.

"Brazil is a country that's defined by its income inequality," Brian Winter, the vice president of the Americas Society and Council on the Americas, told me. Winter calls this inequality "the most important structural factor" in encouraging Brazilian corruption.

"Until 2010, the chances of you going to jail, especially if you were a politician, were virtually nil"

"You had this tiny elite that really thought they could get away with anything, and often did," he said. "The corruption comes from that."

This problem persisted throughout Brazilian history, through multiple governments and even a military coup in the 1960s.

"There's a famous president of Brazil who ran on the slogan of 'sweeping away corruption' in the 1950s," Matthew Taylor, an American University scholar who studies Brazilian corruption, explained. "He even carried a broom around ... to show that he was serious about corruption."

Over time, corruption became more normalized. For decades, prosecutors and police failed to investigate corruption, creating a climate of impunity in which even the grossest corruption became business as usual.

"Until 2010," Taylor said, "the chances of you going to jail, especially if you were a politician, were virtually nil."

Estimates suggest that in Brazil, roughly "3 to 5 percent of GDP is lost to corruption," according to Taylor.

It's important to note that Brazil is far from unique in this regard: A number of other Latin American countries, for example, have similar corruption problems, sometimes owing to similar factors. But Brazil is a much bigger country — the world's fifth largest by population and seventh by GDP — so the scale of the problem is larger as well.

Petrobras, then, is the culmination of what happens when you have a corruption problem building, more or less unchecked, over several generations and in one of the world's largest countries.

One of the craziest parts of this scandal: how it was uncovered

The Petrobras scandal came to light almost by accident, in mid-2013, when Brazilian police detained a money launderer named Alberto Youssef, who had been previously arrested nine times, on yet another money laundering charge.

But this time, Youssef had something very different to say.

"Guys," Youssef reportedly told prosecutors, "if I speak, the republic is going to fall."

Youssef began to describe what we now know as the Petrobras scandal. Police, in response, in early 2014 made their first arrest in Operation Car Wash, so named, reportedly, because some of the money had been laundered through an actual car wash.

The first to fall was Paulo Roberto Costa, a former Petrobras official whom Youssef had bribed by giving him a Land Rover. Police arrested Costa in March 2014, and he too cooperated. Between those two sources, police had enough information to begin going after some of Brazil's wealthiest and most powerful individuals.

The investigation into Petrobras is run out of Brazil's Public Ministry, a unique institution established by the Brazilian constitution. Basically, it's like an attorney general's office designated specifically for cases in the broad public interest, including wrongdoing by government officials.

"Some people liken it to a fourth branch of government," Taylor explains. "It has its own budget, and each of the prosecutors is autonomous — not just from the executive, but from each other."

"Petrobras is a different kind of scandal"

This institution exists, according to Taylor, as a direct result of Brazil's long struggle to throw off its history of dictatorship.

In 1964, the Brazilian military overthrew the president and instituted a dictatorship that lasted for 20 years. In 1985, when Brazil returned to democracy and established a new constitution, the country's leader felt that a special prosecutor's office was necessary to restore rule of law and democratic accountability.

In 1998 this office was strengthened further, and was granted the complete independence it enjoys today.

"Coming out of 20 years of dictatorship," Taylor says, "the country was looking to repair some of the damage that dictatorship had caused."

Over time, rule of law and democratic governance strengthened in Brazil in other, more organic ways. The police and judiciary became more assertive and more independent, for example, as did prosecutors. As the legacy of dictatorship waned and those institutions developed, they gradually became strong enough, and independent enough, to take on larger corruption issues that might have otherwise been tolerated.

This development is an important part of how the Petrobras investigation came to be. A group of officials from across the Brazilian government had developed real expertise investigating and prosecuting corruption cases.

Working with a judge named Sergio Moro, who has a lot of experience with anti-corruption investigations, they began pulling on the thread provided by Youssef's 2013 confession. But they had no idea how much it would eventually unravel.

How Petrobras became a national crisis

Between March 2014 and March 2015, dozens of engineers, construction executives, and Petrobras officials were arrested as part of Operation Car Wash. Each arrest allowed prosecutors to gather more evidence, often through plea bargains wherein implicated figures outed co-conspirators.

In March 2015, the scandal really blew up: Brazil's Supreme Court announced that it was investigating 34 politicians on suspicion of involvement in the scandal — a political scandal.

Since the scandal began, 86 people have been convicted of crimes relating to Petrobras in total, most of them members of Brazil's political or economic elite.

The scandal has infuriated the Brazilian public, sparking enormous demonstrations. Last Sunday, somewhere between 1 million and 3.5 million people turned out to protest.

"There has always been a general acknowledgement by everyday citizens that there is some degree of corruption," Natasha Borges Sugiyama, a political scientist at the University of Wisconsin Milwaukee, says. "Petrobras is a different kind of scandal."

According to Sugiyama, part of the issue is scale: Ordinary Brazilian corruption doesn't cost the nation billions of dollars. But it's also about who is involved in the corruption. Leaders of Brazil's largest state-owned company, its biggest construction firms, and political leaders from across the political spectrum have been fingered by prosecutors. It's a damning indictment of the entire elite class.

"The scandal has implicated broad swaths of the elite," Sugiyama says. "It has revealed people's disquiet [with] the political elite."

As one telling example, it's worth dwelling on the conviction of Marcelo Odebrecht, the head of Brazil's largest construction company. In March 2016, Odebrecht was sentenced to 19 years for bribery and money laundering.

"The recession is driving the anger"

Odebrecht is old money: His company was founded by his grandfather, who had built it up in the 1950s. Odebrecht exemplifies everything that infuriates Brazilians about the Petrobras scandal: a rich boy who has corrupted the Brazilian government in order to make himself even richer.

Worsening outrage, Brazil's economy is doing poorly at the moment. Years of high government spending helped create a debt and inflation problem. The collapse in oil and other commodities prices has shrunk some of Brazil's most lucrative industries. It's an even worse version of the stagflation Americans experienced in the 1970s: The Brazilian real (its currency) is rapidly losing value at the same time as the country is experiencing a recession.

The malfeasance of the Brazilian elite, then, is coming out just as ordinary Brazilians are experiencing truly difficult times. Traditional anti-elitism and economic pressures are sparking an explosion of anger.

"The recession is driving the anger," Winter says. "There was a corruption scandal of smaller, but still epic proportions, 10 years ago ... and the [incumbent] government survived, in large part because the economy was good."

Petrobras is endangering President Rousseff's government

The Petrobras scandal is a true disaster for President Rousseff — polls have found that about two-thirds of Brazilians want her impeached.

The problem isn't that she was directly involved. "Nobody believes Dilma participated in this," Winter says flatly.

Still, from 2003 to 2010 Rousseff was the chairwoman of Petrobras's board. This all occurred under her watch, a seemingly damning indictment of her own judgment and competence.

It's also damaging to her party. The Workers' Party (PT) has cultivated a reputation for cleanliness, for sticking up for the common people against a corrupt system. The clear evidence that a number of PT politicians were involved in Petrobras has tarnished that brand considerably.

"The PT, when it first came to office, was seen as the party of ethics that would clean up Brazilian politics," Taylor says. "For Brazilians to see the PT arm in arm with politicians who have been proven to be corrupt ... is extraordinarily frustrating."

Which brings us to Lula, the former president now implicated as well.

Two weeks ago, police searched Lula's home in connection with Petrobras, and temporarily detained him. Later that day, police released a statement saying that he had personally taken dirty money, leading many Brazilians to conclude that he would soon be indicted.

This has been damaging for Rousseff as well. Lula, who was president from 2003 until 2011, was Rousseff's political mentor. They are often viewed as something of a political pair, for better or worse.

But the protests against them have tended to draw from the middle class and other relatively privileged segments of society.

"Polls suggest the demonstrations are dominated by white and upper-middle-class people," the BBC reports. "One survey of protesters in Sao Paulo indicated that protesters were much wealthier than average and that three-quarters were white (compared to less than half of the general population)."

That's because Lula and Rousseff have made care for the poorer and less white segments of Brazil a top priority. Lula, in his presidency, pushed through the Bolsa Familia program, a poverty relief initiative that distributed cash to about 12 million poor Brazilian families.

As a result, many poor Brazilians are deeply loyal to the PT, even in the face of the Petrobras scandal. You can see how this plays out in a photo from a recent protest: It shows a white couple walking to a protest with their dog, dressed in Brazil's national colors. Alongside them there's a black nanny, dressed in an all-white maid's uniform, walking their kids in a stroller:

Photo: Rich white couple go to protest, drag black nanny along in white uniform (Brazil) https://t.co/HUQcbmVAv4 pic.twitter.com/ePrmUT6kLg — SubMedina (@SubMedina) March 15, 2016

The photo went viral and has come to symbolize, for many Brazilians, the way the upper middle class set the political agenda over Petrobras.

What happens next?

Barring any more shocking revelations — which I wouldn't rule out — the big political questions going forward are whether Rousseff survives to serve out her remaining time in office (her term ends in 2019), and what the consequences of the scandal will be for Brazil's 2018 national elections to pick her successor.

For now, Rousseff is refusing to resign. Speaker of the House Eduardo Cunha — who is himself, ironically enough, being investigated as part of the Petrobras scandal — filed impeachment charges last year, but the charges died due to lack of political support.

However, the new evidence against Lula — as well as the secondary scandal surrounding Rousseff's attempt to appoint Lula to her Cabinet — may well change the impeachment calculus. On March 17, Cunha refiled the impeachment charges.

The impeachment charges are not actually connected to Petrobras. Rather, Rousseff is accused of fudging government accounting to hide the scope of the government's deficit problem during the 2014 reelection campaign.

But legislators may be more likely to support her impeachment because of Petrobras. If public outrage keeps up, legislators may feel real pressure to vote against her.

"The big question is what the street protests do," Taylor says. "There were big protests on Sunday — 3 million — and those were aimed largely at fence sitters in Congress."

If Rousseff is impeached, her vice president (who is, due to Brazil's system, from a different party) takes over. But the political damage done to the PT would likely be devastating. This would likely help another party win in 2018.

But parties across the political spectrum are involved in the scandal. That, together with the ongoing economic problems, might empower the rise of a previously weak or entirely new party.

"Look at politics in our country; the factors that allow [political] outsiders to come in are very strong around the world right now," Winter says. "Think about how appealing an outsider might be in Brazil, where things are actually bad!"

There's another, even crazier way by which Petrobras could cost Rousseff her job.

If strong evidence surfaces that Rousseff's election campaigns were funded by dirty Petrobras money, which seems easily within the realm of possibility, a court could rule that election null and void. Winter suggests thinking about this possibility like a sports team forced to give back a championship title after a cheating scandal.

This procedure, called cassação, would create even more chaos than an impeachment.

If there's a cassação ruling, both Rousseff and her vice president would be forced out of office: After all, the VP was also elected at the same time. Cunha, the speaker of the house, would then take over. But remember, he's also being investigated on suspicion of involvement with Petrobras, and thus at risk of impeachment.

The Brazilian political system, then, is potentially nearing a point of real chaos. The president is facing impeachment and might be thrown out by the courts — which means she has substantially weakened power to pass new legislation to deal with Brazil's economic problems. Leading opposition figures, as well as former leaders, are also in serious trouble.

But it's worth remembering the reason the political chaos happened in the first place: A government investigation uncovered and stopped the biggest corruption scandal in the history of modern democracy.

This isn't just a one-off accomplishment. The investigations of leading politicians like Lula, and the conviction of leading businesspeople like Odebrecht, may signal that Brazil's centuries of impunity are finally coming to an end.

"If you were trying to decide on material grounds whether the costs of corruption outweighed the benefits [before Petrobras]," Taylor says, "you would fall — easily — onto the side of becoming corrupt."

But the Petrobras investigation, and the broader anti-corruption effort it represents, "have changed that calculus," he says. That makes it, in his words, "momentous."