Prime Not-Prime
Difficulty: Hard

Jigar is trying to solve a math problem and prove to Aghaye Mojri that he is good at math, But it seems this problem is too tough to be solved on paper, So he asks you to calculte the SUM of |p-x| for all prime numbers *p* in range [1, n] and all non prime integers *x* in range [1, n]. As the answer can be very large, print the answer mod (10<sup>9</sup>+9).

__Input Format__ <br>
The first line contains an integer __T__, the number of test cases.  
This is followed by __T__ lines each containing 1 integer __n__.

__Constraints__ <br>
1 &le; T ≤ 20 <br>
1 ≤ n ≤ 600000

__Sample Input__ <br>
<pre>
2
2
5
</pre>

__Sample Output__ <br>
<pre>
1
11
</pre>

__Explanation__ <br>

The primes in the range [1, 2] ( both included ) are just [2] and the non primes in the range [1, 2] ( both included ) are just [1]. So, 

we get 

    |2 - 1| = 1

The primes in the range [1, 5] are [2, 3, 5] and the non-primes are [1, 4]. So, 

we get

    |2-1|+|2-4|+ |3-1|+|3-4| + |5-1|+|5-4| = 1+2 + 2+1 + 4+1 = 11


Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (C++):
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string.h>
    using namespace std;

    typedef long long ll;
    const ll MOD = 1000000009;
    const ll N = 600000+10;
    bool mark[N];

    int main()
    {
        memset(mark, 0, sizeof mark);
        mark[0] = mark[1] = true;
        for(ll i = 2; i*i < N; i ++)
            if(mark[i] == false)	// i is prime
                for(ll j = i*i; j < N; j += i)
                    mark[j] = true;

        ll test, n;
        ll allPrime;
        ll leftPrime, leftNotPrime;
        ll rightPrime, rightNotPrime;

        cin >> test;
        while(test --)
        {
            cin >> n;
            allPrime = 0;
            for(ll i = 1; i <= n; i ++)
                if(mark[i] == false)
                    allPrime ++;

            leftPrime = 0;
            leftNotPrime = 1;
            rightPrime = allPrime;
            rightNotPrime = n-allPrime-1;

            ll i = 1;
            long long res = 0;
            while(i < n)
            {
                res = (res + leftPrime*rightNotPrime) % MOD;
                res = (res + leftNotPrime*rightPrime) % MOD;
                if(mark[i+1] == false)	// i+1 is prime
                {
                    leftPrime ++;
                    rightPrime --;
                }
                else
                {
                    leftNotPrime ++;
                    rightNotPrime --;
                }
                i ++;
            }		
            cout << res << endl;
        }

        return 0;
    }