When is the sum of the products of any three distinct numbers less than $n$, divisible by $n$? Define $A$ as the set of all integer triples $(a,b,c)$, such that $0<a<b<c<n$.
$$A=\left\{(a,b,c)\in \mathbb{Z^3}:1\le a<b<c\le n-1 \right\}$$
Define $S$ as the sum of the product $abc$ for each triplet in $A$.
$$S=\displaystyle \sum_{(a,b,c)\in A}a\cdot b\cdot c$$

For what values of $n$ is $S$ divisible by $n$? Find all possible remainders when $S$ is divided by $n$.


The smallest possible value of $n$ is $4$. If $n=4$, we have, $A=\{(1,2,3)\}$ and $S=6.$ Obviously, $4\nmid 6$.
Claim: $S\equiv 0\pmod n$ for all odd $n$.
Proof: If $(a,b,c)\in A$, then $(n-c,n-b,n-a)\in A$. Since,
$$abc+(n-c)(n-b)(n-a)\equiv abc+(-c)(-b)(-a)\equiv 0 \pmod n$$
and $(a,b,c)\not\equiv (n-c,n-b,n-a)$. Therefore, $n$ divides $S$.
If $n$ is even, the same proof does not work because $(a,b,c)$ and $(n-c,n-b,n-a)$ might be identical.
For example, if we set $a=1,b=\frac{n}{2}$ and $c=n-1$,we have, $(a,b,c)\equiv (n-c,n-b,n-a)$.

Also, I checked upto $n=100$ using a simple program:
L=[]
for n in range (4,101):
    S=0
    for a in range (1,n-2):
        for b in range (a+1,n-1):
            for c in range(b+1,n):
                S=S+a*b*c
    print('n:',n,' ','Sum:',S)
    if S%n==0:
        print(True)
    else:
        print(False)
        L.append(n)
    print()
print(L)

This is the output for the first few numbers
n: 4   Sum: 6
False

n: 5   Sum: 50
True

n: 6   Sum: 225
False

n: 7   Sum: 735
True

n: 8   Sum: 1960
True

n: 9   Sum: 4536
True

n: 10   Sum: 9450
True

n: 11   Sum: 18150
True

n: 12   Sum: 32670
False

n: 13   Sum: 55770
True

n: 14   Sum: 91091
False

This is the list of numbers $n $ for which $S$ is not divisible by $n$.
[4, 6, 12, 14, 20, 22, 28, 30, 36, 38, 44, 46, 52, 54, 60, 62, 68, 70, 76, 78, 84, 86, 92, 94, 100]


Why is $S\equiv 0\pmod n\;, \;\forall n\not\equiv 4 \;\text{or}\; 6 \pmod 8$? Also, when $n$ is even and does not divide $S$, is the below claim true ? $$S\equiv \frac{n}{2} \pmod n$$
 A: Suppose $n$ is even, then $(a,b,c)\in A\implies (n-c,n-b,n-a)\in A~~\forall (a,b,c)\ne (r,\frac n2,n-r)$,
here $(a,b,c)$ and $(n-c,n-b,n-a)$vare non identical.
So to calculate $S$ you only need to consider the cases where $(a,b,c)$ is of the form $(r,\frac n2,n-r)$
$$\begin{align*}S&\equiv \sum_{(a,b,c)=(r,\frac n2,n-r)}a\cdot b\cdot c \pmod n
\\&\equiv \sum_{r=1}^{ \frac{n-2}2}\frac{n^2r-r^2}{2}\pmod n
\\&\equiv \frac{12n^3(n-2)+n(n-2)(2n-2)}{96}\pmod n
\\&\equiv \frac{n(2n+1)(3n-1)(n-2)}{48}\pmod n\end{align*}$$
$n|S$ only if $48|n(2n+1)(3n-1)(n-2)$. It can be seen that $3|n(2n+1)(n-2)$ so the condition reduces to $16|n (n-2) $ which means that $n$ or $n-2$ must be a multiple of 8.
A: Set
$$A_{3,n} := \{(a,b,c) \in \mathbb{Z}^3: 1 \leq a < b<c \leq n-1 \}$$
$$S_{3,n} := \sum_{(a,b,c)\in A_{3,n}}abc$$
Note that $$6S_{3,n} = \sum_{1 \leq a,b,c \leq n-1, a\neq b , b \neq c, a \neq c}abc$$
$$= (\sum_{j=1}^{n-1}j)^3 - 3(\sum_{j=1}^{n-1}j^2)(\sum_{j=1}^{n-1}j)+2\sum_{j=1}^{n-1}j^3$$
$$=\frac{(n-1)^3n^3}{8}-3(\frac{n(n-1)}{2})(\frac{n(n-1)(2n-1)}{6})+\frac{n^2(n-1)^2}{2}$$
$$=\frac{1}{8}(n^2)(n-1)^2(n-2)(n-3)$$
Hence
$$S_{3,n} = \frac{1}{48}(n^2)(n-1)^2(n-2)(n-3)$$
thus $S_{3,n}$ is divisible by $n$ precisely when $\frac{1}{48}(n)(n-1)^2(n-2)(n-3)$ is an integer, which is precisely when
$$1) \text{ }\text{ }\text{ }(n)(n-1)^2(n-2)(n-3) \equiv 0 \mod 16$$
$$2) \text{ }\text{ }\text{ }(n)(n-1)^2(n-2)(n-3) \equiv 0 \mod 3$$
$2)$ always holds as $n \equiv 0,1\text{ or }2\text{ }\mod(3)$. $1)$ holds when $n \equiv 0,1,2,3, 5, 7 \mod 8$. Someone with more stamina can fill the rest of the details.
