ALBANY – After weeks of fighting, Gov. Andrew Cuomo and New York City Mayor Bill de Blasio announced a deal Saturday to fund the massive capital plan for the Metropolitan Transportation Authority.

The state and the city had been at odds over how to pay for the MTA’s $9.8 billion funding gap for its five-year, $32 billion capital plan.

The disagreement threatened services and risked further toll hikes for the 1.77 billion riders expected this year on the New York City region transit system — the largest ridership since 1949.

The MTA provides transit services for the Hudson Valley, including Westchester, Rockland, Putnam and Dutchess counties. And Metro-North is expected to set a new record this year with nearly 84 million riders.

“This plan will mean a safer, stronger, more reliable transit system for people all over New York, and is crucial in supporting our growing economy,” Cuomo said in a statement.

The agreement includes the state paying $8.3 billion, and New York City paying $2.5 billion. The MTA will pay the rest.

“Our transit system is the backbone of New York City’s, and our entire region’s, economy,” de Blasio said in a statement.

Cuomo and de Blasio, both Democrats who have been battling over a host of issues since the mayor took office in 2014, had been knocking each other in recent weeks over the MTA stalemate.

The governor pressed de Blasio to put in more city funds into the capital plan, while de Blasio charged that the state had raided some funding for the MTA for other initiatives. They disagreed with each other’s assertions, but reached a deal Saturday that was applauded by unions and lawmakers.

“Today’s announced agreement between the city of New York and the state is great news for the tens of thousands of New Yorkers who depend on a reliable mass transit system,” Assembly Speaker Carl Heastie, D-Bronx, said in a statement.

The capital plan has been critical for the system’s future as it grapples with aging subway lines, trains and stations.

But the MTA funding plan, the largest in its history, also needed a number of revisions to make it affordable with further increasing tolls — which already had grown by 33 percent since 2007.

The MTA has also reduced the capital plan to $26.8 billion and expects to close another $700 million gap by additional efficiencies, according to Saturday’s deal.

Four percent 4 percent toll increases are already planned for 2017 and 2019, with small operating budget gaps over the next several years, Comptroller Thomas DiNapoli warned in a report last month.

Cuomo has indicated that the state’s portion of the deal will be funded through the state budget for the fiscal year that starts April 1. He will release his 2016-17 spending plan in January, and the Legislature will need to adopt it by March 31.

The city plans to fund its piece in part through $600 million in “alternative non tax levy revenue sources.”

The spending will be made proportionate to the services in the 12-county MTA region, the agreement stated.

For example, projects in the city that are funded by the city will be planned with the MTA board in cooperation with city officials. In the suburbs, local officials will collobrate on the projects that will be funded.

The state and city also agreed to not divert any money for the MTA to other state or city projects.

Union leaders praised the deal. The MTA faced a crucial board meeting later this month on how to proceed with its capital plan, and already some repair work had been delayed.

“This is obviously a great win for the thousands of transit workers whose jobs are directly impacted by the MTA Capital Plan, and for the millions of New York’s working families who use the bus and subway system every day,” TWU Local 100 president John Samuelsen said in a statement.

Joseph Spector: jspector@gannett.com, Twitter: @gannettalbany