http://codeforces.com/problemset/problem/145/C
DP + Combinatorial Number + Modular Inverse
Calculate modular inverse: method1) extGCD; method2) Euler's theorem + PowerMod
#include <cstdio>
#include <functional>
#include <algorithm>
using namespace std;
const int MAX=100010;
const int MOD=1000000007;
int a[MAX],dp[MAX];
inline bool lucky(int n)
{
while(n>0)
{
if(n%10!=4 && n%10!=7)
return false;
n/=10;
}
return true;
}
inline int power(int n,int k)
{
if(k==0)
return 1;
long long a=power(n,k>>1);
if(k&1)
return n*a%MOD*a%MOD;
else
return a*a%MOD;
}
inline int inv(int n)
{
return power(n,MOD-2);
}
inline int extgcd(int a,int b,int &x,int &y)
{
int t,ret;
if(!b)
{
x=1,y=0;
return a;
}
ret=extgcd(b,a%b,x,y);
t=x;
x=y;
y=t-a/b*y;
return ret;
}
inline int inverse(int n)
{
int x,y;
extgcd(n,MOD,x,y);
x%=MOD;
return (x>=0)?x:x+MOD;
}
int main()
{
int n,m,t;
scanf("%d%d",&n,&t);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
m=0;
for(int i=0;i<n;i++,m++)
{
if(lucky(a[i]))
{
int k=i+1;
while(k<n && a[k]==a[i])
k++;
a[m]=k-i;
i=k-1;
}
else
a[m]=1;
}
sort(a,a+m,greater<int>());
int num=0,one;
while(a[num]>1 && num<m)
num++;
one=m-num;
dp[0]=1;
for(int i=0;i<num;i++)
for(int j=min(i+1,t);j>0;j--)
dp[j]=(dp[j]+(long long)a[i]*dp[j-1])%MOD;
int ans=dp[t],comb=1;
for(int i=1;i<=t && i<=one;i++)
{
comb=(long long)(one-i+1)*inverse(i)%MOD*comb%MOD;
ans=(ans+(long long)dp[t-i]*comb)%MOD;
}
printf("%d\n",ans);
return 0;
}