We prove a generalized Kac-Rice formula that, in a well defined regular setting, computes the expected cardinality of the preimage of a submanifold via a random map, by expressing it as the integral of a density. Our proof starts from scratch and although it follows the guidelines of the standard proofs of Kac-Rice formula, it contains some new ideas coming from the point of view of measure theory. Generalizing further, we extend this formula to any other type of counting measure, such as the intersection degree.We discuss in depth the specialization to smooth Gaussian random sections of a vector bundle. Here, the formula computes the expected number of points where the section meets a given submanifold of the total space, it holds under natural nondegeneracy conditions and can be simplified by using appropriate connections. Moreover, we point out a class of submanifolds, that we call sub-Gaussian, for which the formula is locally finite and depends continuously with respect to the covariance of the first jet. In particular, this applies to any notion of singularity of sections that can be defined as the set of points where the jet prolongation meets a given semialgebraic submanifold of the jet space.Various examples of applications and special cases are discussed. In particular, we report a new proof of the Poincaré kinematic formula for homogeneous spaces and we observe how the formula simplifies for isotropic Gaussian fields on the sphere. Contents 0.1. Overview 0.2. Structure of the paper 0.3. Aknowledgements