LAS VEGAS (AP) — The San Francisco 49ers had Patrick Mahomes and the Kansas City Chiefs on the ropes four years ago when the teams last met in the Super Bowl.

The Niners held a 10-point lead and had possession with less than 10 minutes to play before everything fell apart on both offense and defense, leading to a 31-20 loss that still stings today.

The difference when the teams meet in the rematch on Sunday will be this time the 49ers will have a quarterback capable of making a big play down the stretch that will deny Mahomes a third Super Bowl title.

San Francisco’s defense held Mahomes to 10 points for the first 50-plus minutes in Super Bowl 54 before giving up three late TDs to lose the game. This year’s unit is not as strong as it was in 2019, but it will be the offense that will carry the Niners to their first championship in 29 seasons.

Brock Purdy has gone from “Mr. Irrelevant” as the final pick in the 2022 draft to the franchise quarterback who has elevated coach Kyle Shanahan’s offense in less than two years. He led the NFL in passer rating (113) and yards per attempt (9.6) with his ability to throw deep and scramble adding new wrinkles to the offense.

Purdy also has a far better group of playmakers than the one that surrounded Jimmy Garoppolo four years ago.

Running back Christian McCaffrey won the AP Offensive Player of the Year award after leading the NFL in yards from scrimmage (2,023) and tying for the lead in touchdowns (21) in the regular season.

Brandon Aiyuk has been one of the most efficient receivers in the NFL with 1,342 yards on 105 targets this season to open up the passing game.

Deebo Samuel has become an even bigger dual threat as a runner and receiver than he was as a rookie in 2019 and tight end George Kittle remains a key part of both the passing and running game.

San Francisco was the sixth team to have four players with at least 1,000 yards from scrimmage in the same season.

That was all lacking four years ago when the offense needed to make only one or two big plays in the fourth quarter to keep the ball away from Mahomes and win the game.

Instead, San Francisco gained one first down on the first two drives of the fourth quarter, giving Mahomes the opportunity to take the lead. Garoppolo then missed an open Emmanuel Sanders on a potential go-ahead deep shot in the closing minutes and threw a late interception to seal the loss.

After playing most of his brief career as a front-runner, Purdy has shown the ability to come from behind in the playoffs if the Niners needed that. He led a game-winning touchdown drive in the fourth quarter of a divisional round win over Green Bay and then helped San Francisco rally from 17 points down to beat Detroit in the NFC title game.

He’s also a far better deep passer than Garoppolo with his 62.3 percent completion rate on deep throws leading all passers this season, according to Pro Football Focus.

Beating Mahomes will require more than just offense and even though the Niners have taken a step back in recent weeks defensively, their strengths match up well with Kansas City.

San Francisco is stout defending runs between the tackles, where the Chiefs have had their most success with Isiah Pacheco.

The Niners are also one of the best teams defending the middle of the field thanks to linebackers Fred Warner and Dre Greenlaw so they should be positioned to at least limit Travis Kelce’s production.

Throw in one of the biggest mismatches with edge rusher Nick Bosa against tackles Jawaan Taylor and Donovan Smith and San Francisco should have enough to win a close game.