Buddhism, Taoism and ancestor worship as practiced by the Chinese was a religion and superstitious and their political nonsense based on the worship of an Emperor and autocratic system led to their decline. So Sagan is correct.
While Buddhism and Taoism certainly are religions, I think you'll find that the history of China and their decline have almost nothing to do with it.
First of all, China was not the HRE. The leadership and politics of China were _not_ primarily based on any religion, but on various schools of philosophy about how a perfect society should work. Probably the best known is Confucianism, but other currents which fought for the top spot included legalism, mohism, etc.
Confucianism is not a religion. It's an ultra-conservative old fart's "know your place and stay off my lawn" philosophy. When it preaches stuff like that a son doesn't even have any free will, to the extent that you can't even judge someone until 3 years after his father's death, because until then he must do only what his father says, like a brainless robot... it's not some "because otherwise you'll burn in hell", but just pretty much "because that's the right way." When he even touch religious topics at all, like that some noble went on a pilgrimage that only the Emperor was supposed to do, it's not from a "the gods will surely punish him" angle, but merely that it was not his place and that's not how a proper member of society should act.
And also the most enforced for the last millenium or so of their empire. To even apply for any government job, you had to go through the imperial examination and prove that you know your Confucius by heart.
It did give them a certain inflexibility, because it was a very inflexible philosophy, but it was a philosophy not a religion anyway.
Second, the big decline of China actually had nothing to do with adopting any given religion or philosophy, but with the Qing (manchu) dynasty. They were confucian and buddhist before the manchu takeover, and they were confucian and buddhist after, but you can see how they went from steady progress (although slowed considerably during Ming isolationism) to sharp decline almost overnight.
Contrary to popular "china invented gunpowder but not guns" ideas, they had actually had guns, flamethrowers, rockets used as artillery, etc. They actually devolved to swords and polearms during the Qing dynasty. So by the time of the opium wars, yeah, the Brits faced whole divisions of guys with heavy polearms charging at the guns.
The Qing had their own weird ideas and fixations that were neither properly chinese nor confucian nor buddhist. E.g., they went and enforced their own haircut upon the population, with IIRC the death penalty for having a different haircut.
Corruption during the Qing also reached unprecedented levels. The most surrealistic of example of it is probably in the Battle Of Yalu River in 1894. Some intrepid souls had actually stolen and sold off two main guns off the admiral's battleship. Roll that around in your head: main guns off a bloody battleship. And nobody "noticed." That's some corruption or what? Or, get this, the company making the shells had replaced the cordite in them with sawdust and cement and split the profit with the corrupt officials who were supposed to check. Or the admiral's second in command (no doubt, a fellow with connections) refused to relay the order to deploy in battle line, because, hey, then the Japanese could shoot at the ship he's on, and he's not gonna risk his life for his country.
So basically you don't even really see the effects of religion or woowoo thinking in the Chinese decline, but merely the effects of a bad dynasty. Better yet, one who thought they can return to the good ol' medieval times.