import { SSL_OP_LEGACY_SERVER_CONNECT } from 'constants' import * as fs from 'fs' // import * as readline from 'readline' // const rl = readline.createInterface({ input: process.stdin, output: process.stdout }) // const ask = (query: string) => new Promise((resolve) => rl.question(query, resolve)) // // Don't forget `rl.close()`. const INT = Math.floor declare global { interface Array { last(): T | undefined isEmpty(): boolean } } Array.prototype.last = function () { return this.length === 0 ? undefined : this[this.length - 1] } Array.prototype.isEmpty = function () { return this.length === 0 } const bigIntMax = (...args: bigint[]) => args.reduce((m, e) => (e > m ? e : m)) const bigIntMin = (...args: bigint[]) => args.reduce((m, e) => (e < m ? e : m)) const bigIntAbs = (arg: bigint) => (arg < 0 ? -arg : arg) declare const stdin: number function read_stdin() { return fs.readFileSync(process.env.NODE_ENV === 'debug' ? stdin : process.stdin.fd, 'utf8') } class Input { readonly inputs: string[] private index = 0 constructor(str?: string) { this.inputs = (str ? str : read_stdin()).split(/\s+/) } number() { return Number(this.inputs[this.index++]) } numbers(n: number) { return this.inputs.slice(this.index, (this.index += n)).map(Number) } bigint() { return BigInt(this.inputs[this.index++]) } bigints(n: number) { return this.inputs.slice(this.index, (this.index += n)).map(BigInt) } word() { return this.inputs[this.index++] } words(n: number) { return this.inputs.slice(this.index, (this.index += n)) } } function array(len: number, init: T): T[] { return Array(len).fill(init) } function array2(h: number, w: number, init: T): T[][] { return array(h, 0).map(() => array(w, init)) } function array3(d: number, h: number, w: number, init: T): T[][][] { return array(d, 0).map(() => array2(h, w, init)) } const M = 10 ** 9 + 7 function solve(nums: string, include_max: boolean) { const max_state = { mod3: 0, has3: false, mod8: 0, } let dp = array3(3, 2, 8, 0) for (let i = 0; i < nums.length; i++) { const dp2 = array3(3, 2, 8, 0) const digit = Number(nums[i]) for (let j = 0; j < digit; j++) { const m3 = (max_state.mod3 * 10 + j) % 3 const m8 = (max_state.mod8 * 10 + j) % 8 const has3 = max_state.has3 || j === 3 ? 1 : 0 dp2[m3][has3][m8] += 1 } max_state.mod3 = (max_state.mod3 * 10 + digit) % 3 max_state.mod8 = (max_state.mod8 * 10 + digit) % 8 max_state.has3 = max_state.has3 || digit === 3 for (let m3 = 0; m3 < 3; m3++) { for (let has3 = 0; has3 < 2; has3++) { for (let m8 = 0; m8 < 8; m8++) { for (let j = 0; j < 10; j++) { const t3 = (m3 * 10 + j) % 3 const h3 = has3 || j === 3 ? 1 : 0 const t8 = (m8 * 10 + j) % 8 dp2[t3][h3][t8] = (dp2[t3][h3][t8] + dp[m3][has3][m8]) % M } } } } dp = dp2 } let r = 0 if (include_max && (max_state.mod3 === 0 || max_state.has3) && max_state.mod8 !== 0) { r += 1 } for (let i = 1; i < 8; i++) { r = (r + dp[0][0][i]) % M r = (r + dp[0][1][i]) % M r = (r + dp[1][1][i]) % M r = (r + dp[2][1][i]) % M } return r } function main() { const input = new Input() const A = input.word() const B = input.word() const r = (solve(B, true) - solve(A, false) + M) % M console.log(r) } main()