Let $f=x^3-7x-7$. Suppose $p$ is a prime and $f$ is irreducible over $\mathbb{F}_p$, and let $K = \mathbb{F}_p[x]/(f)$, the field with $p^3$ elements. If we let $\alpha = x \pmod f$ then $\alpha$ is a root of $f$ in $K$. The map $\sigma: K \mapsto K$ defined by $\sigma(r)=r^p$ is an automorphism of $K$ (the Frobenius automorphism).
Because $\sigma$ is an automorphism of $K$ that fixes $\mathbb{F}_p$, a root of $f$ in $K$ is mapped by $\sigma$ to a root of $f$. Let $\beta:= \sigma(\alpha)$ and $\gamma:=\sigma(\beta)$ . The discriminant of $f$ over $\mathbb{Q}$ is $49$ which is non-zero modulo $p$ when $p$ is a prime other than $7$. Therefore $\alpha, \beta \text{ and } \gamma$ are the three distinct roots of $f$ in $K$.
The Galois group of $f$ over $\mathbb{F}_p$ is abelian, because $49$ is a perfect square. It follows that the trace of $\alpha$ is
$$\operatorname{Tr}_{K/\mathbb{F}_p} (\alpha) = \alpha + \beta + \gamma$$
and that the norm of $\alpha$ is
$$\operatorname{N}_{K/\mathbb{F}_p} (\alpha) = \alpha \beta \gamma.$$
Using Vieta’s formulas for the polynomial $f$, we get:
$$\alpha + \beta + \gamma = 0$$
$$\alpha \beta \gamma = 7 \pmod p.$$
Denoting by $A$ the companion matrix of $f \in \operatorname{GL}_{3}(\mathbb{F}_p)$, we get:
$$A + A^p + A^{p^2} = 0 \text{ in } \operatorname{Mat}_3(\mathbb{F}_p)$$ and $$A^{p^2+p+1} = 7\operatorname{I} \text{ in } \operatorname{Mat}_3(\mathbb{F}_p)$$
where $$ A = \begin{pmatrix} 0 & 0 & 7 \\ 1 & 0 & 7 \\ 0 & 1 & 0 \end{pmatrix} $$
(By abuse of notation, we continue below to use $A$ for the matrix in $\operatorname{GL}(n,\mathbb{Z})$)
My question is to characterize those natural numbers $n$ for which
$$A + A^n + A^{n^2} \equiv 0 \pmod n \tag 1$$
and separately
$$A^{n^2+n+1} \equiv 7\operatorname{I} \pmod n \tag 2$$
It has been observed empirically (for $n<10^9$) that if $n$ is a prime different from $7$, then each of congruences (1) or (2) is satisfied if and only if $n \equiv 2, 3, 4, \text { or } 5 \pmod 7$. Over the same range, the only composite satisfying (1) is $n = 4$, while there is no composite satisfying (2).