RIO DE JANEIRO — Allyson Felix and LaShawn Merritt are two savvy track stars who’ve been around long enough to know that not everything always goes to plan.

Over the final, topsy turvy 30 minutes of running at the Olympic track on Saturday night, both Americans came away with prizes they’d been wishing for all along.

Those prizes were both gold, and the United States exited the final night of action at Olympic Stadium with 31 medals – the most it has taken in a non-boycotted Olympics since 1956, when the world and track were both very different places.

The win for the U.S. women’s 4×400-meter relay team was clear-cut. Felix, who ran the anchor leg, now has six gold medals, improving her all-time record for women in Olympic track and field.

Earlier, Mo Farah of Britain became the first runner in 40 years to win back-to-back long-distance doubles at the Olympics, taking gold in the men’s 5,000 meters.

Farah already had won the 10,000 meters in Rio and earned gold medals in the same events in London four years ago.

The last man to win both distance races in consecutive Olympics was Finnish great Lasse Viren at the 1972 Munich and 1976 Montreal Games.

“It’s every athlete’s dream but I can’t believe it,” the 33-year-old Farah said. “I just want to go home now and see my beautiful kids and hang my medals around their necks.”

Farah won in 13 minutes, 3.30 seconds. Paul Chelimo of the United States was temporarily disqualified for a lane infringement after finishing second, but he was reinstated a short time later by track and field’s governing body, which said Chelimo did not break any rules and would get the silver medal.

Hagos Gebrhiwet of Ethiopia won the bronze medal.

This has been an adjust-on-the-fly year for Felix – her failure to make the team for the 200 meters, her jaw-dropping silver-medal moment when a diving Jamaican beat her in the 400, and more. But on back-to-back nights, she took relay gold.

This time, she ran a 49.66-second anchor lap. Over the second and third laps, Americans Natasha Hastings and Phyllis Francis held big leads, only to watch them dwindle dramatically as they legged out their final meters. Felix took the green baton about two steps ahead of Jamaica’s Novelene Williams-Mills and pulled away in the final 100 meters for a total time of 3 minutes, 19.06 seconds and a 1.28-second win.

Yes, the United States may have relay problems – see, the men’s 4×100, which flamed out again the night before – but this event certainly isn’t one of them. The U.S. women have won gold six straight times.

The look on Felix’s face as she crossed the line and waved the green baton said what words could not: Thank goodness, it’s finally over.

“The toughest, without a doubt,” Felix said of her year. “This year, you make plans and want everything to go according to schedule. Nothing went according to schedule.”

Merritt looked equally relieved. He was caught in the tail wind of South African Wayde van Nierkerk’s world-record 400 run earlier in the week and settled for bronze. Then, Merritt was a bit player in the Usain Bolt going-away party, placing fifth the 200.

Arman Hall, Tony McQuay, and Gil Roberts set up Merritt for the win. As Felix had done minutes earlier, Merritt took a narrow lead and opened it way up. The veteran already had two gold medals from the 2008 Beijing Olympics and eight world championship gold medals stretching as far back as 2005.

Four years after the Bahamas beat the U.S. team, the Americans finished with a time of 2 minutes, 57.30 seconds. The Jamaicans brought the baton home .86 seconds later, and the Bahamas took bronze in 2:58.49.

It wasn’t all perfect for the red, white and blue, however.

High jumpers Chaunte Lowe, a mother of three who finally felt ready for her Olympic moment, and Vashti Cunningham, daughter of former NFL quarterback Randall, finished fourth and 13th in a contest taken by Ruth Beitia of Spain.

Caster Semenya of South Africa won the women’s 800, fighting from behind to overtake Francine Niyonsaba of Burundi on the final bend before cruising home.

Semenya won in a South African national record 1:55.28, while Niyonsaba was 1.21 seconds behind. Margaret Wambui of Kenya took bronze in 1:56.89.

Semenya has been the unwilling face of one of the most complicated and sensitive debates for the sport. Do women who have much higher levels of natural testosterone than normal have an advantage over other women in athletics, and if so, is it unfair?

Semenya’s breakthrough title at the world championships seven years ago pushed the IAAF to introduce rules limiting testosterone in female athletes. Under a legal challenge, the IAAF was forced to drop the testosterone-limiting rules last year.