Let set $\mathcal{S}$ define the positive integers $a_1, a_2, \ldots, a_n < 3^{2019}$ which contain only $0$'s and $1$'s in their ternary base representation. Compute $$a_1^2+a_2^2+a_3^2+\cdots+a_n^2 \pmod{1000}.$$
I started by converting $1, 10, 100, \ldots$ base $3$ to base $10.$ This clearly gives $3^0, 3^1, 3^2, \ldots$ in base $10.$ Then I considered the the possible numbers in base $3$ that weren't just a $1$ with a bunch of trailing zeroes, and split them into sections. We get $(1), (10, 11), (100, 101, 110, 111), (1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111), \ldots.$ Then I counted the numbers in each of the pairs and noted that they were simply powers of $2,$ specifically for some $3^n,$ there would be $2^n-1$ more consecutive numbers that would also be in $\mathcal{S}.$ However, I am not sure how to use this information, especially because the sum of the squares of every element in $\mathcal{S}$ seems tricky to deal with, and taking mod $1000$ will be very tricky with such large numbers. Is it possible to manipulate the question in some way to make it easy? May I have some help? Thanks in advance.