Let $T:X \to X$ be a measure preserving ergodic invertible transformation on a probability space. Let $n(x)$ be the first return time to a measurable set $A$ with $\mu(A)>0$. The task is to show that for an $f \in L^1(X)$,
$\int_X f d \mu = \int_A \sum_{i=0}^{n(x)-1} f(T^i(x)) d \mu$
We know that by ergodicity eventually almost every point must enter A. Hence the return time $n(x)$ is defined almost everywhere. I'm guessing we are supposed to use some type of kakutani skyscraper argument and use that $X$ is the union of $A_n=\{n(x)=n\}$ but I'm not sure how to proceed.
I would decompose the whole system into small brick. First if you call $B_k = n^{-1}(k)$ for $k \in \mathbb{N}$ you have (by Poincaré theorem):$$ A = \cup_{ k \in \mathbb{N}} B_k \text{ }mod \text{ }0 $$ Then let $B_k^j= T^{j}(B_k) $, you also have $$ X = \cup_{ k \in \mathbb{N}, j \in [0,k-1]} B_k^j \text{ }mod \text{ }0 $$ So you can decompose your integrals in this small block$$ \int_X f d \mu = \sum_{ k \in \mathbb{N}, i \in [0,k-1]} \int_{ B_k^i} f d \mu $$ And $$ \int_A \sum_{i=0}^{n(x)-1} f(T^i(x)) d \mu = \sum_{ k \in \mathbb{N}} \int_{B_k} \sum_{i=0}^{k-1} f(T^i(x)) d \mu =\sum_{ k \in \mathbb{N}, i \in [0,k-1]} \int_{B_k} f(T^i(x)) d \mu $$ And by $T$-invariance of the measure $\mu$ $$ \int_{B_k} f(T^i(x)) d \mu = \int_{ B_k^i} f d \mu $$ Which conclude the proof.