Marx argued a society's laws and religion are built on top of its factories
Karl Marx's theory of historical materialism claimed that the 'base' of a society — how it organizes production, who owns what — determines its 'superstructure': its laws, politics, religion and culture. Change the economic base, Marx argued, and the ideas people consider natural or moral will eventually shift to match it, which is why he treated history as driven by material conditions rather than by ideas alone.
— Karl Marx, A Contribution to the Critique of Political Economy — Preface, 1859