ISLAMABAD, PAKISTAN -- Stunned by terrorist bombings, ethnic riots and violence spilling over its borders from Iran, India and Afghanistan, this strategic country of 100 million has developed a bunker mentality, wondering from where and how the next blow will come.

In Karachi, the giant port that is Pakistan's economic engine, a recent terrorist bomb explosion that killed 72 seemed to trigger a nervous breakdown in the city, as outraged residents went on rampages, attacking policemen because of their ostensible failure to protect people.

In Peshawar, on the troubled frontier with Afghanistan, anger has been building for months as the Afghan war makes itself felt inside Pakistan in the form of bombs and occasional cross-border air attacks and artillery barrages.

In Lahore, capital of Punjab province, the fear also is making itself felt. The bombs have come there too, and there are more than casual whispers asking what the government is going to do about it.

Prime Minister Mohamed Khan Junejo hints that the Lahore blasts may well be the work of India, an apparent attempt to deflect attention from his Afghan policy. Popular belief, however, says they are the work of the Afghan secret police, trying to punish Pakistan and force it to change policy.

And here in the nation's capital, there is a different kind of bunker mentality setting in, one reflecting the sense that things are not going well either at home or abroad.

Pakistani officials fear that a dangerous confrontation is brewing with the United States over Pakistan's nuclear program, and that it risks changing the entire political equation in a region still reeling from the effects of the Iranian revolution and Soviet invasion of Afghanistan.

When Soviet troops first entered Afghanistan 7 1/2 years ago, Pakistan was treated by many as a virtual pariah following the hanging of former prime minister Zulfiqar Ali Bhutto. Its relations with the United States were at a low point because of Islamabad's drive to develop a nuclear capability. Washington had cut off aid to Pakistan and the American Embassy in Islamabad had been sacked by a mob.

But the arrival of more than 100,000 Soviet troops next door to prop up an unpopular Communist regime in Afghanistan changed all of that.

Relations with Washington blossomed as the two nascent allies supported a covert war that funneled hundreds of millions of dollars in weapons and supplies to Afghan guerrillas. A new aid program sent billions in military and development assistance into Islamabad's coffers.

As host to upwards of 3 million refugees and as a new front-line state facing the Soviets, Pakistan's stock soared in the Islamic world and elsewhere.

The decline from those high-riding days to the tensions and fears of today has been precipitous. So far, the domestic and foreign issues have not merged into a single overwhelming crisis, but each in its own way exerts new pressure on the special alliance between Pakistan and the United States, particularly over Afghanistan.

If the gravity of the confrontation with Washington is appreciated by just a few Pakistanis, the impact of the recent bombings in Karachi, Lahore, Rawalpindi and Peshawar is felt by people here in all walks of life, and the government is under pressure to do something about it.

"If the Afghan imbroglio has something to do with {the bomb explosions}, let us try to speedily end it. If the answers lie elsewhere, let us find them and punish the guilty ones without mercy," said a columnist in the Pakistan Times, a newspaper sympathetic to the government.

So far, the unease over the troubles at home and abroad has been contained by the government of President Mohamed Zia ul-Haq and Prime Minister Junejo. Nevertheless, in a country that has experienced as much rule by the military as by civilians, and that is still struggling to see a new civilian government establish itself, it is a dangerous time.

As seen by Pakistani strategists, there is trouble in every direction:

To the west is Iran, still in turmoil, and the Persian Gulf where U.S. warships are raising the stakes of the Iran-Iraq war.

With a volatile population, including many pro-Khomeini Shiites, there are fears that any U.S.-Iranian clash could quickly lead to serious domestic unrest because of Pakistan's links to the United States.

To the northwest is Afghanistan with its 100,000-plus Soviet troops and a guerrilla war that grows in intensity as talks for a possible settlement appear stalled on a new plateau.

As the Afghan conflict has increased in ferocity, Soviet and Kabul forces have brought the warfare to -- and often across -- the Pakistan border.

Numerous Pakistani border villages, many of them havens for Afghan guerrilla forces, have been bombed and shelled, bringing pressure from Islamabad on the United States for a new air surveillance system.

To the east is India, the archfoe with whom Pakistan has fought three wars and now opposes in an arms race that threatens to take on nuclear proportions.

Besides its troubles with its neighbors, Pakistan's most serious foreign policy crisis has emerged with Washington over the true nature of its nuclear program.

If the Zia government cannot persuade the Reagan administration and a skeptical Congress that it had nothing to do with an attempt by a Pakistani citizen to buy from U.S. kirms materials used in making nuclear weapons, a new $4 billion aid project will be placed in serious jeopardy.

Any major change in the aid program, according to Pakistani and U.S. officials, will force a rethinking of Pakistan's role in the war against Soviet troops in Afghanistan and perhaps in its relations with Iran and the Soviet Union.

"So far, they are only thinking tactically to see what steps they need to take to limit the damage," said one knowledgeable Pakistani. "No one, except perhaps Zia and a couple of others, is thinking in strategic terms of what a break with the United States will really mean for Pakistan. We are in a tight spot. We may have gotten out of it a couple of times before, but this time it will be far more difficult, maybe not possible."

U.S. law calls for cutting aid to a country that is found to be illegally buying material in the United States that could be used in developing a nuclear weapon, unless the president says the aid should continue for overwhelming reasons of national interest.

Pakistanis have been caught at least twice before trying to buy such material in the United States and each time the administration, in effect, has looked the other way. This time, important congressmen are especially angry, because some of them, such as Rep. Steven J. Solarz (D.-N.Y.), had just played key roles in pushing through a new aid package based on assurances from Pakistan over its nuclear program.

The Pakistani defense has been to claim that those carrying out the purchase operations were defying strict orders given by Zia and Junejo, but there is a sense among knowledgeable observers that this may not be enough to satisfy Congress.

The State Department reportedly has been pressing again for Pakistan to sign the nuclear non-proliferation treaty and to open its plants to inspection, as well as to give specific assurances that it would not enrich uranium to weapons grade.

"No chance," said one prominent Pakistani to the suggestion of signing the nonproliferation treaty. "The country wouldn't stand for it."

Pakistan's nuclear program is overwhelmingly popular, one of the few issues here that generates support across the political spectrum. The program's director, nuclear scientist A.Q. Khan, is looked upon as a folk hero, even though his zealousness to push ahead with his program is believed to have helped create the tension with Washington.

"It is the one subject on which everyone unites behind the government. There are no voices against the bomb here; everyone wants it," said one seasoned diplomatic observer.

"What can be done?" asked one Pakistani who has had extensive experience in dealing with the nuclear issue.

"The first steps are what you are seeing now, the naming of culprits, the arrests. Maybe, just maybe, if that isn't enough, there could be some kind of a one-time inspection of Kahuta {Pakistan's main plant where the weapons program is believed to be centered}. The Americans have been pushing for it for a long time. {Former U.S. ambassador Dean} Hinton kept pushing for it until the day he left."

If the Pakistanis were to open Kahuta to outside inspection, it would mean a major shift in policy, and the fact that it is being considered among Pakistanis known to be close to Zia is a reflection of how seriously the current confrontation with the United States is being viewed.

According to long-time observers here, the margins for maneuvering out of the situation are very narrow.

"If Congress does cut the aid program, it won't take much to spark a sharp reaction," said one western expert. "It will be an emotional reaction here, not a rational one."