Obviously if $m<n$, there are no function from $\Bbb A$ onto $\Bbb B$, so assume that $m\ge n$. We’ll use an inclusion-exclusion argument. There are $n^m$ functions of all kinds from $\Bbb A$ to $\Bbb B$. If $b\in\Bbb B$, there are $(n-1)^m$ functions from $\Bbb A$ to $\Bbb B\setminus\{b\}$, i.e., functions whose ranges do not include $b$. We need to subtract these from the original $n^m$, and we need to do it for each of the $n$ members of $\Bbb B$, so a better approximation is $n^m-n(n-1)^m$.
Unfortunately, a function whose range misses two members of $\Bbb B$ gets subtracted twice in that computation, and it should be subtracted only once. Thus, we have to add back in the functions whose ranges miss at least two points of $\Bbb B$. If $b_0,b_1\in\Bbb B$, there are $(n-2)^m$ functions from $\Bbb A$ to $\Bbb B\setminus\{b_0,b_1\}$, and there are $\binom{n}{2}$ pairs of points of $\Bbb B$, so we have to add back in $\binom{n}2(n-2)^m$ to get
$$n^m-n(n-1)^m+\binom{n}2(n-2)^m\;,$$
which can be expressed more systematically as
$$\binom{n}0n^m-\binom{n}1(n-1)^m+\binom{n}2(n-2)^m\;.$$
Unfortunately, this over-corrects in the other direction, by adding back in too much. The final result, when the entire inclusion-exclusion computation is made, is
$$\sum_{k=0}^n(-1)^k\binom{n}k(n-k)^m\;,$$
which can also be written $$n!{m\brace n}\;,$$ where ${m\brace n}$ is a Stirling number of the second kind. The Stirling number gives the number of ways of dividing up $\Bbb A$ into $n$ non-empty pieces, and the $n!$ then gives the number of ways of assigning those pieces to the $n$ elements of $\Bbb B$.
Your first answer is correct. As for the second question, consider the following process of constructing a suitable function:
$f(1)$ has to be something. If $f(1)$ is $1$, then $f(f(1))=1$, which is disallowed. So, set $f(1)=a$, where $a$ is some number in this set which isn't $1$.
Our constraint is that $f(f(1))=3$. That is, $f(a)$ has to be $3$. So, set $f(a)=3$. We are now sure that our function, however we proceed to construct the rest of it, will satisfy the constraint $f(f(1)) = 3$. Proceed to extend the function to all other numbers in this set. Note that any function satisfying the constraint may be constructed in this fashion.
Now, we ask the question: how many ways are there to proceed through this process?
In our first step, setting $f(1) = a$, we have four choices of $a$. Having chosen an $a$, we have no choice but to set $f(a) = 3$. We have $3$ more distinct elements $t\in A$ whose value $f(t)$ must be chosen from a set of the $5$ possible options in $A$.
Thus, there are $4\times 5 \times 5 \times 5 = 500$ possibilities.
All right, apparently this process is confusing, so let's build an example function.
First, we need to choose some $a \neq 1$. Let's say $a = 4$. Now, we have $f(1) = 4$, and $f(4) = 3$.
With that settled, we have to figure out what $f(2),f(3),$ and $f(5)$ are. Let's say $f(2) = 1$, $f(3) = 3$, and $f(5) = 4$. We now have a suitable function. We could have picked any of $5$ values for each of $f(2),f(3)$ and $f(5)$.
Best Answer
Hint. If $f \circ f (1)=2$ then $f(1)=k$ and $f(k)=2$ for some $k\in \{2,3,4,5,6,7,8,9\}$ ($k=1$ is not allowed otherwise $f \circ f (1)=1$). Note that in order to be onto, $f$ should be bijective (hence $f(1)\not=2$ otherwise $f(1)=f(k)=2$ and $f$ is not injective).