<p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>

<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>

<ul>
	<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
	<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>

<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>

<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>

<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>

<p><strong>Output:</strong> <span class="example-io">27</span></p>

<p><strong>Explanation:</strong></p>

<p><em>Some</em> of the good integers are:</p>

<ul>
	<li>551 because it can be rearranged to form 515.</li>
	<li>525 because it is already k-palindromic.</li>
</ul>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>

<p><strong>Output:</strong> <span class="example-io">2</span></p>

<p><strong>Explanation:</strong></p>

<p>The two good integers are 4 and 8.</p>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>

<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 10</code></li>
	<li><code>1 &lt;= k &lt;= 9</code></li>
</ul>

class Solution:
    def countGoodIntegers(self, n: int, k: int) -> int:
        vals = []
        kk=n-1
        i=0
        sset=set()
        while i<kk:
            vals.append(1*(10**i)+1*(10**kk))
            i+=1
            kk-=1
        if i==kk:
            mid = 10**i
            mid%=k
        mods = [v%k for v in vals]
        self.ans=0
        fct=[1]
        def fact(i):
            if len(fct)>i:
                return fct[i]
            while len(fct)<=i:
                fct.append(fct[-1]*len(fct))
            return fct[i]
        def addsum(ct):
            # print(ct,self.ans,end=" ")
            if tuple(ct) in sset:
                # print(self.ans)
                return
            sset.add(tuple(ct))
            tval=1
            for v in ct:
                tval*=fact(v)
            tval=fact(n)//tval
            if ct[0]==0:
                self.ans+=tval
                # print(self.ans)
                return
            zval = 1
            ct[0]-=1
            for v in ct:
                zval*=fact(v)
            ct[0]+=1
            zval = fact(n-1)//zval
            self.ans += tval-zval
            # print(self.ans)
            
        def kpal(i,ms,ct):
            # print(i,ms,ct)
            if i==len(mods):
                if n%2==0:
                    if ms==0:
                        addsum(ct)
                    return
                else:
                    for ii in range(10):
                        mmod = (mid*ii)%k
                        if (mmod+ms)%k==0:
                            ct[ii]+=1
                            addsum(ct)
                            ct[ii]-=1
                    return
            s=0
            if i==0:
                s=1
            modval = mods[i]
            for cnum in range(s,10):
                mms = (ms+modval*cnum)%k
                ct[cnum]+=2
                kpal(i+1, mms, ct)
                ct[cnum]-=2
        kpal(0, 0, [0]*10)
        return self.ans
21958