How did Leibniz know to write derivatives as $$\frac{dy}{dx}$$ so that everything would work out? For example, the chain rule: $$\frac{dy}{dz}=\frac{dy}{dx}\frac{dx}{dz}$$ Integration by Parts: $$xy=\int d(xy)=\int x\,dy+y\,dx \implies \int x\,dy =xy-\int y\,dx$$ Separable differential equations: $$\frac{dy}{dx}=\frac{x}{y}\implies y\,dy=x\,dx\implies y^2-x^2=C$$ Even basic derivatives such as $$\frac{dx}{dx}=1$$ It seems like they cancel!
Everyone I ask always says either 1) it is essentially a lucky accident, 2) presents a "counterexample" that I usually don't think is valid, or 3) says that it can be made rigorous but that's very tedious to do... but clearly Leibniz was not in any of the three situations proposed. He must have had some reason for knowing why his notation worked so well - after all, he invented it.
As for Newton, did he know the same things as Leibniz? How come he wasn't able to come up with an equally useful notation - did he perhaps think about calculus differently?