Egypt's top court on Thursday cleared ousted strongman Hosni Mubarak's last premier to run for president and ruled illegal the Islamist-led parliament that sought to bar him, deepening political rifts just two days before the key vote.
The decision handed legislative power back to the generals who took power when Mubarak was overthrown in a popular uprising early last year, a military source said.
"The Supreme Constitutional Court has ruled that the political isolation law is unconstitutional," the state MENA news agency said.
The court was examining the legality of the law, passed by parliament in April, which sought to bar senior officials of Mubarak's regime and top members of his now-dissolved National Democratic Party from standing for public office for 10 years.
The law applies to those who served in the 10 years prior to Mubarak's ouster on February 11, 2011 after an 18-day popular uprising.
The top court also ruled that articles in the law governing parliament were illegal.
"The constitutional court ruled unconstitutional some articles of the parliamentary election law related to the direct vote system," MENA reported, referring to the third of seats elected on a first-past-the-post system.
"The constitutional court affirmed in the details of its verdict that the parliamentary elections were not constitutional, and the entire composition of parliament has been illegitimate since its election," the official MENA news agency reported.
Members of the ruling military council were in a meeting and did not immediately issue a statement. But a military source said the court decision gave the military legislative power.
"We don't want it (the power) but according to the court decision and that law, it reverts back to us," the source said.
The ruling military decided on a complex electoral system in which voters cast ballots for party lists which made up two thirds of parliament and also for individual candidates for the remaining seats in the lower house.
The individual candidates were meant to be "independents" but members of political parties were subsequently allowed to run, giving the Muslim Brotherhood's Freedom and Justice Party an advantage.
That decision was challenged in court.
Mahmud al-Khodeiri, a senior lawmaker and former judge who won his seat with support from the Muslim Brotherhood, said by-elections were likely to be held for some of the seats.
"There will be a re-election for some of the seats," he told AFP after the ruling, referring to seats won by candidates belonging to parties.
He said no one authority had the right to dissolve parliament at the moment but elections could take place "after there is a new president."
Outside the court dozens gathered to demand the application of the law, amid heavy security.
"That's it, the revolution is over," one protester shouted, as others chanted against the ruling military.
"I reject Shafiq and Mursi, and if the court lets Shafiq stand or if there is a referendum on Mursi, we will go back to Tahrir," the epicentre of protests that toppled Mubarak, said writer Samara Sultan, 30, before the hearing.
"We want the court to fix the parliament and the only way to do that is to repeat (the election)," she said.
Ahmed Said, a filmmaker from Cairo, said parliament was "full of people who use religion and don't care about Egypt. We need people who can make a new Egypt.
"The elections were a fraud and we need the court to order them to be repeated. Shafiq is like Mubarak and we will never accept him," he said.
Shafiq was initially barred from standing in the election in accordance with the law passed by the Islamist-dominated parliament in April.
But later the same month the electoral commission accepted an appeal from Shafiq against his disqualification and the case was referred to the court.
The court's ruling came just two days before the landmark runoff to choose a successor for Mubarak.
In the first round of voting on May 23-24 - which saw 13 candidates compete for the top job - Mursi won 24.7 percent of the vote, slightly ahead of Shafiq's 23.6 percent.
The race has polarised the nation between those who fear a return to the old regime under Shafiq's leadership, and those wanting to keep religion out of politics and who accuse the Muslim Brotherhood of monopolising power since last year's revolt.
The next president will inherit a struggling economy, deteriorating security and the challenge of uniting a nation divided by the uprising and its sometimes deadly aftermath, but his powers are yet to be defined by a new constitution.