Whenever thinking about density and prices, a worthwhile example to consider is Tokyo. Very dense, but housing prices are very affordable. Part of the reason, as I understand, is that Tokyo allows people to keep building (compare to San Francisco).
Graph of Japanese Stock Market: https://www.macrotrends.net/2593/nikkei-225-index-historical...
Graph of Japan's Population: https://tradingeconomics.com/japan/population
Graph of Tokyo's Population: https://www.macrotrends.net/cities/21671/tokyo/population
Average Salary in Tokyo: https://www.timedoctor.com/blog/average-salary-in-japan/ (outdated exchange rate, yen has been collapsing)
In a nutshell: The Japanese economy was larger 33 years ago than it is today, the Japanese population is declining by the millions with even Tokyo now also seeing increasingly declining population, and the average salary in Tokyo is about $45,000. This can absolutely lead to low housing costs, but is probably not a model you want to emulate.