As he prepares to move into Indonesia's presidential palace, one of the first and most difficult jobs facing Abdurrahman Wahid will be to fashion a cabinet and a government acceptable to the disparate political forces that brought him to power.

Wahid, known popularly as "Gus Dur," became Indonesia's president by forging an odd political alliance made up of Islamic-based parties, including his own National Awakening Party; other pro-reform groups; and the Golkar party--the ruling machine associated with former presidents Suharto and B.J. Habibie. As each of the factions that supported him looks for payback, the tug-of-war for coveted positions will begin.

Wahid, a moderate Muslim, will have to listen to the demands of his Islamic supporters, who feel that for too long their aspirations have been suppressed by secular governments determined to keep religion and politics apart in the world's largest Muslim country. The ministries of education and religious affairs are likely to be on their wish list, and Wahid will have to find some way to deal with the many supporters of opposition leader Megawati Sukarnoputri, who feel their candidate was robbed of the presidency through back room deal-making.

It might seem a lot to cope with for a virtually blind Muslim leader who has never articulated a coherent philosophy for governing and who professed recently that he was not interested in the job. "In the end, maybe it will go to me," Wahid mused in an interview just after Megawati's party won parliamentary elections in June, "and I don't like it."

But his political acumen is not to be found in his sometimes erratic, sometimes self-deprecating public comments, often issued in the form of one-liners. Instead, it was visible today in the deft way he outmaneuvered rivals who underestimated him and walked away with Indonesia's top job.

He emerged on top through a combination of good luck, good timing and shrewd calculation. But he also benefited from the political ineptitude of Megawati and her inexperienced, if well-meaning, aides. They came within a whisker of victory, then saw it suddenly and dramatically slip away.

"It's remarkable," said a Western diplomat, shaking his head at how Megawati and her top advisers bungled their best chance for power. "It was theirs to lose--and they lost it."

Megawati became the candidate to beat in June after her Indonesian Democratic Party of Struggle, won the country's freest parliamentary elections in decades, capturing 34 percent of the vote, some 12 percentage points ahead of the second-place party, Golkar. But her party was still short of a majority, and her share was further whittled down once 238 appointive members were added to the 700-member People's Consultative Assembly, which elects the president. She needed to make deals with other factions to win the election, and that meant promises of a share in power--from the vice presidency to seats in the cabinet.

Megawati found such political horse-trading distasteful. After the June elections she adopted what even her allies considered an aloof, almost regal air. She declined to hold regular meetings with natural allies, such as opposition leader Amien Rais, who later became head of the assembly.

In one telling interview, given in July to Tajuk, an Indonesian newsmagazine, she spelled out her philosophy. She rejected the idea of leading a multiparty coalition government, intending instead, she said, to ask for support from all political parties that are "pro-reform, pro-democracy and anti-status quo."

It was a noble idea, perhaps, but one that would eventually prove her undoing. While she was relying on strong popular backing to carry her to the presidency, her opponents, and even onetime allies, were busy making deals to secure a 50 percent-plus-one vote in the assembly. And the one who outmaneuvered the field was Wahid, who had promised to back Megawati but then switched when he saw a chance to boost his own candidacy.

Jusuf Wanandi, who heads the Washington-based Center for Strategic and International Studies, said Megawati largely wasted the months between the June elections and today's assembly vote. "She had to take the lead to create and build a coalition, but she didn't do anything," he said. "She lost it more than Gus Dur achieved it. She had it in front of her. If she had just taken the lead in trying to bring Amien Rais and Gus Dur with her, she would be president today."

As late as Monday, Rais appeared to be begging for an overture from Megawati. In a published interview, he said: "If Megawati reached out from the beginning, she would be far ahead by now. . . . They still have ample time; the solution is they can reach out."

Megawati's aides insisted that she tried to reach out, but they complained bitterly that other party leaders made commitments they were unable or unwilling to keep. "Apparently, political commitments are not irrevocable," said Laksamana Sukardi, one of Megawati's chief advisers. "We had some deals, but until they vote, you don't really know."

CAPTION: Megawati Sukarnoputri backers give vent to short-lived jubilation after election rival B.J. Habibie quit the race.