cardinality of given set = $n$.
binary operation is like a function defined on base set $S$ like this, $f: S \times S → S$
where, domain = $S \times S$ and co-domain = $S$
total no. of possible pairs in domain = $n^2$
total no. of possible $(a, a)$ pairs in domain = $n$.
these $n$ pairs will have $n$ possiblities in co-domain, each.
so, total no. of ways in which every $(a, a)$ element of domain is connected with exactly one element of co-domain ( satisfying definition of function ) = $n^n$
now, total no. of possible $(a, b)$ pairs where a and b are different = $n^2 - n$.
we can think like this from here:
every pair of $(a, b)$ in $n^2 - n$ pairs will form a small bubble of 2 pairs namely $(a, b)$ and $(b, a)$. we will call them a couple and only 1 pair of the 2 will need to choose for both of them.
total no. of such possible couples = $\frac{n^2 - n}{2}$
now each couple will choose an element out of $n$ elements from co-domain.
so, total no. of ways in which a couple can choose exactly one element from co-domain = $n^{\frac{n^2 - n}{2}}$
now, on combining both the results:
total no. of possible commutative functions (binary operations) = $n^n \times n^{\frac{n^2 - n}{2}} = n^{\frac{n^2 + n}{2}}$