Factorization at next-to-leading power and endpoint divergences in gg → h production