By Gauss Lemma, it is enough to prove that $f$ is irreducible over $\mathbb{Z}$. Suppose
$f(x)=p(x)q(x)$ for non-constant monic polynomials $p$ and $q$ with coefficients in $\mathbb{Z}$. For each $1\le i\le n$, evaluating at $x=a_i$, we see that there are only two cases:
Case 1. $p(a_i)=1$ and $q(a_i)=1$,
Case 2. $p(a_i)=-1$ and $q(a_i)=-1$.
Now suppose $p(a_i)=1$ and $p(a_j)=-1$ for some $i\neq j$. Then, by intermediate value theorem, we get that $p(b)=0$ for some $b$ between $a_i$ and $a_j$. However, this gives $0=p(b)q(b)=f(b)\ge 1$, contradiction.
Hence, $p$ must have take either value 1 for all $a_i$, or must take value $-1$ for all $a_i$. Suppose we are in Case 1. Now, $p(x)-1$ has at least $n$ roots, namely $a_1, a_2, ..., a_n$. Likewise for $q(x)-1$. By comparing the sum of the degrees of the terms in the equation $f(x)=p(x)q(x)$, we get that in fact
$$p(x)-1=q(x)-1=(x-a_1)(x-a_2)\cdots (x-a_n)$$
Consequently,
$$f(x)=(x-a_1)^2(x-a_2)^2\cdots (x-a_n)^2 + 1 = p(x)q(x)$$
$$=[(x-a_1)(x-a_2)\cdots (x-a_n)+1][(x-a_1)(x-a_2)\cdots (x-a_n)+1]$$
$$=(x-a_1)^2(x-a_2)^2\cdots (x-a_n)^2 + 2(x-a_1)(x-a_2)\cdots (x-a_n) + 1$$
We have shown that $(x-a_1)(x-a_2)\cdots (x-a_n)$ is the identically zero polynomial, contradiction. The same argument also applies if we are in Case 2.
I learnt the above solution from The USSR Olympiad Problem Book by Shklarsky, Chentzov, and Yaglom. This book is truly a gem.