Dutch troops who freed hostages from a terrorist-held train here early today rehearsed their lightning attack on an empty train miles away, secretly bugged the train where the hostages were held and monitored the movements of the terrorist inside with heat-sensitive radar.

Using troops, armored vehicles and F-104 Starfighters in the dawn attack, the Dutch forces freed 49 hostages from the train and four others from a schoolhouse, ending a drama that began almost three weeks ago.

Two hostages in the train, a 20-year-old woman and a 40-year-old-man, and six of the nine South Moluccan terrorists who held them were shot dead. Seven other hostages, two Dutch marines and one terrorist were wounded in the 20-minute attack on the train. One hostage was wounded in the attack that freed four teachers at the nearby Bovensmilde schoolhouse.

Despite the days of thorough preparation for a military assault to try to free the hostages, it was not until yesterday that the Dutch government suddenly decided to drop its patient efforts to talk the terrorist into surrender and turned instead to force.

A fear that not much time was left before the strain of captivity severely harmed the hostages, and a growing bitterness among the Dutch public toward the three-week stalemate were key factors in the decision to storm the train and school, Dutch officials said.

While the situation has raised antagonisms among the Dutch and Moluccans communities, it has had one salutary effect: The split-second and successful attack by the Dutch forces has wiped away at least some of the derogatory remarks frequently aimed at the long-haired Dutch troops by their more conservative colleagues in other European armies.

The young South Moluccans terrorists seized the train and the school May 23 to press their demands for Dutch support in trying to gain independence for their homeland, now part of Indonesia but once a Dutch colonial possession.

Originally there were 110 hostages in the school, but the terrorists released 105 schoolchildren and one teacher after a virus struck the children.

The Moluccan leader had demanded that the terrorists be allowed to leave the country, to a destination that was never specified, along with 21 other Moluccans in Dutch jails for various other assaults and a planned kidnapping of Queen Juliana.

The Dutch government had steadfastly refused to meet the demands and had tried for nearly three weeks to get the terrorists to negotiate.

"We felt the situation had gone as far as it could," said chief Justice Ministry psychiatrist Dr. Dick Mulder, who had been talking by field telephone with gunmen on the train and in a nearby toolhouse since the dual siege began.

"There was no end in sight. After three weeks the terrorists had returned to the same hard line they took on the first day. The health of the hostages was in danger. I felt the terrorists were playing poker and were willing to go to the very end," he said, and the government decided "the risks were acceptable."

Furthermore, the mood of the country had grown increasingly impatient and angry over the lingering siege, a political factor that mulder and Justice Minister Dries Van Agt also acknowledged was an ingredient in the government's decision to attack.

Mulder said he had the feeling that he could have gone on talking to the clever 24-year-old terrorist leader Max Papillaya, but that no one could say where the limits of stress were for the passengers.

"Unfortunately, force was the possibility that best guaranteed that the mental condition of the passengers could be kept within tolerable limits," Mulder said.

"It is not better to have mentally healthy terrorists and sick hostages."

The chief psychiatrist at the hospital in Groningen, where the hostages were taken today, said: "Most of them were in reasonable condition, considering the extreme stress they've been through." None of them chose to stay for the 24-hour period in which doctors were hoping to help them through the transition to normal conditions.

In a radio broadcast this morning, Dutch Prime Minister Joop Den Uyl said, "The feelings we have now are mixed. We feel thanks that an end has come to the unbearable torture of the hostages and their relatives.In the end, we saw no other way. But that violence proved necessary . . . is something we feel as a defeat."

Nevertheless, from the start the Dutch were working on contingency plans for a daring and risky last-resort assault that amounted to a switch from psychology to technology.

Within hours of the train and school seizures, specially picked Dutch Royal Marines and civilian and military police began practicing for an assault using another train many miles from here.

The marines had undergone similar training 18 months ago when another band of Moluccan extremists seized a train at nearby Beilen and held it for 12 days. Those terrorists eventually gave up after murdering three of their hostages.

At the latest siege dragged on, and while psychiatrists talked over the field telephones, other commando-style troops are said, to have slipped tiny listening devices onto the train's underside.

Two special radars designed to detect differences in hot and cold surfaces were also secretly set up to follow the movement of the terrorists, who were carrying guns whose metal would show up on such radars.

Television cameras and other commandos with binoculars and night-viewing devices had slipped close to the treeline on the train's east side.

"We had a pretty good idea of where everybody in the train was" when the time for the attack came, Dutch officials claimed.

For all this preparation, however, the attack plan included a large measure of faith in estimating people's reactions.

The attack just before 5 a.m. this morning started with the crackle of machine-gun fire riddling the train's windows from marines concealed in the treeline to the east of the train.

Seconds later, when six F-104 Starfire jets swooped down out of a dawn sky with the train in their sights, the mission was not to bomb or strafe but rather to create noise - sudden, prolonged and intolerable noise meant to stun and "freeze" the nine heavily armed terrorists in the train into a precious minute or two of inaction. Even fireworks were used.

While the jets screamed in low criss-cross patterns just a few feet above the train, igniting the bombing after-burners of their jet engines as they climbed steeply away, a force of 30 Marines raced across about a hundred yards of open fields on the west side and blasted their way into the four-car train.

The Dutch used special explosives because the train's doors were of a different metal from those of the train seized 18 months ago.

Within 20 minutes, the attack on the train was over.

The seven wounded civilians apparently were all women. The terrorists had segregated male and female passengers, and the gunmen were sleeping in the same first-class car as the women. Authorities said that the two hostages who were killed, were shot in the head when they jumped to their feet despite the soldiers' orders to lie flat.

The attack on the school in Bovensmilde, where four gunmen held four teachers, took only 10 minutes.

Armored personnel carriers rushed the school on all four sides, one of them bursting through a wall. Marine commandoes and other troops poured through the breach. They met no resistance.

Den Uyl said today the government "could not have let the hijackers leave the country unpunished. It would have been an invitation to a repetition of blackmail with human lives."