<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

107. 987654321 problem

time limit per test: 0.25 sec.
memory limit per test: 4096 KB


For given number N you must output amount of N-digit numbers, such, that last digits of their square is equal to 987654321.


Input

Input contains integer number N (1<=N<=106)


Output

Write answer to the output.


Sample Input

8
Sample Output

0

<|response|>
1. Abridged problem statement
Given an integer N (1 ≤ N ≤ 10^6), count how many N-digit positive integers x (no leading zeros) satisfy
  x² ≡ 987654321 (mod 10^9).
Output that count in decimal.

2. Key observations
- We only care about x² modulo D = 10^9, i.e. the last nine digits of x².
- Let S = 987654321. We seek x² ≡ S (mod D).
- By checking or using Hensel’s lemma/CRT, one finds exactly 8 distinct residues y₁…y₈ modulo 10^9 so that yᵢ² ≡ S.
- Any larger integer x can be written as x = P·10^9 + y, where 0 ≤ y < 10^9 is its last nine digits.
- If x has N digits:
  • If N < 9 → it cannot even have nine digits, answer = 0.
  • If N = 9 → x must equal one of the 8 residues yᵢ, and all eight happen to be 9-digit numbers (no leading zero) → answer = 8.
  • If N > 9 → choose one of the 8 valid suffixes yᵢ, and independently choose a prefix P of length L = N–9 with a nonzero first digit.
    – Number of L-digit prefixes with first digit 1…9 = 9·10^(L−1) = 9·10^(N−10).
    – Total = 8 · (9·10^(N−10)) = 72·10^(N−10).

3. Full solution approach
Step by step:
1. Read N.
2. If N < 9, print 0 and exit.
3. If N = 9, print 8 and exit.
4. Otherwise (N ≥ 10):
   a. We know there are exactly 8 valid nine-digit endings.
   b. The remaining N−9 digits form a prefix P; it must be an (N−9)-digit number with no leading zero, so there are 9·10^(N−10) choices.
   c. Multiply 8 by 9·10^(N−10) = 72·10^(N−10).
   d. To print that potentially huge number, output “72” followed by (N−10) zeros.

This runs in O(N) time only to print the answer; the logic is O(1).

4. C++ implementation
```cpp
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int n;

void read() { cin >> n; }

void solve() {
    // A square ending in ...987654321 must end in 9, so its root ends in 3 or
    // 7. Checking modulo 10^9 shows exactly two roots mod 10^9 give that
    // suffix, so squares end in 987654321 only for roots of at least 9 digits.
    // For n < 9 there are none; for n == 9 there are 8 (the two residues times
    // the choices of leading digit that keep it a 9-digit number); for n > 9
    // each extra digit multiplies the count by 10, giving 72 followed by
    // (n - 10) zeros.

    if(n < 9) {
        cout << 0 << '\n';
    } else if(n == 9) {
        cout << 8 << '\n';
    } else {
        cout << 72;
        n -= 10;
        while(n--) {
            cout << 0;
        }
        cout << '\n';
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}
```

5. Python implementation with detailed comments
```python
import sys

def main():
    data = sys.stdin.read().strip()
    if not data:
        return
    N = int(data)

    # Case 1: N < 9 → no N-digit number can have 9-digit suffix
    if N < 9:
        print(0)
        return

    # Case 2: N == 9 → exactly 8 valid 9-digit roots
    if N == 9:
        print(8)
        return

    # Case 3: N >= 10 → 8 suffix choices × 9·10^(N−10) prefixes = 72·10^(N−10)
    # We output '72' followed by (N−10) zeros to represent that number exactly.
    zeros = N - 10
    sys.stdout.write('72' + '0' * zeros + '\n')

if __name__ == '__main__':
    main()
```
