Until a few years ago, Pakistan was one of the most dangerous countries on earth. The tribal areas in the north were infested by the Taleban, whose bases stretched to within 100 miles of the capital, Islamabad. Western intelligence agencies feared that the Taleban could seize one of the country’s nuclear installations, then hold the world to ransom. Large parts of the country elsewhere were lawless or terrorised by armed groups.

It would be foolish to claim that Pakistan’s security problems are over. But something extraordinary and unexpected has certainly happened. Since it fails to fit the established narrative of Pakistan as a dangerous nation, it’s gone unacknowledged in the West.

Violence has not just dropped a bit. It is down by three quarters in the last two years. The country is safer than at any point since George W. Bush launched his war on terror 15 years ago.

The change can be dated to a special cabinet meeting called by prime minister Nawaz Sharif in Karachi in September 2013. At this meeting Sharif called an end to Pakistan’s culture of violence. Parts of Karachi, a teeming city of more than 20 million on the Arabian Sea, had been a war zone for decades. All the main political parties employed paramilitary wings and some formed alliances with terror groups including al-Qaeda.

The Taleban had long treasured a secure basis in Karachi, as had religious terror groups. That was a conventional crime industry specialising in kidnap, drug smuggling and extortion (every business had to pay protection money to gangs).

Pakistan’s politicians tolerated this. Pervez Musharraf, the army chief and president, was often accused of allowing the armed wing of Karachi’s largest political party, MQM, to operate with complete impunity.

This policy continued under Musharraf’s civilian successor, Asif Zardari, whose Pakistan’s People’s Party governed Karachi in coalition with MQM from 2008 to 2012. Five years ago we walked around gangster-infested Liyari town in Karachi’s port area with the local mafia don, Uzair Baloch. Baloch (now in jail) told us he could speak to Zardari whenever he wanted. The violence just rose and rose, until Zardari’s replacement Nawaz Sharif ordered his cabinet to Karachi and gave the state’s paramilitary arm, the Rangers, unlimited powers. This was the moment when political tolerance of violence ended.

We interviewed Major-General Bilal Akbar, director-general of Sindh Rangers for the past two-and-a-half years, at his HQ in the south of the city; he has since transferred to be the Pakistani army’s chief of general staff. After asking us to pass on his regards to Nick Carter, head of the British army (with whom he used to play bridge every Friday night when they were both stationed in Kabul), he explained the security situation.

In 2013 there were 2,789 killings in Karachi. In the first 11 months of 2016 there were 592. In 2013 there were 51 terrorist bomb blasts. Up to late November this year, there were two.

Three years ago, Karachi suffered from an orgy of kidnapping for ransom. There were 78 cases in 2013, rising to 110 the following year. This year, there have been 19.

Some 533 extortion cases were reported in 2013; in 2016, only 133. Sectarian killing is sharply down: while 38 members of the Shia minority (who are brutally targeted in Pakistan) were killed in 2013, that figure was down by two thirds in 2016.

Major-General Bilal told us: ‘We have apprehended 919 target killers from the militant wings of political parties since September 2013. They confessed to over 7,300 killings. The daily homicide rate in the city is less than two now. It used to be ten or 15, and during ethnic clashes we could lose 100 lives a day.’

Just three years ago, according to the Numbeo international crime index, Karachi was the sixth most dangerous city in the world. Today it stands at number 31 — and falling.

Six months after he ordered the Rangers into Karachi, Nawaz Sharif took an even more momentous decision. The prime minister, whose initial instinct had been to negotiate with the Taleban and oppose the use of force, yielded to advice from his generals. He sent the army into North Waziristan, the Taleban stronghold on the Afghan border.

North Waziristan had not just provided a base for the Taleban leadership. It was a centre for the manufacture of explosives, suicide vests and military equipment, and for training camps, as well as drawing in foreign fighters from al-Qaeda. It was the epicentre of terrorism in Pakistan, which is why this intractable and remote area had been left alone by the army for so long.

In June 2014, General Raheel Sharif (now a national hero, and no relation of prime minister Sharif) took charge of a massive military offensive, Zarb-e-Azb. Taleban groups responded with a series of atrocities of which the most grotesque was the attack on the Army Public School in Peshawar, in which a reported 140 children were killed.

That stimulated the National Action Plan in January 2015, hailed by prime minister Sharif as the defining moment in the fight against terrorism. It established special military courts and outlawed terror groups which had previously been given latitude by the state.

At the same time, the army stepped up its operations. According to official figures, it has killed about 3,500 Taleban fighters, destroyed 992 hideouts and cleared 3,600 square kilometres of territory. Nearly 500 soldiers have died.

The Taleban has been gravely weakened. According to Bakhtiar Mohamed, director of the National Counter Terrorism Authority. ‘The army has gone very deeply into every nook and corner of the tribal areas. There is no possibility of any revival of extremism.’

Some of the methods have been brutal. There is very little testimony from the tribal areas, but one expert says Miranshah, capital of North Waziristan, has been turned into a ‘car park’. The MQM complains that some 150 of its activists have been ‘disappeared’ (a complaint which takes a bit of cheek, considering MQM’s own ruthless methods).

The Human Rights Commission of Pakistan protests that more than 430 people have been executed since the moratorium on the death penalty (which was imposed by Zardari in 2008) was ended in 2014. The Human Rights Commission is also alarmed about the new military courts.

This is understandable — yet what else should the state have done? Witnesses simply refuse to come forward in the civil system. The same applies to lawyers and judges.

Take the case of Malik Ishaq, founder of the terror group Lashkar-e-Jhangvi and responsible for thousands of murders. Mindful of their safety, more then 70 judges and lawyers refused either to hear or to prosecute the case. Finally Ishaq died, say the police, in an ‘encounter killing’.

Then there is Omar Sheikh, the alleged murderer of Wall Street Journal reporter Daniel Pearl in 2001. Now, 15 years on, the Pearl case has still not been resolved, and Shaikh rots in jail in Hyderabad, 100 miles up the Indus from Karachi.

Not all of Pakistan has benefited from the new security environment, as the south-western province of Balochistan shows. In this mountainous region riven by insurgency and sectarian hatred, terror groups have continued to operate freely. Lashkar-e-Jhangvi, which some say has support from Saudi citizens, has carried out unspeakable attacks on the Hazara Shia minority. The leadership of the Haqqani network — part of the so-called Afghan Taleban — has migrated from the tribal areas to Quetta, the provincial capital. Isis is on the rise.

There have been three major incidents this year, including a terrible attack on lawyers in Quetta. Many of the young lawyers of the city had assembled at a hospital to mourn a murdered colleague, when a suicide bomber struck.

The Pakistan state has long been accused of tolerating and even abetting groups like Lashkar-e-Jhangvi because they serve its strategic interests. Some local people in Balochistan believe that LEJ is little more than an enforcement arm of the state, licensed to carry out brutality which cannot be meted out legally. But even here there are hopeful signs. Sartaj Aziz, adviser to Narwaz Sharif, told us that LEJ is the next target.

There are other cheering developments. The Sindh Assembly has just passed a bill to prevent forced conversions of Pakistan’s remaining Hindus. But there is a long way to go, and the progress could easily be reversed. There is the possibility that the Taleban has simply fragmented, making it more unpredictable, dangerous and erratic. Some of the successes are merely tactical: structural reform to the police and the judiciary are essential to enforce the other measures.

Meanwhile, Britain continues to harbour the MQM leader Altaf Hussain in lodgings in west London, to the fury and bafflement of Karachi police. He and his organisation both deny claims of terrorism and extremism, declaring that they ‘always act within peacful political structures’. But one high-ranking law enforcement official told us: ‘I calculate there have been 7,540 violent days in Karachi in the last 30 years. On 99 per cent of those days that violence has been incited by Altaf Hussain. I calculate he has been responsible for 35,000 deaths. Just sitting in London. Imagine the reverse scenario — if Gerry Adams had been masterminding IRA terror from Karachi.’