An Equilibrium Model of Housing and Mortgage Markets with State-Contingent Lending Contracts