What Nawaz Sharif's win means for Pakistan's neighbours
Nawaz Sharif's astonishing victory at the polls is certain to be welcomed by neighbours and other countries in the region hoping for a more stable Pakistan - but much will depend on how effectively he is able to work with the Pakistani army, writes Ahmed Rashid.
Mr Sharif was twice prime minister in the 1990s and both times he made genuine efforts to make peace with India but was thwarted at every step by an aggressive and uncompromising army which eventually launched the war in Kargil in disputed Kashmir in 1999 that led to a military debacle and also a coup against his government.
Mr Sharif clearly understands that Pakistan's traditional allies in the West, immersed in a global recession, are unlikely to offer Islamabad much in the way of a bailout. However, India can play a major role in reviving Pakistan's bankrupt economy as a potential investor.
This time around, the army - faced with an apparent collapse of the state - is also more amenable to the idea of improving relations with India, but army chief Gen Pervez Kayani still baulks at the idea of Indian factories and investment taking root in Pakistan.
However, Pakistan has little choice and Sharif even less so, as avenues to rescue the economy close one by one. Gen Kayani is also due to step down at the end of the year and a new army chief may well be able to put the years of acrimony between Mr Sharif and the army to an end.
President Asif Ali Zardari virtually surrendered foreign policy decision-making to the army in order to stay on its right side.
Mr Sharif is unlikely to do that and will instead need to co-operate with the army in order to have an effective policy towards brokering peace in Afghanistan between the Americans, President Hamid Karzai and the Taliban leaders, who are all based in Pakistan.
The peaceful withdrawal of US troops next year from Afghanistan through the Pakistani port city of Karachi, the end of the Afghan war and the survival of the regime in Kabul will all largely depend on how seriously Pakistan plays its role in forcing the Taliban to the peace table.
Mr Sharif is keen to do so - simply because he knows he will be unable to tackle Pakistan's internal crisis without peace across the border.
Mr Sharif will face a quandary with Iran as the US puts pressure on Pakistan to abide by UN-mandated sanctions on Iran imposed because of its nuclear programme. Pakistan has just signed an agreement for a critically-needed gas pipeline to be built between the two countries and Mr Sharif will want to continue that programme as Pakistan is severely lacking in gas supplies.
The five central Asian states - Tajikistan, Uzbekistan, Kyrgyzstan, Kazakhstan and Turkmenistan - are already deeply concerned about the US pullout from Afghanistan and the US failure to control the Taliban - for which they also blame the Pakistan military. They will be relieved to see Mr Sharif in power and expect him to broker peace in Afghanistan.
Pakistan's traditional ally, China, which has become increasingly concerned and even privately critical of Pakistan's pandering to extremism and the Taliban, will now seek an ally in Mr Sharif and hope that he will be able to work with the army to end Pakistan's tolerance of extremism.
China is worried because militant Uighur Muslims from the Chinese province of Xinjiang are still receiving training in Pakistan.
The most difficult relationship is likely to be with the Americans. Mr Sharif, like other politicians in this election campaign, has risen on a wave of anti-Americanism.
His brother Shahbaz Sharif, former chief minister of Punjab, had stopped all projects by the US Agency for International Development (USAID) in Punjab province as a mark of anger against Washington's policies and its use of drones.
The army has its own multi-dimensional quarrels with Washington, especially over the use of drone missiles.
However, Mr Sharif knows that he will need US support in order to garner desperately needed aid from the IMF, the World Bank and other global institutions and he cannot do that without US support for a peace process with India and Afghanistan.
Many politicians, including Mr Sharif, have long felt that the army has had a monopoly of power over foreign policy for far too long. Even on election day, it was the military which evicted a New York Times correspondent from the country, an act which the interim government knew nothing about.
Declan Walsh was thrown out for apparently annoying the military back in February with a story about conflict between the CIA and the ISI over the use of drone missiles.
Mr Sharif is also going to need a more friendly and pragmatic foreign policy in his efforts to end Pakistan's long-running domestic insurgencies - the Pakistani Taliban in the tribal areas, the Baloch separatists in Balochistan and the multiple ethnic and sectarian killings in Karachi,
Blaming India or Afghanistan for helping create Pakistan's own domestic instability, as former regimes have done, resolves nothing.
Pakistan stands isolated and disliked in the neighbourhood and rebuilding its reputation is not going to be easy.