import bisect import copy import decimal import fractions import heapq import itertools import math import random import sys import time from collections import Counter,deque,defaultdict from functools import lru_cache,reduce from heapq import heappush,heappop,heapify,heappushpop,_heappop_max,_heapify_max def _heappush_max(heap,item): heap.append(item) heapq._siftdown_max(heap, 0, len(heap)-1) def _heappushpop_max(heap, item): if heap and item < heap[0]: item, heap[0] = heap[0], item heapq._siftup_max(heap, 0) return item from math import gcd as GCD read=sys.stdin.read readline=sys.stdin.readline readlines=sys.stdin.readlines write=sys.stdout.write A,B,P=readline().split() A=list(map(int,list(A))) B=list(map(int,list(B))) P=int(P) mod=10**9+7 def solve(N): if len(N)<=6: ans=0 N=int("".join(map(str,N))) for n in range(1,N): if (n%3==0 or "3" in str(n)) and n%P: ans+=1 return ans dp0=[0]*3 dp1=[0]*3 for i in range(1,N[0]): if i==3: dp1[i%3]+=1 else: dp0[i%3]+=1 s=N[0] bl=(N[0]==3) for n in N[1:-5]: prev0=dp0 prev1=dp1 dp0=[0]*3 dp1=[0]*3 for p in range(3): for i in range(10): if i==3: dp1[(p+i)%3]+=prev0[p] dp1[(p+i)%3]+=prev1[p] dp1[(p+i)%3]%=mod else: dp0[(p+i)%3]+=prev0[p] dp1[(p+i)%3]+=prev1[p] dp0[(p+i)%3]%=mod dp1[(p+i)%3]%=mod for i in range(n): if bl|(i==3): dp1[(s+i)%3]+=1 else: dp0[(s+i)%3]+=1 for i in range(1,10): if i==3: dp1[i%3]+=1 else: dp0[i%3]+=1 s=(s+n)%3 bl|=(n==3) X=N[-5]*10000+N[-4]*1000+N[-3]*100+N[-2]*10+N[-1] ans=0 for i in range(3): for x in range(100000): if x%P==0: continue if (i+x)%3==0 or "3" in str(x): ans+=dp0[i] ans+=dp1[i] for x in range(1,X): if x%P==0: continue if (s+x)%3==0 or "3" in str(x) or bl: ans+=1 for x in range(1,100000): if x%P==0: continue if x%3==0 or "3" in str(x): ans+=1 return ans%mod ans=(solve(B)-solve(A))%mod B=[0]*4+B if (3 in B or sum(B)%3==0) and (B[-5]*10000+B[-4]*1000+B[-3]*100+B[-2]*10+B[-1])%P!=0: ans+=1 ans%=mod print(ans)