It was Sunday night, the opening of the United Nations' 40th anniversary week. A half block from the Museum of Modern Art, where Mayor Edward I. Koch was hosting a glittering reception for foreign dignitaries, two motorcades met at the intersection.

As gridlock threatened, the two long lines of limousines, one bearing President Mohammed Zia ul-Haq of Pakistan, the other Prime Minister Shimon Peres of Israel, played Alphonse and Gaston.

"It was 'After you.' 'No, after YOU,' " recalled Gillian Sorensen, the city's liaison with the United Nations. "Finally, they came in one right after the other." Peres was first.

The United Nations' unprecedented birthday bash has brought 80 world leaders, including kings, presidents, prime ministers, sultans, a chancellor and hundreds of lesser officials to midtown Manhattan this week, causing equally unprecedented headaches involving logistics, security and protocol in this normally blase city.

"There are more heads of government here than have ever gathered in one city at one time," Sorensen said.

More than 1,500 police officers patrolled a 150-block area around the U.N.'s glass-and-steel headquarters on the East River today as President Reagan arrived for a three-day visit. Secret Service details swarmed the streets, and private security guards spoke the tongues of many nations into their radios.

"Security is awesome," assistant director Robert Snow said. "We've never had this many people to protect at the same time on the same date." Three-quarters of the Secret Service's 2,000 agents, according to one report, have been detailed here to protect 63 heads of government.

"If I were a terrorist, I don't think I'd come here," said Police Commissioner Benjamin Ward, as platoons of helmeted officers on motorcycles blocked off the streets, sharpshooters patrolled the roofs of U.N. buildings and five Coast Guard vessels waited in the river.

All city trash cans as well as 121 mailboxes between 42nd and 53rd streets along First and Second avenues suddenly disappeared last week -- temporarily removed, so they could not be used as bomb receptacles.

With an estimated 10,000 limousines moving through the streets and double-parking three deep, police warned motorists to stay away. "Midtown Motor Madness. UN GRIDLOCK" the Daily News headlined.

New York police had urged delegations to limit the size of their motorcades. But when Indian Prime Minister Rajiv Gandhi arrived from the airport yesterday with an entourage of 32 cars, they knew they had lost the battle.

"The Bible says 'by the size of their motorcades, ye shall know them,' " said Victor Ross, the Department of Transportation spokesman.

Ordinary silver stretch-limos with smoky windows were not enough for some dignitaries. Limousine company owners reported rivalries between delegations over perks, with cellular telephones, circular couches, fancy hors d'oeuvres and even video cassettes, handy during expected traffic jams, in demand.

Few pretend that the official ceremonies Thursday will be newsworthy. The business of diplomacy takes place in private meetings and social occasions. Tonight, 400 limos were expected at the Waldorf-Astoria Hotel where Reagan hosted a reception, Gandhi gave a dinner and the Sports Hall of Fame had a party, all at about the same time.

Smaller countries try to keep pace: On Monday, Zambia, Oman, Lesotho, the Dominican Republic and Sri Lanka competed for distinguished guests. And politics figure in the socializing: Arab diplomats boycotted Koch's reception because of his criticism of the Palestine Liberation Organization.

"They missed a good party," Koch said.

At the U.N. Plaza, where 20 delegations from Nicaragua to Lebanon have reserved whole floors looking over the East River, room service is hopping at 3 a.m., officials report, and phones ring off the hook with international demands.

"We've had all kinds of requests, from a Russian Orthodox Bible to banana ice cream," said concierge Rita Di Renzo, who said her most difficult challenge was finding someone to give a facial to a male diplomat at 10 p.m.

Elevator protocol is rigid. Only heads of government and foreign ministers have elevators cleared and held for them. "Ordinary ministers wait their turn," manager Ivan Chadima said.

Chadima, highly security-conscious, takes a dim view of the jogging habits of Nicaraguan President Daniel Ortega, who has taken to running in Central Park this week, accompanied by guards armed with Uzi submachine guns. "The publicity is hard for politicians to resist," he said.

At the Waldorf, where 25 heads of government are housed, including Reagan, Gandhi, Zia and Prime Minister Yasuhiro Nakasone of Japan, the hotel provides peach-colored monogrammed towels for Nancy Reagan and comic books and Snickers candy bars for the children of a Saudi prince.

Hotel managers report that the diplomats are for the most part too busy to request theater tickets, although Sorensen's office did manage to get a box at the Metropolitan Opera for the Polish leader, Gen. Wojciech Jaruzelski, to see "Tosca" during his visit two weeks ago when he spoke to the General Assembly.

Sorensen's office also arranged for Belgian royalty to visit a Harlem shelter for homeless men. A Danish delegation toured the Riker's Island prison.

The foreign minister of Malta put in a request to meet former vice-presidential candidate Geraldine A. Ferraro, but she was out of town.

A few shopping expeditions have been squeezed in. A Middle Eastern delegate was reported to have bought $4,000 worth of toys, including a $3,000 toy Mercedes. Designer Mary McFadden sold two gowns worth more than $1,000 each to the wife of Egyptian President Hosni Mubarak.

One leader, President Mobutu Sese Seko of Zaire, made a big splash by renting two Amtrak club cars to take a delegation of 50 to Washington and back, providing champagne and lavish meals, at a reported price tag of $9,800.

At the intersection of 49th Street and Third Avenue this morning, Tony Ciacco of Prime Time Limo shouted out the window of his Cadillac, blocked by police barricades.

"I gotta get up there," he said. "I got a vice president of South Korea waiting at the Waldorf."

Police Officer Keith Cheply was unimpressed. "Makes no difference to me," he said.

As Ciacco pulled away, Cheply reached into his jacket for a packet of candy bars. "I came prepared." he said. "I knew this was going to be a rough day."