Nobody here can do the maths!” Alberto Youssef despaired in an expletive-filled phone conversation wiretapped by Brazilian prosecutors three years ago. Mr Youssef, since convicted of helping to move $444m to offshore bank accounts in thousands of separate transactions, is a money launderer-turned-whistleblower in Brazil’s so-called Petrolão — the sprawling corruption scandal that has hit the highest levels of government.

The scandal, which saw billions of dollars skimmed from construction contracts taken out by Petrobras, the state-controlled energy company, to be used as kickbacks for politicians, has filled Brazil’s streets with protesters and led to calls for the impeachment of Dilma Rousseff, the most unpopular president in Brazilian history. Quite some maths.

Mr Youssef’s job, in essence, was to prod business executives for bribes and soothe the avarice of politicians, most of them in the governing Workers’ party. The politicians’ greed was remarkable — but in keeping with the times. Those were Brazil’s boom days, when commodity prices were high and many Brazilians believed that the optimistic horizon of their “country of the future” had actually arrived. Life for some was sweet: over $2bn was stolen in bribes.

But optimism, and Brazilians are famously optimistic, is only pessimism without the extra information — and much of that information has since been revealed. Dozens of senior business executives have been sent to jail for their role in the Petrolão and over 30 politicians are being investigated for corruption. The boom is also over and much of the commodity windfall, it turns out, was misspent on new shopping centres instead of social services. The anger Brazilians feel stems from a sense that their future has been stolen. The economy has plunged into its worst recession since the Great Depression. Brazil’s “moment” has passed on.

In some eyes, it has moved to Mexico, in many ways Brazil’s opposite. While Brazil’s economy is bowed down by corruption, protectionism and Ms Rousseff’s fondness for interventionism, Mexico’s already liberal economy is being liberalised further, and its protected oil sector opened to foreign investment. Brazil’s commodity-led economy is hitched to a slowing China; Mexico’s low-cost manufacturers are linked to the recovering US. President Enrique Peña Nieto has proclaimed a new era.

Unlike Brazilians, though, most Mexicans have not drunk the “new era” Kool-Aid. Legal impunity is rampant. While Mexico is far ahead of Brazil in the World Bank’s ease of doing business survey (at 39th position, ahead of Chile and Israel, versus 120th for Brazil), the opposite is true on the rule of law. Mexico, for example, ranks 103rd on Transparency International’s corruption perceptions index, worse than China, while Brazil at 69th position is level-pegged with Italy and Greece.

Mexico’s institutional rot seems to extend from top to bottom. The president has suffered a series of conflict of interest scandals involving his wife and finance minister. Journalists who report on disappearances and drug-fuelled violence, such as the presumed murder of 43 students last year, are themselves killed. This week, more than 500 intellectuals slammed Mexico’s “censorship by bullet”. While in Brazil criminals are being jailed, in Mexico drug lords can escape from high-security prisons through tunnels built into their cells’ showers. The government response to outcries after each shameful incident? Muted embarrassment and a business-as-usual attitude emphasising economic reform over the rule-of-law problems that are Mexico’s biggest concern.

What Mexico’s presidential palace does not seem to realise is that insecurity and lawlessness also have financial implications. At the turn of the century, Brazil and Mexico had equal stocks of foreign direct investment, equivalent to 19 per cent of gross domestic product. Since then, that stock has risen in Brazil to 32 per cent, while in Mexico, so “open for business”, it has increased to just 26 per cent. Even today, despite the ebbing commodity price boom, annual foreign investment inflows to Brazil are running at almost 3 per cent of GDP; in Mexico, they are at 2 per cent. Investor surveys also reveal increasing concern about legal impunity and insecurity.

Mr Peña Nieto once shrugged off a question about corruption as a “cultural issue”. But times have changed, and culture must with it. Corruption scandals are now being prosecuted more quickly in Latin America — largely because prosecutors and citizens are building on the investigations that probed the region’s human rights abuses of the recent past. Standards have shifted.

Brazil, with its relatively strong legal institutions and hard-wired belief in freedom of expression, seems to understand this. Mexico, with weaker institutions and a younger democracy, does not. It would be foolish to believe the Petrobras corruption probe will fundamentally change Brazilian political and business life. (Did Italy’s “Clean Hands” movement remove its structural problems?) But, at the very least, the bringing down of previously untouchable figures represents the possibility of a new direction. In Mexico, by contrast, no senior figures are sent to jail, and corruption can feel rampant.

If Mexico got rule of law right, foreign direct investment inflows might rise to Brazilian levels — an extra $13bn or so a year. At its crudest level, the Mexican government just needs to do the maths.

johnpaul.rathbone@ft.com