human
What is a Python program to print one integer, the number of such X, that L ≤ X ≤ R and F(X) is a lucky number, modulo 1000000007 given the following:
I found an interesting problem on https://www.codechef.com/problems/LUCKY2:
Chef loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.

Let F(X) equals to the number of lucky digits in decimal representation of X. Chef wants to know the number of such integers X, that L ≤ X ≤ R and F(X) is a lucky number. Help him and calculate that number modulo 109+7.

-----Input-----
First line contains one integer T, the number of test cases. Each of the following T lines contains two space separated positive integers L and R.

-----Output-----
For each of the T test cases print one integer, the number of such X, that L ≤ X ≤ R and F(X) is a lucky number, modulo 1000000007.

-----Constraints-----

1 ≤ T ≤ 10

1 ≤ L ≤ R ≤ 101000

-----Example-----
Input:
4
1 100
1 10000
1 100000
4444 4447

Output:
0
16
640
2

-----Notes-----
First test case: of course, any number of less than 4 digits can't contain lucky number of lucky digits, so the answer is 0.

Second test case: 16 required numbers are 4444 4447 4474 4477 4744 4747 4774 4777 7444 7447 7474 7477 7744 7747 7774 7777.

Third test case: there are 640 required lucky numbers. Some of them are 4474, 14747, 41474, 77277, 44407, 74749.

Fourth test case: the only two required numbers are 4444 and 4447.

I tried it in Python, but could not do it. Can you solve it?
assistant
#Here is Python program to print one integer, the number of such X, that L ≤ X ≤ R and F(X) is a lucky number, modulo 1000000007.

lucky = {4, 774, 7, 744, 777, 74, 747, 44, 77, 47, 474, 444, 477, 447}
from functools import lru_cache
import sys 
sys.setrecursionlimit(10 ** 6)
mod = 10 ** 9 + 7
fact = [1]
for i in range(1, 1001):
    fact.append(fact[-1] * i % mod)
inv = [pow(i, mod-2, mod) for i in fact]
C = lambda k, n: fact[n] * inv[n-k] * inv[k] % mod
def f(n):
    n = [int(x) for x in n]
    @lru_cache(None)
    def dp(pos, cnt, free):
        if cnt > 777:
            return 0
        diff = len(n) - pos 
        ans = 0
        if free:
            for i in lucky:
                i -= cnt
                if 0 <= i <= diff:
                    ans += C(i, diff) * pow(2, i, mod) * pow(8, diff - i, mod)
                    ans %= mod 
            return ans
        if pos == len(n):
            return int(cnt in lucky)
        for i in range(10 if free else n[pos]+1):
            ans += dp(pos+1, cnt + int(i == 4 or i == 7), free or i < n[pos])
            ans %= mod 
        return ans 
    return dp(0, 0, 0)
    
t = int(input())
for _ in range(t):
    l, r = input().split()
    l = str(int(l) -1) 
    print((f(r) - f(l)) % mod)