Few political turnarounds can match the last month in Senegal. Just over two weeks ago, Bassirou Diomaye Faye was a little-known opposition leader languishing in jail, detained without trial on charges including inciting insurrection, who had never held elected office. One week ago, he defeated the governing party's candidate, Amadou Ba, in the country's presidential election, winning 54% in the first round. On Tuesday, the 44-year-old has been sworn in as the fifth president of Senegal, becoming Africa's youngest elected head of state. In a region where a large majority of the population are under 30, his victory offers hope to those young people frustrated by a lack of economic opportunities, with old elites seemingly clinging to power.
Mr Faye's spectacular rise is a powerful reminder that elections still represent the best way to remove a failing government for many citizens in Africa.
Not only has his win removed an unpopular government from office, it has strengthened the country's democratic institutions and reinvigorated popular confidence in democracy at a time when coups in other West African states have done the opposite.
The story of Mr Faye's victory will also inspire other leaders across the continent, who have experienced years of rising repression, intimidation and censorship.
According to long-time Ugandan opposition leader Kizza Besigye, who has recently worked with his younger counterpart Bobi Wine to campaign for democracy in his country, "Senegal's extraordinary electoral process has demonstrated, again, that with a well-mobilised, resilient and well-led population, it's possible to non-violently achieve the desired democratic transition in Africa".
The inspirational impact of Mr Faye's success will be magnified by the fact that it did not come easily.
Ahead of the election the government of President Macky Sall took a number of undemocratic steps in what was seen as an attempt to try and hold on to power against a backdrop of growing popular discontent.
This included the sustained persecution of opposition leaders and critical voices and a last-ditch attempt to delay the elections in a desperate bid to avoid defeat, which led some commentators to ask whether we were seeing the death of Senegalese democracy.
Many of these measures were aimed at undermining the momentum behind the popular opposition party, the African Patriots of Senegal for Work, Ethics and Fraternity (Pastef).
This included detaining the party's popular leader Ousmane Sonko and Mr Faye, who was Pastef's secretary-general. There was also widespread intimidation of Pastef supporters.
The jailing of Mr Sonko - for allegedly acting immorally towards an individual younger than 21 after allegations by a massage therapist - along with a number of inflammatory moves, sparked some of the biggest protests Senegal has seen in recent years. In turn, a heavy-handed response from the security forces led to numerous deaths.
Mr Sonko described the charges as trumped-up and aimed at barring him from running for president.
Pastef itself was dissolved by the authorities last year after it was accused of stoking violence in the country - but its leadership continued operating.
It took incredible bravery and hard work from opposition leaders, civil society groups, journalists and those working in some of the country's democratic institutions to ensure that this bleak situation ended in an election that Mr Faye was in a position to win.
It was the members of the Constitutional Council, Senegal's top court, that ensured the election would go ahead as scheduled when they stood up to the president and ruled that his attempt to change the date was unlawful.
The Pastef leadership also played an important role, standing firm in the face of great intimidation.
Despite his firebrand reputation, Mr Sonko also proved to be willing to be flexible and put his personal presidential ambitions to one side to give his colleague the greatest chance of success.
Indeed, without this Mr Faye would not have even been on the ballot.
Mr Sonko expected to be barred from the polls due to his convictions, and his application to be a candidate was subsequently rejected by the Constitutional Council on the grounds that it "was incomplete". Despite efforts to get him back on the ballot, Pastef's leaders came to the conclusion that it was unlikely that he would be allowed to run.
This realisation made it clear that choosing Mr Faye, who was never actually put on trial, was a safer option - even though it meant Mr Sonko, the party's figurehead, taking a backseat.
Civil society groups and journalists also played an important role, continuing to report on government repression and human rights abuses, despite being attacked, detained and tear gassed.
Through their work, they ensured that Senegalese citizens and the rest of the world knew what was happening in their country, increasing the pressure on President Sall to back down.
In the end, these efforts, and the weight of Senegal's democratic traditions, ultimately led Mr Sall to release both Mr Faye and Mr Sonko from jail - albeit as part of a wider amnesty deal that critics argue is really designed to confer immunity on government leaders for the abuses they committed during the period of political turmoil.
Mr Faye's victory could not have come at a better time for opposition politicians across the continent.
On the same weekend as the election, prominent opposition figures from countries such as Angola, Uganda and Zimbabwe met in Cape Town to discuss "the rising tide of authoritarianism, military dictatorships and hollowed democracies where elections are abused to preserve power".
Amid growing frustration at the increasingly violent strategies being used to repress critical voices, news of a democratic transfer in Senegal was roundly celebrated, lifting spirits and reaffirming the importance of non-violent strategies of resistance.
As Dr Besigye put it, events in Senegal were an important reminder that democratic transitions benefit a whole country, while coups "only recreate a new form of autocratic leadership".
This does not mean that the Senegalese experience will be easy to replicate in other countries, however.
With a history of more liberal and competitive politics, including democratic transfers of power and a military that has avoided interfering in politics, it is still feasible to secure power via the ballot box in Senegal.
In states such as Uganda and Zimbabwe, this is much harder because electoral commissions are less independent, the judiciary is more compromised, and the security forces are even more repressive.
Challenges lie ahead
The legacy of Mr Faye's unlikely rise to power will also depend on what Pastef leaders do from now on.
It is easy to forget that when President Sall came to power in 2012 his victory was also heralded as a democratic breakthrough.
But by deviating from the principles and promises that led people to support him, the outgoing president ensured that he will now be remembered as another leader who was corrupted by power.
To avoid this fate, Mr Faye and Mr Sonko need to focus on rebuilding and reuniting their country.
This will only happen if they avoid becoming distracted by the personal benefits of being in power, and destabilising the government by competing between themselves for overall control.
The most effective thing that opposition parties can do to boost democracy is to govern inclusively and to demonstrate that respecting political rights and civil liberties is the best way to ensure economic development and political stability.