# Contractions of probability amplitude in quantum field theory

Given a Lagrangian $$\mathcal{L}=\frac12(\partial_\mu \phi(x))^2+\frac12m^2 \phi(x)^2-\frac{\lambda}{4!} \phi(x)^4$$

The probability amplitude in each order is given by: $$A=(2\pi)^3 (2E_q)^{1/2} (2E_p)^{1/2} \langle 0|\hat{a_q} \sum_{n=0}^{\infty} T\left[\frac{\left(-i\int d^{4}z\ \hat{\phi}^4(z)\right)^n}{n!}\right] \hat{a_p}^\dagger |0\rangle$$

One of the difficult points in evaluating the $$n^{th}$$ order term is counting the distinct contractions using Wick's theorem.

Mathematically, given that there are $$n$$ objects with $$N$$ distinct types of identical objects, in the $$i$$ group, there are $$n_i$$ distinct objects, namely $$\sum_{i=1}^{N} n_i=n$$, what is the general formula of counting the number of each distinct pairing?