Principle of Inclusion Exclusion: If $S$ is a set containing $N$ elements. Then the number of objects in $S$ having none of the properties $A_1,A_2,...,A_m$ is
$$
N(A'_1\cap A'_2\cap ...\cap A'_m)=N-\Big[\sum_{i_1}N(A_{i_1})-\sum_{i_1,i_2}N(A_{i_1}\cap A_{i_2})+\sum_{i_1,i_2,i_3}N(A_{i_1}\cap A_{i_2}\cap A_{i_3})-.....\\
+(-1)^{k-1}\sum_{{i_1},{i_2},{i_3}...,{i_k}}N(A{i_1}\cap A_{i_2}\cap A_{i_3}\cap ...\cap A{i_k})+....+(-1)^{m-1}N(A_{1}\cap A_{2}\cap A_{3}\cap ....\cap A_{m})\Big]\\
\implies \boxed{N\Big(\bigcap_{i=1}^mA'_i\Big)=N-N\Big(\bigcup_{i=1}^mA_i\Big)\\
=N-\bigg[ \sum (-1)^{k-1}\sum_{{i_1},{i_2},{i_3}...,{i_k}}N(A{i_1}\cap A_{i_2}\cap A_{i_3}\cap ...\cap A{i_k}) \bigg]\\
=N-\bigg[ \sum (-1)^{k-1}\sum_{{i_1},{i_2},{i_3}...,{i_k}}N\Big(\bigcap_{i=1}^mA_i\Big)\bigg]}
$$
where ${i_1},_{i_2},...,_{i_k}$ are distinct in all the cases.
Let $A=\{a_1,a_2,...,a_m\}$ and $B=\{b_1,b_2,...,b_n\}$, where $m\geq n$, and let $S$ be the set of all functions $f:A\to B$. Then then number of elements in $S$ is, $N=N(S)=n^m$.
Let $A_i=\{b_i \text{is not in the range of }f\}$, then $N(A'_i)$ counts the number of functions that have $b_i$ in their range, ie., $N(A_i)=(n-1)^m,....,N(A_1\cap A_2\cap ...\cap A_k)=(n-k)^m$ for each $i=1,2,...,m$.
$$
\sum_{i_1}N(A_{i_1})={}^nC_1(n-1)^m\\
\sum_{i_1,i_2}N(A_{i_1}\cap A_{i_2})={}^nC_2(n-2)^m\\
.\\
.\\
\sum_{{i_1},{i_2},{i_3}...,{i_k}}N(A_{i_1}\cap A_{i_2}\cap A_{i_3}\cap ...\cap A{i_k})={}^nC_k(n-k)^m
$$
$$
\#\{f:A\to B:f\text{ is onto}\}=N(A'_1\cap A'_2\cap ...\cap A'_m)=N-\Big[\sum_{i_1}N(A_{i_1})-\sum_{i_1,i_2}N(A_{i_1}\cap A_{i_2})+\sum_{i_1,i_2,i_3}N(A_{i_1}\cap A_{i_2}\cap A_{i_3})-.....\\
+(-1)^{k-1}\sum_{{i_1},{i_2},{i_3}...,{i_k}}N(A{i_1}\cap A_{i_2}\cap A_{i_3}\cap ...\cap A{i_k})+....+(-1)^{m-1}N(A_{i_1}\cap A_{i_2}\cap A_{i_3}\cap ....\cap A_{i_m})\Big]
\\
=n^m-\Bigg[ {}^nC_1(n-1)^m-{}^nC_2(n-2)^m+{}^nC_3(n-3)^m-....(-1)^{k-1} .{}^nC_k(n-k)^m \Bigg]\\
={}^nC_0n^m-{}^nC_1(n-1)^m+{}^nC_2(n-2)^m-{}^nC_3(n-3)^m+....(-1)^{k} .{}^nC_k(n-k)^m \\
=\sum_{k=0}^n (-1)^k.{}^nC_k(n-k)^m=n!.\frac{1}{n!}\sum_{k=0}^n (-1)^k.{}^nC_k(n-k)^m=n!.S(m,n)
$$