<|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 with detailed comments  
#include <bits/stdc++.h>  
using namespace std;

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

    long long N;
    cin >> N;

    // Case 1: fewer than 9 digits → impossible to match 9-digit pattern
    if (N < 9) {
        cout << 0 << "\n";
        return 0;
    }

    // Case 2: exactly 9 digits → exactly 8 roots mod 1e9, all are 9-digit
    if (N == 9) {
        cout << 8 << "\n";
        return 0;
    }

    // Case 3: N ≥ 10 → total = 72 * 10^(N−10)
    // Print "72" then (N−10) zeros
    cout << 72;
    long long zeros = N - 10;
    // Append zeros one by one
    while (zeros--) {
        cout << '0';
    }
    cout << "\n";
    return 0;
}

5. Python implementation with detailed comments  
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()