It is well-known that a prime number $p$ is $\equiv 1 \pmod 4$ iff $p=x^2+y^2$ for some integers $x,y$ (except for $p=2$). My question is: is there an irreducible homogeneous polynomial $f \in \Bbb Z[x_1, ..., x_n]$ of total degree $>1$, such that $p \equiv 3 \pmod 4$ iff $p \in \mathrm{Im}(f)$ (up to finitely many exceptions) ? I saw this question, but this is also the condition $p \mid f(y)$ for some $y \in \Bbb Z^n$ (and not $p=f(y)$ as I want).
More generally, given $M \geq 1$ and $S \subset \Bbb Z/M\Bbb Z$, when is there $n \geq 1$ and an irreducible homogeneous polynomial $f \in \Bbb Z[x_1, ..., x_n]$ of total degree $>1$, such that a prime $p$ is in the image of $f$ iff $[p]_M \in S$, up to finitely many exceptions?