First make sure the gain is computed correctly.
Sales Price
less sales expenses
less cost of the home (purchase price, plus closing costs, plus improvements made)
this equals profit or loss.
Then if a personal residence, and you lived in it for at least 2 of the prior 5 years, the gain can be excluded (unless there is some deprecation taken on it as a rental or office in the home).
Now back to your original $177,000 mortgage. This has no impact on the gain or loss on the sale. You could own it outright or have a mortgage, the gain or loss on the sale will still be the same.