NEW YORK (AP) _ Shalane Flanagan thought about the seven years building to this race, possibly her last. She thought about the running star striding next to her. She thought about her family. She thought about Meb.

With one hellacious holler at the finish, it all poured out.

Flanagan dethroned Mary Keitany on Sunday and became the first American woman to win the New York City Marathon since 1977, potentially ending her decorated career with her first major marathon victory.

Flanagan's breakthrough came in the last career race for American great Meb Keflezighi. The 2009 New York winner collapsed at the finish line, his 42-year-old body pushed to its limit in his 26th marathon. Keflezighi finished 11th, about five minutes behind 24-year-old winner Geoffrey Kamworor of Kenya.

This may have been Flanagan's final race, too, although the four-time Olympian wasn't ready to commit. But she likes the idea of her and Keflezighi going out together.

"I was thinking of Meb, and I was thinking of how I wanted to make him proud,'' Flanagan said.

Her win came five days after the bike path terror attack in lower Manhattan killed eight and raised questions about security for Sunday. That hit home for Flanagan, a Massachusetts native who completed the 2013 Boston Marathon shortly before a bomb went off at the finish line, killing three and wounding more than 260 others.

"It's been a tough week for New Yorkers, and a tough week for our nation,'' Flanagan said. "I thought of, `What a better gift than to make Americans smile today?'''

Flanagan ended a dominant stretch in New York by Keitany, a Kenyan runner who had won here three straight years. Flanagan stalked Keitany most of the way, hovering behind her during an unusually slow first 20 miles by the lead women.

Flanagan, Keitany and third-place finisher Mamitu Daska of Ethiopia broke from the pack in the 21st mile, and with about three miles left, Flanagan hit the jets. She finished in 2 hours, 26 minutes, 53 seconds, about a minute faster than Keitany.

Flanagan cried and yelled as she approached the finish line without another runner in sight.

"It's indescribable,'' the 36-year-old said. "It's a moment I'm trying to soak up and savor.''

The last American woman to win New York was Miki Gorman, who took consecutive titles in 1976-77.

"Way too long,'' Flanagan said.

Flanagan knows about long waits. She finished second here in her debut marathon in 2010 but hadn't run New York since. It was also her first marathon since finishing sixth at the Rio Games. She fractured her lower back last winter and missed the Boston Marathon in the spring. She was "heartbroken'' to miss her hometown race, but the injury set her up to take aim at Keitany in New York.

Flanagan had called Keitany "the alpha racer'' and said she was ready to "suffer dearly'' while keeping pace with the unpredictable Kenyan. As she went stride-for-stride with Keitany in the middle miles, Flanagan wasn't sure what she'd have for the final stretch.

"There's always creeping doubts of whether I was going to have enough to beat the best in the world,'' she said.

Keitany never found her top gear Sunday, though, and finished with her slowest New York time since a third-place result in 2010. Keitany said she had a problem of a personal nature Saturday afternoon that affected her Sunday, but she would not disclose the issue.

Flanagan held a stern look on the course until getting emotional near the finish in Central Park. On the podium, she put her hands over face and began to cry again when she was announced the winner. She turned to hug Keitany before accepting her medal.

"This is the moment I've dreamed of since I was a little girl,'' Flanagan said.

She's determined to pass on that dream, too. She and husband Steven Edwards — a former track and field star — have been fostering two teenage daughters who are also aspiring runners. Flanagan's parents were also marathoners, and her mom, Cheryl Treworgy, once held the world record.

"This means a lot to me, to my family,'' Flanagan said. "And hopefully inspires the next generation of American women to just be patient.''

Kamworor beat countryman Wilson Kipsang by three seconds, winning with a time of 2:10:53. Kamworor separated from the pack late and seemed like he would cruise to his first major marathon victory, but Kipsang sneaked up on him at the very end. Kamworor was surprised to see the 2014 New York winner behind him on a large video board located near the finish line.

"I look at the camera, I saw someone was coming, which was Wilson,'' Kamworor said. "And I had to believe in myself because I was holding out for the finish.''

Kamworor kissed the pavement right after crossing the finish, then turned to embrace Kipsang. Ethiopia's Lelisa Desisa finished third.

A few minutes later, the Central Park crowd gave perhaps its loudest cheers of the day to Keflezighi. He waved and blew kisses to the grandstands through the home stretch, then had to be carried away after crumbling at the finish line.

"It was a beautiful victory lap, you could say,'' Keflezighi said.

The race went off as planned a few days after New York's biggest terror attack since 9/11. Police had promised an unprecedented effort to secure the course, a plan including hundreds of extra uniformed patrol and plainclothes officers, roving teams of counterterrorism commandos armed with heavy weapons, bomb-sniffing dogs and rooftop snipers.

Mayor Bill de Blasio said the city was expecting 2 million fans to line the streets. The only interruption came when firetrucks had to cross the course to address an emergency that organizers said was unrelated to the race. The course was briefly closed around Mile 18 to let the trucks through, but that was well after the elite runners had finished.

Beverly Ramos competed with a Puerto Rican flag headband two months after her home island was ravaged by Hurricane Maria. Like many in Puerto Rico, Ramos was without power after the storm. Still, she decided to continue training near San Juan, seeking out safe places to run amid the wreckage.

"You have to stay strong no matter what,'' Ramos said. "A lot of runners encouraged me to continue and to push.''

The wheelchair events were a Swiss sweep. Manuela Schar finally upended four-time defending champion Tatyana McFadden to win the women's race, while countryman Marcel Hug repeated as the men's champ. Schar had been runner-up the last three years but beat her nemesis McFadden by nearly three minutes. Hug has won New York three times.

Comedian Kevin Hart made his marathon debut at New York. Forget about chasing gold — the actor and fitness nut was mostly worried about avoiding humiliation.

"I don't want to see a bunch of memes of me looking weird out here,'' Hart said shortly before starting.

Photo above via WABC.