TORONTO -- Welcomed with a hearty roar, Kanye West ran through a career-spanning collection of his hits to close out a Pan Am Games headlined by a harder, better, faster, stronger Canada -- until a faulty microphone didn't let the rapper finish.

Nearly 40,000 spectators packed Toronto's Rogers Centre to raise a toast to Canada's athletes -- and their record-setting 217-medal haul -- at the Pan Am Games closing ceremony, and West's surprisingly long performance would have put an exclamation point on the evening, already marked by a flamboyant fireworks display lighting up the CN Tower like a rainbow-leafed palm tree, if not for the sound gaffe.

Kanye West

Kanye West performs during the closing ceremony of the 2015 Pan Am Games in Toronto on Sunday, July 26, 2015. (Mark Blinch/THE CANADIAN PRESS)

West tried in vain for a period of time to overcome the error, before tossing his mic high into the air and letting it smash while he stalked offstage. The crowd chanted "Kanye!" in his absence.

Although the closing ceremony was otherwise perhaps less knowingly grand than the opening, it was still an expansive production best-explained by some of its staggering statistics: 510 volunteers in the cast, 500-plus costumes from 3,000-plus yards of fabric and roughly 3,235 athletes, who flooded the venue's floor as one fully integrated contingent.

The stage was multiple stages really, connected by a network of runways, and it was shadowed by columns as high as 20 metres intended to depict Toronto's ever-rising skyline.

Spiritually, the show was about Toronto too. The Ontario capital's diversity inspired the ceremony's centrepiece, where dozens of dancers and performers filled the light-bathed stage with globally derived movements, each meant to pay tribute to a locally prevalent population.

Pan Am closing ceremony

The Canadian delegation watches a performance during the closing ceremony of the Pan Am Games Sunday, July 26, 2015, in Toronto. (Julio Cortez/AP Photo)

Brazilian capoeira gurus acrobatically spun through the air, Chinese tai chi specialists wove together in careful synchronicity to form a dragon, and Argentine tangoers sashayed seductively. Other eye-catching dances were intended to nod toward the city's First Nations, Caribbean, Indian, Filipino and Irish populations, with energetic modern dancers strutting out to the sound of a swiftly strummed acoustic guitar for the section's finale -- an apparent general Canadian dedication that ended with the indoor emergence of a CN Tower setpiece, which was swiftly showered in pyro.

Though imbued with a school's-out party spirit, the two-hour show still featured no less mandatory housekeeping than the Cirque du Soleil-curated opening ceremony (which drew a sold-out crowd of roughly 45,000 to the same venue).

There were three speeches, from Pan American Sports Organization vice-president Ivar Sisniega and TO2015 CEO Saad Rafi, who declared: "At these Games, hope won out, optimism prevailed (and) community spirit carried the day."

There was the handover ceremony -- the symbolic transfer of Pan Am responsibility to 2019 host city Lima -- which was followed by a creative segment during which a little Peruvian boy journeyed around the world with his alpaca (or this was how the plot was explained in the show's companion literature). The segment concluded with more than 50 performers converging on the stage to dance to "Carnaval," by Peruvian band La Sarita.

The entrance of the flags and parade of nations, at least, were accomplished in a far more raucously casual fashion than at the opening ceremony, with gold medal-winning basketball star Kia Nurse toting the flag for Canada.

Still, the crowd kicked up the most energy for the close.

First, Ryder -- a six-time Juno winner -- wrapped her weight-lifting voice around a three-song set headlined by "Stompa," before Miami pop-rapper Pitbull strutted onstage for a trio of his own club-thumping hits, the third of which -- his smash Ne-Yo collaboration "Give Me Everything" -- succeeded in finally coaxing some movement from the crowd.

Pitbull

Pitbull performs during the closing ceremony of the 2015 Pan Am Games in Toronto on Sunday, July 26, 2015. (Darren Calabrese/THE CANADIAN PRESS)

And then it was consistently bound-to-controversy West, whose promised presence at the closing ceremony sparked a fleeting outcry from certain corners.

He was greeted with deafening warmth Sunday, stepping onstage completely alone to perform his undeniable 2008 hit "Stronger." He then ran through his feisty "Power," which flowed into his even-feistier "Black Skinhead," and then yet another defiant classic, "Can't Tell Me Nothing."

"Everyone in the arena, clap your hands like this," he instructed. "We celebrating y'all tonight!"

With the stadium lights brightly turned up, frankly detracting slightly from the energy, he continued with a set that included "Touch the Sky," "The Good Life," and "All of the Lights."

It's not clear how he would have closed out the show if not for the error -- but a fitting choice might have been "Gold Digger," which snuck into the middle of his set. The chart-topping tune might have resonated with the plundering Canadians in the crowd, anyway, who wound up with a second-best 78 gold medals at these Games.