How JPMorgan’s settlement could boost its profits

October 29, 2013, 8:31 PM UTC

FORTUNE — Don’t cry for JPMorgan: The latest settlement announced on Friday could actually boost its profits by $1.1 billion.

The exact amount isn’t certain. And the bottom line boost is likely to stretch over the next year or so. But the fact that JPMorgan (JPM) is able to see any gain from the penalties it’s shelling out for its misdeeds during the housing bubble is another milestone in the miracle of bank accounting.

How so? Sharpen your pencil.

There were two parts to Friday’s $5.1 billion deal. The first part was to settle a lawsuit regulators brought on behalf of Fannie Mae and Freddie Mac more than two years ago, against JPMorgan and other banks. The suits alleged that JPMorgan and others underwrote mortgage bonds that contained loans that were riskier than the banks let on. Fannie and Freddie bought the bonds, along with other investors, and lost money. JPMorgan is paying $4 billion to settle the claim that it, along with Bear Stearns and Washington Mutual, which JPMorgan bought in 2008, are in part responsible for some of the losses on the bonds.

MORE: JPMorgan’s $13 billion fine: payoff, not extortion

That seems about right. The amount was far less than what Fannie and Freddie lost on the soured deals. But Fannie and Freddie share some responsibility as well. What’s more, the amount was in line with what other banks have been paying to settle similar lawsuits.

But wrapped into the deal was also a payment that had to do with the mortgage loans that JPMorgan and Bear Stearns passed to Fannie and Freddie to insure in the run-up to the housing bust. Many of those loans weren’t up to snuff either and ended up in default. Fannie and Freddie have been battling with JPMorgan and other banks over how much of those losses the government guarantors should swallow, and how much should be sent back to the banks. The cost of resolving all those disputed repurchase claims from 2000 to 2008 for JPMorgan: $1.1 billion.

That appears to be an exceptionally good deal. It’s much less than other banks have paid. For instance, earlier this year, Bank of America (BAC) paid $10.4 billion, including a payment for nearly $7 billion in loans, to settle its disputes over loans that Fannie guaranteed in the same time period. That was on top of a $1.4 billion settlement it had already reached with Fannie. In early October, Wells Fargo paid $900 million, but that deal only covered mortgages insured by Freddie. It still needs to settle with the larger Fannie.

But perhaps the best evidence that the deal was particularly sweet for the bank comes from JPMorgan itself. As recently as the end of September, JPMorgan estimated that a settlement with Fannie and Freddie over its repurchase liability could cost the bank $2.2 billion. And that’s where the $1.1 billion gain could come from.

When banks make an estimate of losses, they have to set aside money for that. It goes into something called a reserve and banks have to take a charge when they set it up. But if a bank doesn’t end up needing all the money it has put aside for a loss, it’s supposed to dissolve the reserve. Any cash remaining goes straight to the bank’s pre-tax profits.

MORE: JPMorgan’s fine is bad news for Bank of America, Wells

It’s not clear JPMorgan will immediately add the full $1.1 billion to its bottom line. JPMorgan declined to comment.

The reserve only covers Fannie and Freddie repurchase claims. The $4 billion portion of the settlement comes from JPMorgan’s legal reserve, which last quarter the bank upped to $23 billion. But the Fannie and Freddie reserve also covers repurchase claims that came after the financial crisis. Those loans, though, have proven less problematic than loans made before the crisis. In the past year, just 8% of all of JPMorgan’s repurchase claims were on mortgages that were made after 2008.

And it’s possible that JPMorgan could make more than $1 billion. The $1.1 billion JPMorgan is paying Fannie and Freddie is to repurchase loans on which borrowers have missed a payment or stopped paying completely. That doesn’t mean those loans are worthless. Some of those borrowers might start paying again, either on their own or after a modification. Houses that end up in foreclosure can still be sold. What’s more, there are hedge funds and distressed debt buyers who would be happy to take those loans off JPMorgan’s hands for a price. Based on the bank’s past estimates, JPMorgan could generate $151 million from selling the repurchased loans.

All this could explain, at least in part, why JPMorgan’s stock has continued to climb even as its legal problems have grown. For JPMorgan, the penalty for its past mortgage deeds will end up being far less painful than it seems.