Sri Lanka's parliament elects a new president on Wednesday to replace Gotabaya Rajapaksa, who last week fled to Singapore and resigned following months of protests over the country's financial meltdown.

AFP looks at how cash-strapped Sri Lanka ended up in its worst-ever economic crisis, and what comes next in its complicated, corrupt and sometimes violent political system.

Sri Lanka's financial woes were triggered by the coronavirus pandemic but exacerbated by mismanagement under Rajapaksa's government.

The country has been unable to finance even the most essential imports since late last year, and has since defaulted on its debt.

Discontent had been mounting for months over severe food and fuel shortages, record inflation and lengthy power cuts.

Even Rajapaksa's closest allies began abandoning him, and when protesters overran his official residence in Colombo this month, he was forced to flee to a navy base and then to Singapore in fear for his life.

Rajapaksa was dubbed "The Terminator" for ruthlessly crushing Tamil rebels as head of the defence ministry during his elder brother Mahinda's presidency between 2005 and 2015.

He was loved by the country's Sinhala Buddhist majority, but loathed by Tamils and Muslims who saw him as a war criminal, a racist and an oppressor of minorities.

When inflation crossed 50 percent, and with four out of five people forced to cut back on food because of acute shortages, the ethnically divided nation united in its opposition to Rajapaksa.

Rajapaksa formally quit on July 14, just two years and eight months into his five-year term, with Prime Minister Ranil Wickremesinghe automatically elevated as the acting leader under the country's constitution.

Wickremesinghe is serving as a stop-gap until Wednesday, when the 225-seat parliament elects one of its members to lead the country for the balance of Rajapaksa's term.

The 225 MPs will rank the three candidates in order of preference in a secret ballot.

Candidates need more than half the vote to be elected. If no-one crosses the threshold on first preferences, the candidate with the lowest support will be eliminated and their votes distributed according to second preferences.

A cadet walks in front of the Sri Lankan president’s official residence on Friday after it was overrun by protesters last weekend
A cadet walks in front of the Sri Lankan president’s official residence on Friday after it was overrun by protesters last weekend AFP / Arun SANKAR

The secret ballot gives MPs a freer hand than an open poll, and previous elections have seen allegations of bribes offered and accepted in exchange for votes.

During a constitutional crisis in October 2018, some MPs said they had been offered $3.5 million in cash and apartments abroad for their support.

Acting President Wickremesinghe, 73, a pro-Western six-time prime minister, appears to be the front-runner.

He has secured support from the leadership of the Rajapaksas' SLPP, which is still the largest single bloc in parliament, and his hardline stance against protesters has gone down well with MPs who have been at the receiving end of mob violence.

The SLPP has more than 100 seats and Wickremesinghe would almost certainly be elected if party discipline holds.

The SLPP is fractured so that party dissident and former media minister Dullas Alahapperuma, 63, is a serious challenger.

The main opposition leader Sajith Premadasa, 55, has teamed up with Alahapperuma in a pact that would see him named prime minister if their ticket succeeds.

It is an unlikely pairing. Alahapperuma was a journalist and rights campaigner in the late 1980s, when Premadasa's late father Ranasinghe ruled the country with an iron fist.

A distant third candidate is leftist leader Anura Dissanayake, whose coalition has just three parliamentary seats.

Despite their differences, Sri Lanka's political parties are united in their support for ongoing talks with the International Monetary Fund, with Wickremesinghe saying a bailout is urgently needed.

Sri Lanka declared itself bankrupt in mid-April when the government defaulted on its $51 billion foreign debt.

But the political crisis has interrupted the negotiations, and the IMF said last week that it hoped the unrest would be resolved soon so they could resume.

No political party in the current parliament has a clear majority.

Even if the country could afford to hold a fresh election, Tamil legislator Dharmalingam Sithadthan pointed out that a strong mandate was not always a guarantee of stability or success.

"We had Gotabaya with a record 6.9 million votes and what did he do?" Sithadthan told AFP. "He was a total failure."