Shapovalov elements θ β,m of the classical or quantized universal enveloping algebra of a simple Lie algebra g are parameterized by a positive root β and a positive integer m. They relate the highest vector of a reducible Verma module with highest vectors of its submodules.We obtain a factorization of θ β,m to a product of θ β,1 and calculate θ β,1 as a residue of a matrix element of the inverse Shapovalov form via a generalized Nigel-Moshinsky algorithm. This way we explicitly express θ β,m of a classical simple Lie algebra through the Cartan-Weyl basis in g. In the case of quantum groups, we give an analogous formulation through the entries of the R-matrix (quantum L-operator) in fundamental representations.