What impact did the political factors have on the political stability of the Tang and Song dynasties?