Let $S = \{(a_1, a_2, \dots, a_p) \mid a_1 + 2a_2 + \dots + pa_p = 1, a_i \neq i\}$, $T = \{(a_1, a_2, \dots, a_p) \mid a_1 + 2a_2 + \dots + pa_p = 4, a_i \neq i\}$,
and $h(x) \equiv 2x, 1 \leq h(x) \leq p$. Note that $h$ is bijective. Define the function $\Gamma : S \rightarrow T$ as
$$\Gamma(a_1, a_2, \dots, a_p) = \left(h(a_{h^{-1}(1)}), h(a_{h^{-1}(2)}), \dots, h(a_{h^{-1}(p)})\right)$$1. $a_i \neq i \Longleftrightarrow a_{h^{-1}(i)} \neq h^{-1}(i) \Longleftrightarrow h(a_{h^{-1}(1)}) \neq i$
2. $f\left(h(a_{h^{-1}(1)}), h(a_{h^{-1}(2)}), \dots, h(a_{h^{-1}(p)})\right) = \sum_{i = 1}^{n}ih(a_{h^{-1}(i)})$ $=\sum_{i = 1}^{n}h(i)h(a_{i}) \equiv \sum_{i = 1}^{n}2i\cdot 2a_i$ $= 4f(x_1, x_2, \dots, x_p) \equiv 4 \pmod p$
Hence, $\Gamma(S) \subset T$. We can see $\Delta(T) \subset S$ with similar argument, where
$$\Delta : T \rightarrow S, \ \Delta(a_1, a_2, \dots, a_p) = \left(h^{-1}(a_{h(1)}), h^{-1}(a_{h(2)}), \dots, h^{-1}(a_{h(p)})\right)$$Since $\Gamma$ and $\Delta$ are bijective (can be proved easily), the result $|S| = |T|$ follows. $\blacksquare$