Solving a Cournot oligopoly game with N firms
Your FOC is wrong.

So $$\max_{q_i} \quad q_i(a − \sum_{j=1}^nq_j) - q_i^2$$

$$\implies \max_{q_i} \quad aq_i − q_i\sum_{j=1}^nq_j - q_i^2$$

The real FOC is

$$a - \sum_{j\neq i}q_j - 2q_i - 2q_i = 0.$$