Let $p$ be a prime number. A flea is staying at point $0$ of the real line. At each minute,
the flea has three possibilities: to stay at its position, or to move by $1$ to the left or to the right.
After $p-1$ minutes, it wants to be at $0$ again. Denote by $f(p)$ the number of its strategies to do this
(for example, $f(3) = 3$: it may either stay at $0$ for the entire time, or go to the left and then to the
right, or go to the right and then to the left). Find $f(p)$ modulo $p$.
1. **Understanding the Problem:**
   The flea can move left, right, or stay in place. After \( p-1 \) minutes, it must return to the starting point, 0. We need to find the number of ways \( f(p) \) for the flea to achieve this, modulo \( p \).

2. **Formulating the Problem:**
   Let \( k \) be the number of left movements and \( k \) be the number of right movements. The remaining \( p-1-2k \) movements are staying in place. The total number of ways to arrange these movements is given by:
   \[
   f(p) = \sum_{k=0}^{(p-1)/2} \frac{(p-1)!}{k! k! (p-1-2k)!}
   \]

3. **Applying Wilson's Theorem:**
   Wilson's theorem states that for a prime \( p \), \((p-1)! \equiv -1 \pmod{p}\). We use this to simplify the factorials in the expression:
   \[
   (p-1-a)! \equiv (-1)^{a-1} \frac{1}{a!} \pmod{p}
   \]

4. **Simplifying the Expression:**
   Using the above result, we rewrite \( f(p) \):
   \[
   f(p) = \sum_{k=0}^{(p-1)/2} \frac{(p-1)!}{k! k! (p-1-2k)!} \equiv \sum_{k=0}^{(p-1)/2} \frac{-1}{k! k! (-1)^{2k-1} \frac{1}{(2k)!}} \pmod{p}
   \]
   Simplifying further:
   \[
   f(p) = \sum_{k=0}^{(p-1)/2} \frac{(2k)!}{k! k!} = \sum_{k=0}^{(p-1)/2} \binom{2k}{k} \pmod{p}
   \]

5. **Using Generating Functions:**
   The generating function for central binomial coefficients is:
   \[
   (1-4x)^{-1/2} = \sum_{k=0}^{\infty} \binom{-1/2}{k} (-4x)^k = \sum_{k=0}^{\infty} \binom{2k}{k} x^k
   \]
   From this, we get:
   \[
   \binom{-1/2}{k} (-4)^k = \binom{2k}{k}
   \]
   Thus:
   \[
   \binom{2k}{k} \equiv \binom{\frac{p-1}{2}}{k} (-4)^k \pmod{p}
   \]

6. **Summing the Series:**
   We sum the series:
   \[
   \sum_{k=0}^{(p-1)/2} \binom{\frac{p-1}{2}}{k} (-4)^k \equiv (-3)^{\frac{p-1}{2}} \pmod{p}
   \]

7. **Using Legendre Symbol:**
   The Legendre symbol \(\left( \frac{-3}{p} \right)\) helps determine the value of \((-3)^{\frac{p-1}{2}}\):
   \[
   \left( \frac{-3}{p} \right) = \left( \frac{p}{3} \right)
   \]
   Therefore:
   \[
   f(p) = 0 \text{ for } p=2,3
   \]
   \[
   f(p) = 1 \text{ when } p \equiv 1 \pmod{3}
   \]
   \[
   f(p) = -1 \text{ when } p \equiv 2 \pmod{3}
   \]

The final answer is \( \boxed{ f(p) = 0 \text{ for } p=2,3 } \), \( f(p) = 1 \text{ when } p \equiv 1 \pmod{3} \), and \( f(p) = -1 \text{ when } p \equiv 2 \pmod{3} \).