Problem:
問題名
今
n
枚の数字が書かれたカードがあります。これらの一部または全部を適当に並べて数字を作ることを考えます。この時作られる数字を全て足した数を求めて下さい。
例えば、 1 と 2 があったら、作られる数字は 1, 2, 12, 21 の 4 つなので、全て足した数は 36 になります。並べた結果同じ数字が出来ても違う並べ方だとしたら別々に足します。たとえば、 1 というカードと 11 というカードがあったら並べて 111 になる並べ方が2通りありますがそれぞれ別のものとして足し合わせます。カードの中にリーディングゼロのカードはありませんし、リーディングゼロになる数字は認めません。答えを1,000,000,007 で割ったものを出力してください。
Input
入力は、以下の形で与えられます。
n
a
1
a
2
...
a
n
最初の 1 行にはカードの枚数を表す
n
（
1 ≤ n ≤ 200
）、次の
n
行にはそれぞれのカードに書かれている数字
a
i
(
0 ≤ a
i
< 10000
) が書かれています。また複数のカードに同じ数字が書かれていることはありません。
Output
作ることの出来る全ての数字の合計を 1,000,000,007 で割ったものを 1 行に出力しなさい。
Sample Input 1
2
1
2
Output for the Sample Input 1
36
サンプルにあった例です。
Sample Input 2
2
1
11
Output for the Sample Input 2
234
作られる数字は 1 と 11 と、 111 が 2 通り作られるので全て足して 234 となります。
Sample Input 3
4
0
4
7
8
Output for the Sample Input 3
135299
04 や 078 といった並べ方は認められないことに注意してください。

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;

int dp[202][202][802];

long long solve(vector<string> S) {
	if (S.size() == 0) return 0;
	vector<int> digsum(6, 0);
	for (int i = 0; i < S.size(); i++) {
		digsum[S[i].size()] += stoi(S[i]);
	}

	// init
	vector<long long> fact(S.size() + 2, 0);
	vector<long long> pow10(5 * S.size() + 2, 0);
	vector<vector<long long>> nr(S.size() + 2, vector<long long>(S.size() + 2, 0));
	fact[0] = 1;
	pow10[0] = 1;
	for (int i = 1; i <= S.size() * 1; i++) fact[i] = (1LL * i * fact[i - 1]) % mod;
	for (int i = 1; i <= S.size() * 5; i++) pow10[i] = (10LL * pow10[i - 1]) % mod;
	nr[0][0] = 1;
	for (int i = 0; i < S.size(); i++) {
		for (int j = 0; j < S.size(); j++) {
			nr[i + 1][j + 0] += nr[i][j];
			nr[i + 1][j + 0] %= mod;
			nr[i + 1][j + 1] += nr[i][j];
			nr[i + 1][j + 1] %= mod;
		}
	}

	// brute force
	long long ans = 0;
	for (int d = 1; d <= 5; d++) {
		for (int i = 0; i <= S.size(); i++) {
			for (int j = 0; j <= S.size(); j++) {
				for (int k = 0; k <= 4 * S.size() + 1; k++) dp[i][j][k] = 0;
			}
		}
		bool flag = false;
		dp[0][0][0] = 1;
		for (int i = 0; i < S.size(); i++) {
			bool special = ((flag == true || S[i].size() != d) ? true : false);
			for (int j = 0; j < S.size(); j++) {
				for (int k = 0; k <= S.size() * 4; k++) {
					if (dp[i][j][k] == 0) continue;
					dp[i + 1][j][k] += dp[i][j][k];
					dp[i + 1][j][k] %= mod;
					if (special == true) {
						dp[i + 1][j + 1][k + S[i].size()] += dp[i][j][k];
						dp[i + 1][j + 1][k + S[i].size()] %= mod;
					}
				}
			}
			if (special == false) flag = true;
		}

		// calculate
		for (int i = 0; i <= S.size() - 1; i++) {
			for (int j = 0; j <= S.size() * 4 + 1; j++) {
				for (int k = i; k <= S.size() - 1; k++) {
					long long ways = fact[i] * fact[k - i] % mod;
					ways = ways * dp[S.size()][i][j] % mod;
					ways = ways * nr[S.size() - 1 - i][k - i] % mod;
					long long score = pow10[j] * digsum[d] % mod;
					if (ways * score >= 10000) {
						ways += 1;
						ways -= 1;
					}
					ans += ways * score;
					ans %= mod;
				}
			}
		}
	}
	return ans;
}

int main() {
	// step 1. input
	int N; cin >> N;
	vector<string> S1;
	vector<string> S2;
	for (int i = 0; i < N; i++) {
		string r; cin >> r;
		S1.push_back(r);
		if (r != "0") S2.push_back(r);
	}

	// step 2. solve
	long long ans = solve(S1);
	if (S1.size() != S2.size()) ans = (ans - solve(S2) + mod) % mod;
	cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 1120
memory_kb: 130768
final_rank: 18

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ll = long long;
#define rep(i,n) for(int i=0;i<n;i++)
#define rep2(i,s,t) for(int i=s;i<t;i++)

const ll mod=1e9+7;

ll modpow(ll x, ll k, ll m = mod) {
  ll v = 1;
  while(k) {
    if(k & 1) v = v * x % m;
    x = x * x % m;
    k >>= 1;
  }
  return v;
}

using vll=vector<ll>;
int len=2000;
vll fac, finv;
vll TENPOW;
vll SUMP;

void precalc(ll m = mod) {
  fac.assign(len, 1), finv.assign(len, 1);
  TENPOW.assign(len,1);

  rep(i,len-1)TENPOW[i+1]=(TENPOW[i]*10)%mod;
  rep2(i, 2, len) fac[i] = fac[i - 1] * i % m;
  finv[len - 1] = modpow(fac[len - 1], m - 2, m);
  for(int i = len - 2; i >= 0; i--) finv[i] = finv[i + 1] * (i + 1) % m;
  SUMP.assign(len,0);
  rep(z,len)rep(j,z+1){
    SUMP[z]+=fac[z]*finv[z-j];
    SUMP[z]%=mod;
  }
}

ll binom(ll n, ll k, ll m = mod) {
  if(n < 0 || k < 0 || k > n) return 0;
  return fac[n] * finv[k] % m * finv[n - k] % m;
}

ll calc(vll A){
    vll num(4,0);
    int N=A.size();
    for(ll a:A){
        int bs=0;
        while(a>9){
            a/=10;
            bs++;
        }
        num[bs]++;
    }
    vector<vector<vector<ll>>> DP(4,vector<vector<ll>>(N,vector<ll>(N*4+1,0)));
    rep(i,4){
        if(num[i]==0)continue;
        num[i]--;
        vector<vector<ll>> F(N+1,vector<ll>(N*4+1,0));
        F[0][0]=1;
        rep(j,4){
            vector<vll> NF(N+1,vector<ll>(N*4+1,0));
            rep(k,num[j]+1){
                rep(n,N+1-k)rep(m,n*4+1){
                    if(m+(j+1)*k<=4*N&&n+k<=N)
                    NF[n+k][m+(j+1)*k]+=F[n][m]*binom(num[j],k);
                    NF[n+k][m+(j+1)*k]%=mod;
                }
            }
            swap(F,NF);
        }
        DP[i]=F;
        num[i]++;
    }
    ll an=0;
    for(ll a:A){
        int bs=0;
        ll ca=a;
        while(a>9){
            a/=10;
            bs++;
        }
        rep(n,N+1)rep(m,N*4+1){
            ll res=DP[bs][n][m]*TENPOW[m];
            res%=mod;
            res*=ca;
            res%=mod;
            res*=fac[n];
            res%=mod;
            ll z=(N-n-1);
            if(z<0)continue;
            
            res*=SUMP[z];
            res%=mod;
            an+=res;
            an%=mod;
            assert(res>=0);

        }
    }
    return an;
}

int main() {

    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    precalc();
    int N=200;
    cin>>N;
    vector<ll> A(N);
    rep(i,N)cin>>A[i];
    sort(A.begin(),A.end());
    reverse(A.begin(),A.end());
    ll an=calc(A);
    if(A.back()==0){
        A.pop_back();
        an-=calc(A);
    }
    an=(an%mod+mod)%mod;
    cout<<an<<endl;
}
accuracy: 1
time_ms: 1850
memory_kb: 10772
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ll = long long;
#define rep(i,n) for(int i=0;i<n;i++)
#define rep2(i,s,t) for(int i=s;i<t;i++)

/*
- `len` を指定したうえで `precalc()` でテーブルを前計算．`len <= mod`
である必要がある
- `binom(n, k)` は `n < len` を満たす必要がある
- `mod` が固定のときは `const ll mod = 998244353;` のようにする
- `a < len` に対して $O(1)$ で逆元が求められる．
*/
const ll mod=1e9+7;

// 注意： % m を % mod と書かないように！
ll modpow(ll x, ll k, ll m = mod) {
  ll v = 1;
  while(k) {
    if(k & 1) v = v * x % m;
    x = x * x % m;
    k >>= 1;
  }
  return v;
}

using vll=vector<ll>;
int len=2000;
vll fac, finv;
vll TENPOW;
vll SUMP;

void precalc(ll m = mod) {
  fac.assign(len, 1), finv.assign(len, 1);
  TENPOW.assign(len,1);

  rep(i,len-1)TENPOW[i+1]=(TENPOW[i]*10)%mod;
  rep2(i, 2, len) fac[i] = fac[i - 1] * i % m;
  finv[len - 1] = modpow(fac[len - 1], m - 2, m);
  for(int i = len - 2; i >= 0; i--) finv[i] = finv[i + 1] * (i + 1) % m;
  SUMP.assign(len,0);
  rep(z,len)rep(j,z+1){
    SUMP[z]+=fac[z]*finv[z-j];
    SUMP[z]%=mod;
  }
}

ll binom(ll n, ll k, ll m = mod) {
  if(n < 0 || k < 0 || k > n) return 0;
  // assert(n < len);
  return fac[n] * finv[k] % m * finv[n - k] % m;
}

ll inv(ll a, ll m = mod) {
  // a = (a % m + m) % m; // 必要なら
  if(a < len) return fac[a - 1] * finv[a] % m;
  return modpow(a, m - 2, m);
}


ll calc(vll A){
    vll num(4,0);
    int N=A.size();
    for(ll a:A){
        int bs=0;
        while(a>9){
            a/=10;
            bs++;
        }
        num[bs]++;
    }
    vector<vector<vector<ll>>> DP(4,vector<vector<ll>>(N,vector<ll>(N*4+1,0)));
    rep(i,4){
        if(num[i]==0)continue;
        num[i]--;
        vector<vector<ll>> F(N+1,vector<ll>(N*4+1,0));
        F[0][0]=1;
        rep(j,4){
            vector<vll> NF(N+1,vector<ll>(N*4+1,0));
            rep(k,num[j]+1){
                rep(n,N+1-k)rep(m,n*4+1){
                    if(m+(j+1)*k<=4*N&&n+k<=N)
                    NF[n+k][m+(j+1)*k]+=F[n][m]*binom(num[j],k);
                    NF[n+k][m+(j+1)*k]%=mod;
                }
            }
            swap(F,NF);
        }
        DP[i]=F;
        num[i]++;
    }
    ll an=0;
    for(ll a:A){
        int bs=0;
        ll ca=a;
        while(a>9){
            a/=10;
            bs++;
        }
        rep(n,N+1)rep(m,N*4+1){
            ll res=DP[bs][n][m]*TENPOW[m];
            res%=mod;
            res*=ca;
            res%=mod;
            res*=fac[n];
            res%=mod;
            ll z=(N-n-1);
            if(z<0)continue;
            
            res*=SUMP[z];
            res%=mod;
            // if(res>0)cout<<ca<<" "<<n<<" "<<m<<endl;
            an+=res;
            an%=mod;
            assert(res>=0);
            // cout<<res<<" "<<an<<endl;

        }
    }
    return an;
}

int main() {

    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    precalc();
    int N=200;
    cin>>N;
    vector<ll> A(N);
    rep(i,N)cin>>A[i];
    // set<ll> S;
    // rep(i,N){
    //     A[i]=0;
    //     while(S.count(A[i]))A[i]=rand()%10000;
    //     S.insert(A[i]);
    // }
    sort(A.begin(),A.end());
    reverse(A.begin(),A.end());
    ll an=calc(A);
    if(A.back()==0){
        A.pop_back();
        an-=calc(A);
    }
    an=(an%mod+mod)%mod;
    cout<<an<<endl;
}
accuracy: 1
time_ms: 1850
memory_kb: 10716
final_rank: 13

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ll = long long;
#define rep(i,n) for(int i=0;i<n;i++)
#define rep2(i,s,t) for(int i=s;i<t;i++)

/*
- `len` を指定したうえで `precalc()` でテーブルを前計算．`len <= mod`
である必要がある
- `binom(n, k)` は `n < len` を満たす必要がある
- `mod` が固定のときは `const ll mod = 998244353;` のようにする
- `a < len` に対して $O(1)$ で逆元が求められる．
*/
const ll mod=1e9+7;

// 注意： % m を % mod と書かないように！
ll modpow(ll x, ll k, ll m = mod) {
  ll v = 1;
  while(k) {
    if(k & 1) v = v * x % m;
    x = x * x % m;
    k >>= 1;
  }
  return v;
}

using vll=vector<ll>;
int len=2000;
vll fac, finv;
vll TENPOW;
vll SUMP;

void precalc(ll m = mod) {
  fac.assign(len, 1), finv.assign(len, 1);
  TENPOW.assign(len,1);

  rep(i,len-1)TENPOW[i+1]=(TENPOW[i]*10)%mod;
  rep2(i, 2, len) fac[i] = fac[i - 1] * i % m;
  finv[len - 1] = modpow(fac[len - 1], m - 2, m);
  for(int i = len - 2; i >= 0; i--) finv[i] = finv[i + 1] * (i + 1) % m;
  SUMP.assign(len,0);
  rep(z,len)rep(j,z+1){
    SUMP[z]+=fac[z]*finv[z-j];
    SUMP[z]%=mod;
  }
}

ll binom(ll n, ll k, ll m = mod) {
  if(n < 0 || k < 0 || k > n) return 0;
  // assert(n < len);
  return fac[n] * finv[k] % m * finv[n - k] % m;
}

ll inv(ll a, ll m = mod) {
  // a = (a % m + m) % m; // 必要なら
  if(a < len) return fac[a - 1] * finv[a] % m;
  return modpow(a, m - 2, m);
}


ll calc(vll A){
    vll num(4,0);
    int N=A.size();
    for(ll a:A){
        int bs=0;
        while(a>9){
            a/=10;
            bs++;
        }
        num[bs]++;
    }
    vector<vector<vector<ll>>> DP(4,vector<vector<ll>>(N,vector<ll>(N*4+1,0)));
    rep(i,4){
        if(num[i]==0)continue;
        num[i]--;
        vector<vector<ll>> F(N+1,vector<ll>(N*4+1,0));
        F[0][0]=1;
        rep(j,4){
            vector<vll> NF(N+1,vector<ll>(N*4+1,0));
            rep(k,num[j]+1){
                rep(n,N+1-k)rep(m,n*4+1){
                    if(m+(j+1)*k<=4*N&&n+k<=N)
                    NF[n+k][m+(j+1)*k]+=F[n][m]*binom(num[j],k);
                }
            }
            swap(F,NF);
        }
        DP[i]=F;
        num[i]++;
    }
    ll an=0;
    for(ll a:A){
        int bs=0;
        ll ca=a;
        while(a>9){
            a/=10;
            bs++;
        }
        rep(n,N+1)rep(m,N*4+1){
            ll res=DP[bs][n][m]*TENPOW[m];
            res%=mod;
            res*=ca;
            res%=mod;
            res*=fac[n];
            res%=mod;
            ll z=(N-n-1);
            if(z<0)continue;
            
            res*=SUMP[z];
            res%=mod;
            // if(res>0)cout<<ca<<" "<<n<<" "<<m<<endl;
            an+=res;
            an%=mod;

        }
    }
    return an;
}

int main() {

    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    precalc();
    int N=200;
    cin>>N;
    vector<ll> A(N);
    rep(i,N)cin>>A[i];
    // set<ll> S;
    // rep(i,N){
    //     A[i]=0;
    //     while(S.count(A[i]))A[i]=rand()%10000;
    //     S.insert(A[i]);
    // }
    sort(A.begin(),A.end());
    reverse(A.begin(),A.end());
    ll an=calc(A);
    if(A.back()==0){
        A.pop_back();
        an-=calc(A);
    }
    an=(an%mod+mod)%mod;
    cout<<an<<endl;
}
accuracy: 0.5
time_ms: 30
memory_kb: 3736
final_rank: 19

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

template <unsigned mod = 1000000007> struct fp {
    unsigned v;
    static constexpr int get_mod() {
        return mod;
    }
    constexpr unsigned inv() const {
        assert(v != 0);
        int x = v, y = mod, p = 1, q = 0, t = 0, tmp = 0;
        while (y > 0) {
            t = x / y;
            x -= t * y, p -= t * q;
            tmp = x, x = y, y = tmp;
            tmp = p, p = q, q = tmp;
        }
        if (p < 0)
            p += mod;
        return p;
    }
    constexpr fp(ll x = 0) : v(x >= 0 ? x % mod : (mod - (-x) % mod) % mod) {}
    fp operator-() const {
        return fp() - *this;
    }
    fp pow(ull t) {
        fp res = 1, b = *this;
        while (t) {
            if (t & 1)
                res *= b;
            b *= b;
            t >>= 1;
        }
        return res;
    }
    fp &operator+=(const fp &x) {
        if ((v += x.v) >= mod)
            v -= mod;
        return *this;
    }
    fp &operator-=(const fp &x) {
        if ((v += mod - x.v) >= mod)
            v -= mod;
        return *this;
    }
    fp &operator*=(const fp &x) {
        v = ull(v) * x.v % mod;
        return *this;
    }
    fp &operator/=(const fp &x) {
        v = ull(v) * x.inv() % mod;
        return *this;
    }
    fp operator+(const fp &x) const {
        return fp(*this) += x;
    }
    fp operator-(const fp &x) const {
        return fp(*this) -= x;
    }
    fp operator*(const fp &x) const {
        return fp(*this) *= x;
    }
    fp operator/(const fp &x) const {
        return fp(*this) /= x;
    }
    bool operator==(const fp &x) const {
        return v == x.v;
    }
    bool operator!=(const fp &x) const {
        return v != x.v;
    }
    friend istream &operator>>(istream &is, fp &x) {
        return is >> x.v;
    }
    friend ostream &operator<<(ostream &os, const fp &x) {
        return os << x.v;
    }
};

template <typename T> T Inv(ll n) {
    static const int md = T::get_mod();
    static vector<T> buf({0, 1});
    assert(n > 0);
    n %= md;
    while (SZ(buf) <= n) {
        int k = SZ(buf), q = (md + k - 1) / k;
        buf.push_back(buf[k * q - md] * q);
    }
    return buf[n];
}

template <typename T> T Fact(ll n, bool inv = 0) {
    static const int md = T::get_mod();
    static vector<T> buf({1, 1}), ibuf({1, 1});
    assert(n >= 0 and n < md);
    while (SZ(buf) <= n) {
        buf.push_back(buf.back() * SZ(buf));
        ibuf.push_back(ibuf.back() * Inv<T>(SZ(ibuf)));
    }
    return inv ? ibuf[n] : buf[n];
}

template <typename T> T nPr(int n, int r, bool inv = 0) {
    if (n < 0 || n < r || r < 0)
        return 0;
    return Fact<T>(n, inv) * Fact<T>(n - r, inv ^ 1);
}
template <typename T> T nCr(int n, int r, bool inv = 0) {
    if (n < 0 || n < r || r < 0)
        return 0;
    return Fact<T>(n, inv) * Fact<T>(r, inv ^ 1) * Fact<T>(n - r, inv ^ 1);
}
template <typename T> T nHr(int n, int r, bool inv = 0) {
    return nCr<T>(n + r - 1, r, inv);
}

/**
 * @brief Modint
 */

using Fp = fp<1000000007>;
int MAXS = 10000, MAXL = 800, MAXN = 200;

Fp Calc(vector<ll> COUNT) {
    vector<Fp> D2(MAXN+1), D10(MAXL+1);
    D10[0] = 1;
    rep(i,0,MAXN+1) {
        D2[i] = 0;
        rep(j,0,i+1) {
            D2[i] += nCr<Fp>(i,j) * Fact<Fp>(j);
        }
    }
    rep(i,0,MAXL) D10[i+1] = D10[i] * 10;
    Fp Ret = 0;
    rep(i,0,COUNT[1]+1) {
        rep(j,0,COUNT[2]+1) {
            rep(k,0,COUNT[3]+1) {
                rep(l,0,COUNT[4]+1) {
                    Fp Mul = 1;
                    Mul *= nCr<Fp>(COUNT[1],i);
                    Mul *= nCr<Fp>(COUNT[2],j);
                    Mul *= nCr<Fp>(COUNT[3],k);
                    Mul *= nCr<Fp>(COUNT[4],l);
                    Mul *= nCr<Fp>(i+j,j);
                    Mul *= nCr<Fp>(i+j+k,k);
                    Mul *= nCr<Fp>(i+j+k+l,l);
                    Mul *= Fact<Fp>(i);
                    Mul *= Fact<Fp>(j);
                    Mul *= Fact<Fp>(k);
                    Mul *= Fact<Fp>(l);
                    Mul *= D2[COUNT[1]+COUNT[2]+COUNT[3]+COUNT[4]-i-j-k-l];
                    Mul *= D10[i+j*2+k*3+l*4];
                    Ret += Mul;
                }
            }
        }
    }
    return Ret;
    // vector<int> Len;
    // rep(i,1,5) {
    //     rep(j,0,COUNT[i]) Len.push_back(i);
    // }
    // int N = Len.size();
    // vector DP(N+1, vector(N+1, vector<Fp>(MAXL+1,0)));
    // DP[0][0][0] = 1;
    // rep(k,0,N) {
    //     rep(i,0,k+1) {
    //         int j = k-i;
    //         rep(m,0,MAXL+1) {
    //             if (DP[i][j][m] == 0) continue;
    //             DP[i+1][j][m] += DP[i][j][m] * (i+1);
    //             DP[i][j+1][m+Len[k]] += DP[i][j][m] * (j+1);
    //         }
    //     }
    // }
    // Fp Ret = 0, D = 1;
    // rep(k,0,MAXL+1) {
    //     rep(i,0,N+1) {
    //         int j = N-i;
    //         Ret += DP[i][j][k] * D;
    //     }
    //     D *= 10;
    // }
    // return Ret;
}

Fp Solve(vector<bool> X) {
    vector<ll> COUNT(5,0), SUM(5,0);
    rep(i,0,MAXS) {
        if (!X[i]) continue;
        int Len = to_string(i).size();
        COUNT[Len]++;
        SUM[Len] += i;
    }
    Fp Ret = 0;
    rep(i,1,5) {
        if (COUNT[i] == 0) continue;
        COUNT[i]--;
        Ret += Calc(COUNT) * SUM[i];
        COUNT[i]++;
    }
    return Ret;
}

int main() {
    int N;
    cin >> N;
    vector<bool> X(MAXS,false);
    rep(i,0,N) {
        int A;
        cin >> A;
        X[A] = true;
    }
    if (N == 1 && X[0]) {
        cout << 0 << endl;
        return 0;
    }
    Fp ANS = Solve(X);
    if (X[0]) {
        X[0] = false;
        ANS -= Solve(X);
    }
    cout << ANS << endl;
}
accuracy: 1
time_ms: 1830
memory_kb: 3524
final_rank: 12

code_snippet: #include <string.h>
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

constexpr int mod = 1000000007;

void mpl(int &x,int y) {
    x += y;
    if(x >= mod) x -= mod;
}

int dp[202][202][202][2][2];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<int>a(n),b(5,1);
    for(int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for(int i = 1; i < 5; i++) b[i] = b[i-1]*10;
    dp[0][0][0][0][1] = 1;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            for(int k = 0; k < n; k++) {
                for(int f1 = 0; f1 < 2; f1++) {
                    for(int f2 = 0; f2 < 2; f2++) {
                        if(!dp[i][j][k][f1][f2]) continue;
                        mpl(dp[i+1][j+1][k][f1][f2],1ll*dp[i][j][k][f1][f2]*(j+1)*b[to_string(a[i]).size()]%mod);
                        if(a[i] == 0) {
                            mpl(dp[i+1][j][k+1][f1][f2],1ll*dp[i][j][k][f1][f2]*k%mod);
                            mpl(dp[i+1][j][k+1][f1][0],dp[i][j][k][f1][f2]);
                        }
                        else {
                            mpl(dp[i+1][j][k+1][f1][f2],1ll*dp[i][j][k][f1][f2]*k%mod);
                            mpl(dp[i+1][j][k+1][f1][1],dp[i][j][k][f1][f2]);
                        }
                        if(f1 == 0) mpl(dp[i+1][j][k][1][f2],1ll*dp[i][j][k][f1][f2]*a[i]%mod);
                        mpl(dp[i+1][j][k][f1][f2],dp[i][j][k][f1][f2]);
                    }
                }
            }
        }
    }
    int ans = 0;
    for(int i = 0; i <= n; i++) {
        for(int j = 0; j <= n; j++) {
            mpl(ans,dp[n][i][j][1][1]);
        }
    }
    cout << ans << "\n";
}
accuracy: 1
time_ms: 320
memory_kb: 129544
final_rank: 17

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

constexpr ll mod = 1e9+7;

ll mod_pow(ll a,ll b){
    a%=mod;
    if(b==0)return 1;
    if(b==1)return a;
    ll res=mod_pow(a,b/2)%mod;
    res*=res; res%=mod;
    if(b%2)res*=a;
    return res%mod;
}

struct perm{
    vector<ll> p,invp;
    perm(int n){
        p.resize(n+1),invp.resize(n+1);
        p[0]=1;
        for(int i=1;i<=n;i++){
            p[i]=p[i-1]*i%mod;
        }
        invp[n]=mod_pow(p[n],mod-2);
        for(int i=n-1;i>=0;i--){
            invp[i]=invp[i+1]*(i+1)%mod;
        }
    }
    ll comb(ll x,ll y){
        if(x<y||y<0)return 0;
        return (p[x]*invp[x-y]%mod)*invp[y]%mod;
    }
};
perm p(1<<20);

const int N = 201;
ll P[N][N];

ll solve(vector<pair<int,int>> a){
    ll res = 0;
    sort(a.begin(), a.end());
    int n = a.size();
    for(int i=0;i<n;i++){
        ll dsum = 0;
        {
            int j = i;
            while(i<n and a[i].second == a[j].second){
                dsum += a[i].first;
                i++;
            }
            i--;
        }
        int m = 0;
        vector<int> cnt(6);
        for(int j=0;j<n;j++){
            if(i==j) continue;
            cnt[a[j].second]++;
            m += a[j].second;
        }
        vector<vector<ll>> dp(n,vector<ll>(m+1));
        dp[0][0] = 1;
        // 何個使ったか　iを使う前に何桁あるか
        for(int j=1;j<=5;j++){
            for(int k=n-1;k>=0;k--){
                for(int l=m;l>=0;l--){
                    if(!dp[k][l]) continue;
                    for(int c=1;c<=cnt[j];c++){
                        ll ad = p.comb(cnt[j], c)*dp[k][l]%mod;
                        ad *= p.comb(k+c, c);
                        ad %= mod;
                        ad *= p.p[c];
                        ad %= mod;
                        dp[k+c][l+c*j] += ad;
                        if(dp[k+c][l+c*j] >= mod) dp[k+c][l+c*j] -= mod;
                    }
                }
            }
        }
        for(int j=0;j<n;j++){
            ll ten = 1;
            for(int k=0;k<=m;k++){
                /*
                 * n-1-j枚ある
                 * 0枚選んで並べる通り数
                 * 1枚選んで並べる通り数
                 * n-1-j枚選んで並べる通り数
                 */
                ll ad = dsum * dp[j][k] % mod * P[n-1-j][n-1-j] % mod * ten % mod;
                res += ad;
//                if(dp[j][k])cout << a[i].second << " " << P[n-1-j][n-1-j] << " " << dp[j][k] << " " << j << " " << k << " " << ad << endl;
                ten = ten * 10 % mod;
            }
        }
    }
    return res%mod;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    for(int i=0;i<N;i++){
        ll u = 1;
        for(int j=0;j<=i;j++){
            P[i][j] = u;
            u *= (i-j); u %= mod;
            if(j){
                P[i][j] += P[i][j-1];
                if(P[i][j] >= mod) P[i][j] -= mod;
            }
        }
    }
    int n; cin >> n;
    vector<pair<int,int>> a(n);
    for(int i=0;i<n;i++){
        cin >> a[i].first;
        a[i].second = to_string(a[i].first).size();
    }
    sort(a.rbegin(), a.rend());
    ll res = solve(a);
    if(a.back().first == 0){
        a.pop_back();
        res -= solve(a);
        if(res < 0) res += mod;
    }
    cout << res << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 20912
final_rank: 2

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

constexpr ll mod = 1e9+7;

ll mod_pow(ll a,ll b){
    a%=mod;
    if(b==0)return 1;
    if(b==1)return a;
    ll res=mod_pow(a,b/2)%mod;
    res*=res; res%=mod;
    if(b%2)res*=a;
    return res%mod;
}

struct perm{
    vector<ll> p,invp;
    perm(int n){
        p.resize(n+1),invp.resize(n+1);
        p[0]=1;
        for(int i=1;i<=n;i++){
            p[i]=p[i-1]*i%mod;
        }
        invp[n]=mod_pow(p[n],mod-2);
        for(int i=n-1;i>=0;i--){
            invp[i]=invp[i+1]*(i+1)%mod;
        }
    }
    ll comb(ll x,ll y){
        if(x<y||y<0)return 0;
        return (p[x]*invp[x-y]%mod)*invp[y]%mod;
    }
};
perm p(1<<20);

const int N = 201;
ll P[N][N];

ll solve(vector<pair<int,int>> a){
    ll res = 0;
    sort(a.begin(), a.end());
    int n = a.size();
    for(int i=0;i<n;i++){
        ll dsum = 0;
        {
            int j = i;
            while(i<n and a[i].second == a[j].second){
                dsum += a[i].first;
                i++;
            }
            i--;
        }
        int m = 0;
        vector<int> cnt(6);
        for(int j=0;j<n;j++){
            if(i==j) continue;
            cnt[a[j].second]++;
            m += a[j].second;
        }
        vector<vector<ll>> dp(n,vector<ll>(m+1));
        dp[0][0] = 1;
        // 何個使ったか　iを使う前に何桁あるか
        for(int j=1;j<=5;j++){
            for(int k=n-1;k>=0;k--){
                for(int l=m;l>=0;l--){
                    if(!dp[k][l]) continue;
                    for(int c=1;c<=cnt[j];c++){
                        ll ad = p.comb(cnt[j], c)*dp[k][l]%mod;
                        ad *= p.comb(k+c, c);
                        ad %= mod;
                        ad *= p.p[c];
                        ad %= mod;
                        dp[k+c][l+c*j] += ad;
                        if(dp[k+c][l+c*j] >= mod) dp[k+c][l+c*j] -= mod;
                    }
                }
            }
        }
        for(int j=0;j<n;j++){
            ll ten = 1;
            for(int k=0;k<=m;k++){
                /*
                 * n-1-j枚ある
                 * 0枚選んで並べる通り数
                 * 1枚選んで並べる通り数
                 * n-1-j枚選んで並べる通り数
                 */
                ll ad = dsum * dp[j][k] % mod * P[n-1-j][n-1-j] % mod * ten % mod;
                res += ad;
//                if(dp[j][k])cout << a[i].second << " " << P[n-1-j][n-1-j] << " " << dp[j][k] << " " << j << " " << k << " " << ad << endl;
                ten = ten * 10 % mod;
            }
        }
    }
    return res%mod;
}

/*
 * 60666 60600
 * 66066 66000
 * 66606 66600
 * 66660 66060
 *
 *   666 600
 *  6066 6000
 *  6606 6600
 *  6660 6060
 *
 *   303 300
 *   330 300
 *    66 00
 *
 *    13 10
 */


ll gutyoku(vector<pair<int,int>> a){
    int n = a.size();
    ll res = 0;
    map<int,int> mp;
    for(int i=1;i<(1<<n);i++){
        vector<pair<pair<int,int>,int>> v;
        for(int j=0;j<n;j++){
            if((1<<j)&i) v.push_back({a[j],j});
        }
        sort(v.begin() , v.end());
        do{
            if(v[0].first.first == 0) continue;
            ll sum = 0;
            for(auto p:v){
                for(int j=0;j<p.first.second;j++){
                    sum *= 10; sum %= mod;
                }
                sum += p.first.first;
            }
            mp[sum]++;
            res += sum;
        }while(next_permutation(v.begin(), v.end()));
    }
    for(auto p:mp){
        cout << p.first << " " << p.second << endl;
    }
    return res%mod;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    for(int i=0;i<N;i++){
        ll u = 1;
        for(int j=0;j<=i;j++){
            P[i][j] = u;
            u *= (i-j); u %= mod;
            if(j){
                P[i][j] += P[i][j-1];
                if(P[i][j] >= mod) P[i][j] -= mod;
            }
        }
    }
    int n; cin >> n;
    vector<pair<int,int>> a(n);
    for(int i=0;i<n;i++){
        cin >> a[i].first;
        a[i].second = to_string(a[i].first).size();
    }
    sort(a.rbegin(), a.rend());
//    cerr << gutyoku(a) << endl;
    ll res = solve(a);
    if(a.back().first == 0){
        a.pop_back();
        res -= solve(a);
        if(res < 0) res += mod;
    }
    cout << res << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 21004
final_rank: 3

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

constexpr ll mod = 1e9+7;

ll mod_pow(ll a,ll b){
    a%=mod;
    if(b==0)return 1;
    if(b==1)return a;
    ll res=mod_pow(a,b/2)%mod;
    res*=res; res%=mod;
    if(b%2)res*=a;
    return res%mod;
}

struct perm{
    vector<ll> p,invp;
    perm(int n){
        p.resize(n+1),invp.resize(n+1);
        p[0]=1;
        for(int i=1;i<=n;i++){
            p[i]=p[i-1]*i%mod;
        }
        invp[n]=mod_pow(p[n],mod-2);
        for(int i=n-1;i>=0;i--){
            invp[i]=invp[i+1]*(i+1)%mod;
        }
    }
    ll comb(ll x,ll y){
        if(x<y||y<0)return 0;
        return (p[x]*invp[x-y]%mod)*invp[y]%mod;
    }
};
perm p(1<<20);

const int N = 201;
ll P[N][N];

ll solve(vector<pair<int,int>> a){
    ll res = 0;
    sort(a.begin(), a.end());
    int n = a.size();
    for(int i=0;i<n;i++){
        ll dsum = 0;
        {
            int j = i;
            while(i<n and a[i].second == a[j].second){
                dsum += a[i].first;
                i++;
            }
            i--;
        }
        int m = 0;
        vector<int> cnt(6);
        for(int j=0;j<n;j++){
            if(i==j) continue;
            cnt[a[j].second]++;
            m += a[j].second;
        }
        vector<vector<ll>> dp(n,vector<ll>(m+1));
        dp[0][0] = 1;
        for(int j=1;j<=5;j++){
            for(int k=n-1;k>=0;k--){
                for(int l=m;l>=0;l--){
                    if(!dp[k][l]) continue;
                    for(int c=1;c<=cnt[j];c++){
                        ll ad = p.comb(cnt[j], c);
                        ad *= p.comb(k+c, c);
                        ad %= mod;
                        ad *= p.p[c];
                        ad %= mod;
                        dp[k+c][l+c*j] += ad;
                        if(dp[k+c][l+c*j] >= mod) dp[k+c][l+c*j] -= mod;
                    }
                }
            }
        }
        for(int j=0;j<n;j++){
            ll ten = 1;
            for(int k=0;k<=m;k++){
                res += dsum * dp[j][k] % mod * P[n-1-j][n-1-j] % mod * ten % mod;
                ten = ten * 10 % mod;
            }
        }
    }
    return res%mod;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    for(int i=0;i<N;i++){
        ll u = 1;
        for(int j=0;j<=i;j++){
            P[i][j] = u;
            u *= (i-j); u %= mod;
            if(j){
                P[i][j] += P[i][j-1];
                if(P[i][j] >= mod) P[i][j] -= mod;
            }
        }
    }
    int n; cin >> n;
    vector<pair<int,int>> a(n);
    for(int i=0;i<n;i++){
        cin >> a[i].first;
        a[i].second = to_string(a[i].first).size();
    }
    sort(a.rbegin(), a.rend());
    ll res = solve(a);
    if(a.back().first == 0){
        a.pop_back();
        res -= solve(a);
        if(res < 0) res += mod;
    }
    cout << res << endl;
}
accuracy: 0.10526315789473684
time_ms: 10
memory_kb: 19728
final_rank: 20

code_snippet: /**
 *    author:  otera
**/
#include<bits/stdc++.h>
#ifndef OTERA_MODINT
#define OTERA_MODINT 1


#include <cassert>
#include <numeric>
#include <type_traits>

#ifdef _MSC_VER
#include <intrin.h>
#endif


#include <utility>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

constexpr long long safe_mod(long long x, long long m) {
    x %= m;
    if (x < 0) x += m;
    return x;
}

struct barrett {
    unsigned int _m;
    unsigned long long im;

    explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}

    unsigned int umod() const { return _m; }

    unsigned int mul(unsigned int a, unsigned int b) const {

        unsigned long long z = a;
        z *= b;
#ifdef _MSC_VER
        unsigned long long x;
        _umul128(z, im, &x);
#else
        unsigned long long x =
            (unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
        unsigned int v = (unsigned int)(z - x * _m);
        if (_m <= v) v += _m;
        return v;
    }
};

constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
    if (m == 1) return 0;
    unsigned int _m = (unsigned int)(m);
    unsigned long long r = 1;
    unsigned long long y = safe_mod(x, m);
    while (n) {
        if (n & 1) r = (r * y) % _m;
        y = (y * y) % _m;
        n >>= 1;
    }
    return r;
}

constexpr bool is_prime_constexpr(int n) {
    if (n <= 1) return false;
    if (n == 2 || n == 7 || n == 61) return true;
    if (n % 2 == 0) return false;
    long long d = n - 1;
    while (d % 2 == 0) d /= 2;
    constexpr long long bases[3] = {2, 7, 61};
    for (long long a : bases) {
        long long t = d;
        long long y = pow_mod_constexpr(a, t, n);
        while (t != n - 1 && y != 1 && y != n - 1) {
            y = y * y % n;
            t <<= 1;
        }
        if (y != n - 1 && t % 2 == 0) {
            return false;
        }
    }
    return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);

constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
    a = safe_mod(a, b);
    if (a == 0) return {b, 0};

    long long s = b, t = a;
    long long m0 = 0, m1 = 1;

    while (t) {
        long long u = s / t;
        s -= t * u;
        m0 -= m1 * u;  // |m1 * u| <= |m1| * s <= b


        auto tmp = s;
        s = t;
        t = tmp;
        tmp = m0;
        m0 = m1;
        m1 = tmp;
    }
    if (m0 < 0) m0 += b / s;
    return {s, m0};
}

constexpr int primitive_root_constexpr(int m) {
    if (m == 2) return 1;
    if (m == 167772161) return 3;
    if (m == 469762049) return 3;
    if (m == 754974721) return 11;
    if (m == 998244353) return 3;
    int divs[20] = {};
    divs[0] = 2;
    int cnt = 1;
    int x = (m - 1) / 2;
    while (x % 2 == 0) x /= 2;
    for (int i = 3; (long long)(i)*i <= x; i += 2) {
        if (x % i == 0) {
            divs[cnt++] = i;
            while (x % i == 0) {
                x /= i;
            }
        }
    }
    if (x > 1) {
        divs[cnt++] = x;
    }
    for (int g = 2;; g++) {
        bool ok = true;
        for (int i = 0; i < cnt; i++) {
            if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
                ok = false;
                break;
            }
        }
        if (ok) return g;
    }
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);

unsigned long long floor_sum_unsigned(unsigned long long n,
                                      unsigned long long m,
                                      unsigned long long a,
                                      unsigned long long b) {
    unsigned long long ans = 0;
    while (true) {
        if (a >= m) {
            ans += n * (n - 1) / 2 * (a / m);
            a %= m;
        }
        if (b >= m) {
            ans += n * (b / m);
            b %= m;
        }

        unsigned long long y_max = a * n + b;
        if (y_max < m) break;
        n = (unsigned long long)(y_max / m);
        b = (unsigned long long)(y_max % m);
        std::swap(m, a);
    }
    return ans;
}

}  // namespace internal

}  // namespace atcoder


#include <cassert>
#include <numeric>
#include <type_traits>

namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value ||
                                  std::is_same<T, __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int128 =
    typename std::conditional<std::is_same<T, __uint128_t>::value ||
                                  std::is_same<T, unsigned __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using make_unsigned_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value,
                              __uint128_t,
                              unsigned __int128>;

template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
                                                  is_signed_int128<T>::value ||
                                                  is_unsigned_int128<T>::value,
                                              std::true_type,
                                              std::false_type>::type;

template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
                                                 std::is_signed<T>::value) ||
                                                    is_signed_int128<T>::value,
                                                std::true_type,
                                                std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<(is_integral<T>::value &&
                               std::is_unsigned<T>::value) ||
                                  is_unsigned_int128<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
    is_signed_int128<T>::value,
    make_unsigned_int128<T>,
    typename std::conditional<std::is_signed<T>::value,
                              std::make_unsigned<T>,
                              std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
    typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<is_integral<T>::value &&
                                  std::is_unsigned<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
                                              std::make_unsigned<T>,
                                              std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

}  // namespace internal

}  // namespace atcoder


namespace atcoder {

namespace internal {

struct modint_base {};
struct static_modint_base : modint_base {};

template <class T> using is_modint = std::is_base_of<modint_base, T>;
template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;

}  // namespace internal

template <int m, std::enable_if_t<(1 <= m)>* = nullptr>
struct static_modint : internal::static_modint_base {
    using mint = static_modint;

  public:
    static constexpr int mod() { return m; }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    static_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    static_modint(T v) {
        long long x = (long long)(v % (long long)(umod()));
        if (x < 0) x += umod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    static_modint(T v) {
        _v = (unsigned int)(v % umod());
    }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v -= rhs._v;
        if (_v >= umod()) _v += umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        unsigned long long z = _v;
        z *= rhs._v;
        _v = (unsigned int)(z % umod());
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        if (prime) {
            assert(_v);
            return pow(umod() - 2);
        } else {
            auto eg = internal::inv_gcd(_v, m);
            assert(eg.first == 1);
            return eg.second;
        }
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static constexpr unsigned int umod() { return m; }
    static constexpr bool prime = internal::is_prime<m>;
};

template <int id> struct dynamic_modint : internal::modint_base {
    using mint = dynamic_modint;

  public:
    static int mod() { return (int)(bt.umod()); }
    static void set_mod(int m) {
        assert(1 <= m);
        bt = internal::barrett(m);
    }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    dynamic_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        long long x = (long long)(v % (long long)(mod()));
        if (x < 0) x += mod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        _v = (unsigned int)(v % mod());
    }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v += mod() - rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        _v = bt.mul(_v, rhs._v);
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        auto eg = internal::inv_gcd(_v, mod());
        assert(eg.first == 1);
        return eg.second;
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static internal::barrett bt;
    static unsigned int umod() { return bt.umod(); }
};
template <int id> internal::barrett dynamic_modint<id>::bt(998244353);

using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
using modint = dynamic_modint<-1>;

namespace internal {

template <class T>
using is_static_modint = std::is_base_of<internal::static_modint_base, T>;

template <class T>
using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;

template <class> struct is_dynamic_modint : public std::false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};

template <class T>
using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;

}  // namespace internal

}  // namespace atcoder


namespace otera {
    using modint107 = atcoder::modint1000000007;
    using modint998 = atcoder::modint998244353;
    using modint = atcoder::modint;
}; //namespace otera

std::ostream& operator<<(std::ostream& out, const atcoder::modint1000000007 &e) {
    out << e.val();
    return out;
}

std::ostream& operator<<(std::ostream& out, const atcoder::modint998244353 &e) {
    out << e.val();
    return out;
}

std::ostream& operator<<(std::ostream& out, const atcoder::modint &e) {
    out << e.val();
    return out;
}

#endif // OTERA_MODINT


namespace otera{
    template<typename T>
    class powtable {
        public:
        powtable(long long x) : x_(T(x)), pw_(1, 1) {}
        powtable(long long x, int n) : x_(T(x)), pw_(1, 1) {
            ensure(n);
        }
        T pow(const int n) {
            assert(n >= 0);
            ensure(n);
            return pw_[n];
        }
        T operator[](const int n) {
                return pow(n);
        }
        private:
        T x_;
        std::vector<T> pw_;
        void ensure(const int n) {
            int sz = pw_.size();
            if(n + 1 <= sz) return;
            int next_sz = std::max(n + 1, sz * 2);
            pw_.resize(next_sz);
            for(int i = sz; i < next_sz; ++ i) {
                pw_[i] = pw_[i - 1] * x_;
            }
        }
    };
} // namespace otera



namespace otera{
    template<typename T>
    class factorial {
        public:
        factorial() {}
        factorial(int n) {
            ensure(n);
        }
        T com(const int n, const int k) {
            if(n == k) return 1;
            if(n < k or n < 0 or k < 0) return 0;
            ensure(n);
            return fact_[n] * finv_[k] * finv_[n - k];
        }
        T fact(const int n) {
            if(n < 0) return 0;
            ensure(n);
            return fact_[n];
        }
        T inv(const int n) {
            if(n < 0) return 0;
            ensure(n);
            return inv_[n];
        }
        T finv(const int n) {
            if(n < 0) return 0;
            ensure(n);
            return finv_[n];
        }
        private:
        static std::vector<T> fact_, inv_, finv_;
        int MOD = T::mod();
        void ensure(const int n) {
            int sz = fact_.size();
            if(n + 1 <= sz) return;
            int next_sz = std::max(n + 1, sz * 2);
            fact_.resize(next_sz), inv_.resize(next_sz), finv_.resize(next_sz);
            for(int i = sz; i < next_sz; ++ i) {
                fact_[i] = fact_[i - 1] * i;
                inv_[i] = -inv_[MOD % i] * (MOD / i);
                finv_[i] = finv_[i - 1] * inv_[i];
            }
        }
    };
    template<typename T> std::vector<T> factorial<T>::fact_ {1, 1};
    template<typename T> std::vector<T> factorial<T>::inv_ {1, 1};
    template<typename T> std::vector<T> factorial<T>::finv_ {1, 1};
} // namespace otera

using namespace std;

#define int long long

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using int128_t = __int128_t;
#define repa(i, n) for(int i = 0; i < n; ++ i)
#define repb(i, a, b) for(int i = a; i < b; ++ i)
#define repc(i, a, b, c) for(int i = a; i < b; i += c)
#define overload4(a, b, c, d, e, ...) e
#define overload3(a, b, c, d, ...) d
#define rep(...) overload4(__VA_ARGS__, repc, repb, repa)(__VA_ARGS__)
#define rep1a(i, n) for(int i = 0; i <= n; ++ i)
#define rep1b(i, a, b) for(int i = a; i <= b; ++ i)
#define rep1c(i, a, b, c) for(int i = a; i <= b; i += c)
#define rep1(...) overload4(__VA_ARGS__, rep1c, rep1b, rep1a)(__VA_ARGS__)
#define rev_repa(i, n) for(int i=n-1;i>=0;i--)
#define rev_repb(i, a, b) assert(a > b);for(int i=a;i>b;i--)
#define rev_rep(...) overload3(__VA_ARGS__, rev_repb, rev_repa)(__VA_ARGS__)
#define rev_rep1a(i, n) for(int i=n;i>=1;i--)
#define rev_rep1b(i, a, b) assert(a >= b);for(int i=a;i>=b;i--)
#define rev_rep1(...) overload3(__VA_ARGS__, rev_rep1b, rev_rep1a)(__VA_ARGS__)
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define eb emplace_back
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define rall(c) c.rbegin(), c.rend()
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define Sort(a) sort(all(a))
#define Rev(a) reverse(all(a))
#define Uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))
#define si(c) (int)(c).size()
inline ll popcnt(ull a){ return __builtin_popcountll(a); }
#define kth_bit(x, k) ((x>>k)&1)
#define unless(A) if(!(A))
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll intpow(ll a, ll b, ll m) {ll ans = 1; while(b){ if(b & 1) (ans *= a) %= m; (a *= a) %= m; b /= 2; } return ans; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define ULL(...) ull __VA_ARGS__;in(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;in(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;in(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;in(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__;in(__VA_ARGS__)
#define vec(type,name,...) vector<type>name(__VA_ARGS__)
#define VEC(type,name,size) vector<type>name(size);in(name)
#define vv(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define VV(type,name,h,w) vector<vector<type>>name(h,vector<type>(w));in(name)
#define vvv(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vc<T>>;
template <class T> using vvvc = vector<vvc<T>>;
template <class T> using vvvvc = vector<vvvc<T>>;
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template<class T> void scan(T& a){ cin >> a; }
template<class T> void scan(vector<T>& a){ for(auto&& i : a) scan(i); }
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){ scan(head); in(tail...); }
void print(){ cout << ' '; }
template<class T> void print(const T& a){ cout << a; }
template<class T> void print(const vector<T>& a){ if(a.empty()) return; print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ cout << ' '; print(*i); } }
int out(){ cout << '\n'; return 0; }
template<class T> int out(const T& t){ print(t); cout << '\n'; return 0; }
template<class Head, class... Tail> int out(const Head& head, const Tail&... tail){ print(head); cout << ' '; out(tail...); return 0; }
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x
#define debug_1(x1) cout<<#x1<<": "<<x1<<endl
#define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl
#define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl
#define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl
#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl
#ifdef DEBUG
#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)
#define dump(...) { print(#__VA_ARGS__); print(":"); out(__VA_ARGS__); }
#else
#define debug(...)
#define dump(...)
#endif

struct io_setup {
    io_setup(int precision = 20) {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout << fixed << setprecision(precision);
    }
} io_setup_ {};

using mint = otera::modint107;
otera::factorial<mint> bc;

mint calc(int n, vc<int> a) {
    otera::powtable<mint> pw(10);
    otera::powtable<mint> pw2(2);
    // INT(n);
    // VEC(int, a, n);
    vc<int> b(n);
    vc<int> c(5, 0);
    int cnt0 = 0;
    rep(i, n) {
        if(a[i] == 0) ++ cnt0;
        b[i] = si(to_string(a[i]));
        c[b[i]] ++;
    }
    vc<mint> pre(n + 1, 0);
    pre[0] = 1;
    for(int j = 1; j <= n; ++ j) {
        for(int i = 0; i <= j; ++ i) {
            pre[j] += bc.com(j, i) * bc.fact(i);
        }
    }
    mint ans = 0;
    vvc<mint> dp(4 * n + 1, vc<mint>(n + 1, 0));
    dp[0][0] = 1;
    for(int j = 1; j <= 4; ++ j) {
        for(int x = 4 * n; x >= 0; -- x) {
            for(int y = n; y >= 0; -- y) {
                if(dp[x][y] == 0) continue;
                for(int z = 1; z <= c[j]; ++ z) {
                    if(x + j * z <= 4 * n && y + z <= n) {
                        dp[x + j * z][y + z] += dp[x][y] * bc.com(c[j], z);
                    }
                }
            }
        }
    }
    rep(i, 4 * n + 1) {
        rep(j, n + 1) {
            if(dp[i][j] != 0) {
                debug(i, j, dp[i][j]);
            }
        }
    }
    vector ndp(4 * n + 1, vc<mint>(n + 1, 0));
    rep(i, n) {
        if(a[i] != 0) {
            debug(i, b[i]);
            for(int x = 0; x <= 4 * n; ++ x) {
                for(int y = 0; y <= n; ++ y) {
                    if(x - b[i] >= 0 && y - 1 >= 0) {
                        ndp[x][y] = dp[x][y] - ndp[x - b[i]][y - 1];
                    } else {
                        ndp[x][y] = dp[x][y];
                    }
                }
            }
            rep(x, 4 * n + 1) {
                assert(ndp[x][n] == 0);
            }
            for(int x = 0; x <= 4 * n; ++ x) {
                for(int y = 0; y <= n - 1; ++ y) {
                    if(ndp[x][y] != 0) {
                        debug(i, x, y, ndp[x][y]);
                    }
                    ans += a[i] * ndp[x][y] * pw[x] * pre[n - 1 - y] * bc.fact(y);
                }
            }
        }
    }
    return ans;
}

void solve() {
    // otera::powtable<mint> pw(10);
    // otera::powtable<mint> pw2(2);
    INT(n);
    VEC(int, a, n);
    mint ans = calc(n, a);
    int cnt0 = 0;
    rep(i, n) {
        if(a[i] == 0) ++ cnt0;
    }
    if(cnt0 > 0) {
        Sort(a); Rev(a); a.pop_back();
        ans -= mint(cnt0) * calc(n - 1, a);
    }
    out(ans);
}

signed main() {
    int testcase = 1;
    // in(testcase);
    while(testcase--) solve();
    return 0;
}
accuracy: 1
time_ms: 550
memory_kb: 4852
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }
template <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }

template <int mod>
class Modint {
    using mint = Modint;
    static_assert(mod > 0, "Modulus must be positive");

public:
    static constexpr int get_mod() noexcept { return mod; }

    constexpr Modint(long long y = 0) noexcept : x(y >= 0 ? y % mod : (y % mod + mod) % mod) {}

    constexpr int value() const noexcept { return x; }

    constexpr mint& operator+=(const mint& r) noexcept { if ((x += r.x) >= mod) x -= mod; return *this; }
    constexpr mint& operator-=(const mint& r) noexcept { if ((x += mod - r.x) >= mod) x -= mod; return *this; }
    constexpr mint& operator*=(const mint& r) noexcept { x = static_cast<int>(1LL * x * r.x % mod); return *this; }
    constexpr mint& operator/=(const mint& r) noexcept { *this *= r.inv(); return *this; }

    constexpr mint operator-() const noexcept { return mint(-x); }

    constexpr mint operator+(const mint& r) const noexcept { return mint(*this) += r; }
    constexpr mint operator-(const mint& r) const noexcept { return mint(*this) -= r; }
    constexpr mint operator*(const mint& r) const noexcept { return mint(*this) *= r; }
    constexpr mint operator/(const mint& r) const noexcept { return mint(*this) /= r; }

    constexpr bool operator==(const mint& r) const noexcept { return x == r.x; }
    constexpr bool operator!=(const mint& r) const noexcept { return x != r.x; }

    constexpr mint inv() const noexcept {
        int a = x, b = mod, u = 1, v = 0;
        while (b > 0) {
            int t = a / b;
            std::swap(a -= t * b, b);
            std::swap(u -= t * v, v);
        }
        return mint(u);
    }

    constexpr mint pow(long long n) const noexcept {
        mint ret(1), mul(x);
        while (n > 0) {
            if (n & 1) ret *= mul;
            mul *= mul;
            n >>= 1;
        }
        return ret;
    }

    friend std::ostream& operator<<(std::ostream& os, const mint& r) {
        return os << r.x;
    }

    friend std::istream& operator>>(std::istream& is, mint& r) {
        long long t;
        is >> t;
        r = mint(t);
        return is;
    }

private:
    int x;
};

using mint = Modint<(int) 1e9+7>;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    for (int i = 0; i < (int) v.size(); ++i) os << v[i] << " ";
    return os;
}

template <typename T>
class Combination {
public:
    Combination() = default;
    Combination(int n) : fact_(n + 1), fact_inv_(n + 1) {
        fact_[0] = 1;
        for (int i = 1; i <= n; ++i) fact_[i] = fact_[i - 1] * i;
        fact_inv_[n] = T(1) / fact_[n];
        for (int i = n; i > 0; --i) fact_inv_[i - 1] = fact_inv_[i] * i;
    }

    T perm(int n, int r) const {
        if (r < 0 || n < r) return 0;
        return fact_[n] * fact_inv_[n - r];
    }

    T comb(int n, int r) const {
        if (r < 0 || n < r) return 0;
        return fact_[n] * fact_inv_[r] * fact_inv_[n - r];
    }

    T fact(int n) const { return fact_[n]; }
    T fact_inv(int n) const { return fact_inv_[n]; }

private:
    std::vector<T> fact_, fact_inv_;
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int n;
    cin >> n;
    bool zero = false;
    vector<mint> sum(5);
    vector<int> cnt(5);
    rep(_,0,n) {
        int x;
        cin >> x;
        if (x == 0) {
            zero = true;
        }
        int l = to_string(x).size();
        sum[l] += x;
        ++cnt[l];
    }
    Combination<mint> comb(n);
    vector<mint> pow10(4*n+1, 1);
    rep(i,1,4*n+1) pow10[i] = pow10[i-1] * 10;
    vector<mint> cumperm(n+1);
    rep(i,0,n+1) {
        rep(j,0,i+1) cumperm[i] += comb.perm(i, j);
    }

    auto calc = [&]() -> mint {
        if (n == 0) return 0;
        mint ans = 0;
        rep(l,1,5) {
            if (cnt[l] == 0) continue;
            vector<vector<mint>> dp(n, vector<mint>(4*n));
            dp[0][0] = 1;
            rep(k,1,5) {
                int c = cnt[k];
                if (k == l) --c;
                revrep(i,n,0) revrep(j,4*n,0) rep(d,1,c+1) {
                    if (i+d >= n || j+k*d >= 4*n) break;
                    dp[i+d][j+k*d] += dp[i][j] * comb.comb(c, d);
                }
            }
            rep(i,0,n) rep(j,0,4*n) {
                ans += dp[i][j] * comb.fact(i) * cumperm[n-1-i] * pow10[j] * sum[l];
            }
        }
        return ans;
    };

    mint ans = calc();
    if (zero) {
        --n;
        --cnt[1];
        ans -= calc();
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 800
memory_kb: 3988
final_rank: 7

code_snippet: #line 2 "library/bits/stdc++.h"

// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#line 2 "Source.cpp"
using namespace std;

#define REP(a, b) for (long long a = 0; a < b; ++a)
#define ll long long
#define ld long double
#define ALL(x) begin(x), end(x)

#line 1 "library/atcoder/modint.hpp"



#include <cassert>
#line 6 "library/atcoder/modint.hpp"
#include <type_traits>

#ifdef _MSC_VER
#include <intrin.h>
#endif

#line 1 "library/atcoder/internal_math.hpp"



#line 5 "library/atcoder/internal_math.hpp"

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

// @param m `1 <= m`
// @return x mod m
constexpr long long safe_mod(long long x, long long m) {
    x %= m;
    if (x < 0) x += m;
    return x;
}

// Fast modular multiplication by barrett reduction
// Reference: https://en.wikipedia.org/wiki/Barrett_reduction
// NOTE: reconsider after Ice Lake
struct barrett {
    unsigned int _m;
    unsigned long long im;

    // @param m `1 <= m < 2^31`
    explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}

    // @return m
    unsigned int umod() const { return _m; }

    // @param a `0 <= a < m`
    // @param b `0 <= b < m`
    // @return `a * b % m`
    unsigned int mul(unsigned int a, unsigned int b) const {
        // [1] m = 1
        // a = b = im = 0, so okay

        // [2] m >= 2
        // im = ceil(2^64 / m)
        // -> im * m = 2^64 + r (0 <= r < m)
        // let z = a*b = c*m + d (0 <= c, d < m)
        // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im
        // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2
        // ((ab * im) >> 64) == c or c + 1
        unsigned long long z = a;
        z *= b;
#ifdef _MSC_VER
        unsigned long long x;
        _umul128(z, im, &x);
#else
        unsigned long long x =
            (unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
        unsigned int v = (unsigned int)(z - x * _m);
        if (_m <= v) v += _m;
        return v;
    }
};

// @param n `0 <= n`
// @param m `1 <= m`
// @return `(x ** n) % m`
constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
    if (m == 1) return 0;
    unsigned int _m = (unsigned int)(m);
    unsigned long long r = 1;
    unsigned long long y = safe_mod(x, m);
    while (n) {
        if (n & 1) r = (r * y) % _m;
        y = (y * y) % _m;
        n >>= 1;
    }
    return r;
}

// Reference:
// M. Forisek and J. Jancina,
// Fast Primality Testing for Integers That Fit into a Machine Word
// @param n `0 <= n`
constexpr bool is_prime_constexpr(int n) {
    if (n <= 1) return false;
    if (n == 2 || n == 7 || n == 61) return true;
    if (n % 2 == 0) return false;
    long long d = n - 1;
    while (d % 2 == 0) d /= 2;
    constexpr long long bases[3] = {2, 7, 61};
    for (long long a : bases) {
        long long t = d;
        long long y = pow_mod_constexpr(a, t, n);
        while (t != n - 1 && y != 1 && y != n - 1) {
            y = y * y % n;
            t <<= 1;
        }
        if (y != n - 1 && t % 2 == 0) {
            return false;
        }
    }
    return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);

// @param b `1 <= b`
// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g
constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
    a = safe_mod(a, b);
    if (a == 0) return {b, 0};

    // Contracts:
    // [1] s - m0 * a = 0 (mod b)
    // [2] t - m1 * a = 0 (mod b)
    // [3] s * |m1| + t * |m0| <= b
    long long s = b, t = a;
    long long m0 = 0, m1 = 1;

    while (t) {
        long long u = s / t;
        s -= t * u;
        m0 -= m1 * u;  // |m1 * u| <= |m1| * s <= b

        // [3]:
        // (s - t * u) * |m1| + t * |m0 - m1 * u|
        // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)
        // = s * |m1| + t * |m0| <= b

        auto tmp = s;
        s = t;
        t = tmp;
        tmp = m0;
        m0 = m1;
        m1 = tmp;
    }
    // by [3]: |m0| <= b/g
    // by g != b: |m0| < b/g
    if (m0 < 0) m0 += b / s;
    return {s, m0};
}

// Compile time primitive root
// @param m must be prime
// @return primitive root (and minimum in now)
constexpr int primitive_root_constexpr(int m) {
    if (m == 2) return 1;
    if (m == 167772161) return 3;
    if (m == 469762049) return 3;
    if (m == 754974721) return 11;
    if (m == 998244353) return 3;
    int divs[20] = {};
    divs[0] = 2;
    int cnt = 1;
    int x = (m - 1) / 2;
    while (x % 2 == 0) x /= 2;
    for (int i = 3; (long long)(i)*i <= x; i += 2) {
        if (x % i == 0) {
            divs[cnt++] = i;
            while (x % i == 0) {
                x /= i;
            }
        }
    }
    if (x > 1) {
        divs[cnt++] = x;
    }
    for (int g = 2;; g++) {
        bool ok = true;
        for (int i = 0; i < cnt; i++) {
            if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
                ok = false;
                break;
            }
        }
        if (ok) return g;
    }
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);

// @param n `n < 2^32`
// @param m `1 <= m < 2^32`
// @return sum_{i=0}^{n-1} floor((ai + b) / m) (mod 2^64)
unsigned long long floor_sum_unsigned(unsigned long long n,
                                      unsigned long long m,
                                      unsigned long long a,
                                      unsigned long long b) {
    unsigned long long ans = 0;
    while (true) {
        if (a >= m) {
            ans += n * (n - 1) / 2 * (a / m);
            a %= m;
        }
        if (b >= m) {
            ans += n * (b / m);
            b %= m;
        }

        unsigned long long y_max = a * n + b;
        if (y_max < m) break;
        // y_max < m * (n + 1)
        // floor(y_max / m) <= n
        n = (unsigned long long)(y_max / m);
        b = (unsigned long long)(y_max % m);
        std::swap(m, a);
    }
    return ans;
}

}  // namespace internal

}  // namespace atcoder


#line 1 "library/atcoder/internal_type_traits.hpp"



#line 7 "library/atcoder/internal_type_traits.hpp"

namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value ||
                                  std::is_same<T, __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int128 =
    typename std::conditional<std::is_same<T, __uint128_t>::value ||
                                  std::is_same<T, unsigned __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using make_unsigned_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value,
                              __uint128_t,
                              unsigned __int128>;

template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
                                                  is_signed_int128<T>::value ||
                                                  is_unsigned_int128<T>::value,
                                              std::true_type,
                                              std::false_type>::type;

template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
                                                 std::is_signed<T>::value) ||
                                                    is_signed_int128<T>::value,
                                                std::true_type,
                                                std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<(is_integral<T>::value &&
                               std::is_unsigned<T>::value) ||
                                  is_unsigned_int128<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
    is_signed_int128<T>::value,
    make_unsigned_int128<T>,
    typename std::conditional<std::is_signed<T>::value,
                              std::make_unsigned<T>,
                              std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
    typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<is_integral<T>::value &&
                                  std::is_unsigned<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
                                              std::make_unsigned<T>,
                                              std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

}  // namespace internal

}  // namespace atcoder


#line 14 "library/atcoder/modint.hpp"

namespace atcoder {

namespace internal {

struct modint_base {};
struct static_modint_base : modint_base {};

template <class T> using is_modint = std::is_base_of<modint_base, T>;
template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;

}  // namespace internal

template <int m, std::enable_if_t<(1 <= m)>* = nullptr>
struct static_modint : internal::static_modint_base {
    using mint = static_modint;

  public:
    static constexpr int mod() { return m; }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    static_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    static_modint(T v) {
        long long x = (long long)(v % (long long)(umod()));
        if (x < 0) x += umod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    static_modint(T v) {
        _v = (unsigned int)(v % umod());
    }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v -= rhs._v;
        if (_v >= umod()) _v += umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        unsigned long long z = _v;
        z *= rhs._v;
        _v = (unsigned int)(z % umod());
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        if (prime) {
            assert(_v);
            return pow(umod() - 2);
        } else {
            auto eg = internal::inv_gcd(_v, m);
            assert(eg.first == 1);
            return eg.second;
        }
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static constexpr unsigned int umod() { return m; }
    static constexpr bool prime = internal::is_prime<m>;
};

template <int id> struct dynamic_modint : internal::modint_base {
    using mint = dynamic_modint;

  public:
    static int mod() { return (int)(bt.umod()); }
    static void set_mod(int m) {
        assert(1 <= m);
        bt = internal::barrett(m);
    }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    dynamic_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        long long x = (long long)(v % (long long)(mod()));
        if (x < 0) x += mod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        _v = (unsigned int)(v % mod());
    }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v += mod() - rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        _v = bt.mul(_v, rhs._v);
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        auto eg = internal::inv_gcd(_v, mod());
        assert(eg.first == 1);
        return eg.second;
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static internal::barrett bt;
    static unsigned int umod() { return bt.umod(); }
};
template <int id> internal::barrett dynamic_modint<id>::bt(998244353);

using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
using modint = dynamic_modint<-1>;

namespace internal {

template <class T>
using is_static_modint = std::is_base_of<internal::static_modint_base, T>;

template <class T>
using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;

template <class> struct is_dynamic_modint : public std::false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};

template <class T>
using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;

}  // namespace internal

}  // namespace atcoder


#line 10 "Source.cpp"
using mint = atcoder::modint1000000007;

#define int long long
mint dp[300][2000];

int dfs(vector<int> now, int val)
{
  int ans = val;
  REP(i, now.size())
  {
    int tmp = val * 10 + now[i];
    vector<int> news;
    REP(q, now.size())
    {
      if (i != q)
        news.push_back(now[q]);
    }
    ans += dfs(news, tmp);
  }
  return ans;
}

mint mergers[1000];
void solve()
{
  int n;
  cin >> n;
  int cnts[5] = {};
  vector<string> inputs;
  int is_zero = 0;
  REP(i, n)
  {
    string s;
    cin >> s;
    inputs.push_back(s);
    cnts[s.length()]++;
    if (s == "0")
      is_zero = true;
  }
  sort(ALL(inputs));
  mint ans = 0;
  REP(t, 1 + is_zero)
  {
    for (int q = 0; q <= n; ++q)
    {
      mint hoge = 0;
      mint now_choose = 1;
      for (int t = 0; t <= n - q - 1; ++t)
      {
        hoge += now_choose;
        now_choose *= (n - q - 1 - t);
      }
      mergers[q] = hoge;
    }
    mint tmps = 0;
    for (int digit = 1; digit <= 4; ++digit)
    {
      if (cnts[digit] == 0)
        continue;
      cnts[digit]--;

      REP(q, n + 1)
      {
        REP(j, n * 5)
        {
          dp[q][j] = 0;
        }
      }
      dp[0][0] = 1;
      for (int t = 1; t <= 4; ++t)
      {
        REP(_, cnts[t])
        {
          for (int a = n; a >= 0; --a)
          {
            for (int b = 4 * n; b >= 0; --b)
            {
              dp[a + 1][b + t] += dp[a][b] * (a + 1);
            }
          }
        }
      }

      REP(t, inputs.size())
      {
        if (inputs[t].size() == digit)
        {
          for (int q = 0; q <= n; ++q)
          {
            mint multiplier = 1;
            for (int j = 0; j <= 4 * n; ++j)
            {
              tmps += multiplier * mergers[q] * dp[q][j] * stoll(inputs[t]);
              multiplier *= 10;
            }
          }
        }
      }

      cnts[digit]++;
    }

    if (t == 0)
    {
      ans += tmps;
    }
    if (t == 1)
    {
      ans -= tmps;
    }
    if (inputs.size() != 0 and inputs[0] == "0")
    {
      n--;
      inputs.erase(inputs.begin());
      cnts[1]--;
    }
  }
  cout << ans.val() << endl;
}

#undef int
int main()
{
  iostream::sync_with_stdio(false);
  cout << fixed << setprecision(100);
  solve();
}
accuracy: 1
time_ms: 1120
memory_kb: 5828
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;

template <uint64_t Modulus> class modint {
    using i64 = int64_t;
    using u32 = uint32_t;
    using u64 = uint64_t;

    static_assert(Modulus < static_cast<uint32_t>(1) << 31, "Modulus must be less than 2**31");
    static constexpr u32 mod = Modulus;
    u32 v;

public:
    constexpr modint(const i64 x = 0) noexcept : v(x < 0 ? mod - 1 - (-(x + 1) % mod) : x % mod) {}
    constexpr u32& val() noexcept { return v; }
    constexpr const u32& val() const noexcept { return v; }
    constexpr modint operator+(const modint& rhs) const noexcept { return modint(*this) += rhs; }
    constexpr modint operator-(const modint& rhs) const noexcept { return modint(*this) -= rhs; }
    constexpr modint operator*(const modint& rhs) const noexcept { return modint(*this) *= rhs; }
    constexpr modint operator/(const modint& rhs) const noexcept { return modint(*this) /= rhs; }
    constexpr modint& operator+=(const modint& rhs) noexcept {
        v += rhs.v;
        if (v >= mod) v -= mod;
        return *this;
    }
    constexpr modint& operator-=(const modint& rhs) noexcept {
        if (v < rhs.v) v += mod;
        v -= rhs.v;
        return *this;
    }
    constexpr modint& operator*=(const modint& rhs) noexcept {
        v = (u64)v * rhs.v % mod;
        return *this;
    }
    constexpr modint& operator/=(const modint& rhs) noexcept { return *this *= rhs.inv(); }
    constexpr modint pow(long long n) const noexcept {
        assert(0 <= n);
        modint self(*this), res(1);
        while (n > 0) {
            if (n & 1) res *= self;
            self *= self;
            n >>= 1;
        }
        return res;
    }
    constexpr modint inv() const noexcept {
        assert(*this != 0);
        return pow(mod - 2);
    }
    constexpr modint& operator++() noexcept {
        if (++v == mod) v = 0;
        return *this;
    }
    constexpr modint& operator--() noexcept {
        if (v == 0) v = mod;
        return --v, *this;
    }
    constexpr modint operator++(int) noexcept {
        modint t = *this;
        return ++*this, t;
    }
    constexpr modint operator--(int) noexcept {
        modint t = *this;
        return --*this, t;
    }
    constexpr modint operator-() const noexcept { return modint(mod - v); }
    template <class T> friend constexpr modint operator+(T x, modint y) noexcept { return modint(x) + y; }
    template <class T> friend constexpr modint operator-(T x, modint y) noexcept { return modint(x) - y; }
    template <class T> friend constexpr modint operator*(T x, modint y) noexcept { return modint(x) * y; }
    template <class T> friend constexpr modint operator/(T x, modint y) noexcept { return modint(x) / y; }
    constexpr bool operator==(const modint& rhs) const noexcept { return v == rhs.v; }
    constexpr bool operator!=(const modint& rhs) const noexcept { return v != rhs.v; }
    constexpr bool operator!() const noexcept { return !v; }
    friend std::istream& operator>>(std::istream& s, modint& rhs) noexcept {
        i64 v;
        rhs = modint{(s >> v, v)};
        return s;
    }
    friend std::ostream& operator<<(std::ostream& s, const modint& rhs) noexcept { return s << rhs.v; }
};

/**
 * @brief modint
 * @docs docs/modulo/modint.md
 */

template <typename T> struct Binomial {
    Binomial(int MAX = 0) : n(1), facs(1, T(1)), finvs(1, T(1)), invs(1, T(1)) {
        while (n <= MAX) extend();
    }

    T fac(int i) {
        assert(i >= 0);
        while (n <= i) extend();
        return facs[i];
    }

    T finv(int i) {
        assert(i >= 0);
        while (n <= i) extend();
        return finvs[i];
    }

    T inv(int i) {
        assert(i >= 0);
        while (n <= i) extend();
        return invs[i];
    }

    T P(int n, int r) {
        if (n < 0 || n < r || r < 0) return T(0);
        return fac(n) * finv(n - r);
    }

    T C(int n, int r) {
        if (n < 0 || n < r || r < 0) return T(0);
        return fac(n) * finv(n - r) * finv(r);
    }

    T H(int n, int r) {
        if (n < 0 || r < 0) return T(0);
        return r == 0 ? 1 : C(n + r - 1, r);
    }

    T C_naive(int n, int r) {
        if (n < 0 || n < r || r < 0) return T(0);
        T res = 1;
        r = std::min(r, n - r);
        for (int i = 1; i <= r; i++) res *= inv(i) * (n--);
        return res;
    }

private:
    int n;
    std::vector<T> facs, finvs, invs;

    inline void extend() {
        int m = n << 1;
        facs.resize(m);
        finvs.resize(m);
        invs.resize(m);
        for (int i = n; i < m; i++) facs[i] = facs[i - 1] * i;
        finvs[m - 1] = T(1) / facs[m - 1];
        invs[m - 1] = finvs[m - 1] * facs[m - 2];
        for (int i = m - 2; i >= n; i--) {
            finvs[i] = finvs[i + 1] * (i + 1);
            invs[i] = finvs[i] * facs[i - 1];
        }
        n = m;
    }
};

/**
 * @brief binomial
 * @docs docs/combinatorics/binomial.md
 */

const long long MOD = 1000000007;
using mint = modint<MOD>;
const int MAX_N = 205;
mint dp[MAX_N][MAX_N][MAX_N][2];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    Binomial<mint> BINOM;
    int n;
    cin >> n;
    vector<int> a(n);
    for (int& x : a) cin >> x;

    sort(a.rbegin(), a.rend());
    vector<int> len(n);
    int sum = 0;
    for (int i = 0; i < n; i++) {
        len[i] = to_string(a[i]).size();
        sum += len[i];
    }
    vector<mint> power(sum + 1, 1);
    for (int i = 0; i < sum; i++) power[i + 1] = power[i] * 10;

    dp[0][0][0][0] = 1;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j <= i; j++) {
            for (int k = 0; j + k <= i; k++) {
                for (int l = 0; l < 2; l++) {
                    mint val = dp[i][j][k][l];
                    if (val == 0) continue;
                    dp[i + 1][j][k][l] += val;
                    if (l == 0) dp[i + 1][j][k][1] += val * a[i];
                    dp[i + 1][j + 1][k][l] += val;
                    dp[i + 1][j][k + 1][l] += val * power[len[i]];
                }
            }
        }
    }

    mint ans = 0;
    for (int j = 0; j <= n; j++) {
        for (int k = 0; j + k <= n; k++) {
            ans += dp[n][j][k][1] * BINOM.fac(j) * BINOM.fac(k);
        }
    }
    if (a.back() == 0) {
        for (int j = 0; j <= n; j++) {
            for (int k = 0; j + k <= n; k++) {
                ans -= dp[n - 1][j][k][1] * BINOM.fac(j) * BINOM.fac(k);
            }
        }
    }

    cout << ans << '\n';
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 67556
final_rank: 9

code_snippet: #define LOCAL
#include <bits/stdc++.h>
using namespace std;
#pragma region Macros
typedef long long ll;
typedef __int128_t i128;
typedef unsigned int uint;
typedef unsigned long long ull;
#define ALL(x) (x).begin(), (x).end()

template <typename T> istream& operator>>(istream& is, vector<T>& v) {
    for (T& x : v) is >> x;
    return is;
}
template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
    for (size_t i = 0; i < v.size(); i++) {
        os << v[i] << (i + 1 == v.size() ? "" : " ");
    }
    return os;
}
template <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& m) {
    os << '{';
    for (auto itr = m.begin(); itr != m.end();) {
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr != m.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T, typename U> ostream& operator<<(ostream& os, const unordered_map<T, U>& m) {
    os << '{';
    for (auto itr = m.begin(); itr != m.end();) {
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr != m.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const set<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
        os << *itr;
        if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const multiset<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
        os << *itr;
        if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const unordered_set<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
        os << *itr;
        if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const deque<T>& v) {
    for (size_t i = 0; i < v.size(); i++) {
        os << v[i] << (i + 1 == v.size() ? "" : " ");
    }
    return os;
}
template <typename T, size_t N> ostream& operator<<(ostream& os, const array<T, N>& v) {
    for (size_t i = 0; i < N; i++) {
        os << v[i] << (i + 1 == N ? "" : " ");
    }
    return os;
}

template <int i, typename T> void print_tuple(ostream&, const T&) {}
template <int i, typename T, typename H, class... Args> void print_tuple(ostream& os, const T& t) {
    if (i) os << ',';
    os << get<i>(t);
    print_tuple<i + 1, T, Args...>(os, t);
}
template <typename... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t) {
    os << '{';
    print_tuple<0, tuple<Args...>, Args...>(os, t);
    return os << '}';
}

void debug_out() { cerr << '\n'; }
template <class Head, class... Tail> void debug_out(Head&& head, Tail&&... tail) {
    cerr << head;
    if (sizeof...(Tail) > 0) cerr << ", ";
    debug_out(move(tail)...);
}
#ifdef LOCAL
#define debug(...)                                                                   \
    cerr << " ";                                                                     \
    cerr << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << '\n'; \
    cerr << " ";                                                                     \
    debug_out(__VA_ARGS__)
#else
#define debug(...) void(0)
#endif

template <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }
template <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }

int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
int topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
int botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }
int botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }
int popcount(signed t) { return __builtin_popcount(t); }
int popcount(long long t) { return __builtin_popcountll(t); }
bool ispow2(int i) { return i && (i & -i) == i; }
long long MSK(int n) { return (1LL << n) - 1; }

template <class T> T ceil(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <class T> T floor(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? x / y : (x - y + 1) / y);
}

template <class T1, class T2> inline bool chmin(T1& a, T2 b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T1, class T2> inline bool chmax(T1& a, T2 b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <typename T> void mkuni(vector<T>& v) {
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
}
template <typename T> int lwb(const vector<T>& v, const T& x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); }
#pragma endregion

#include <cassert>
#include <cstdint>
#include <iostream>

template <uint64_t Modulus> class modint {
    using i64 = int64_t;
    using u32 = uint32_t;
    using u64 = uint64_t;

    static_assert(Modulus < static_cast<uint32_t>(1) << 31, "Modulus must be less than 2**31");
    static constexpr u32 mod = Modulus;
    u32 v;

public:
    constexpr modint(const i64 x = 0) noexcept : v(x < 0 ? mod - 1 - (-(x + 1) % mod) : x % mod) {}
    constexpr u32& val() noexcept { return v; }
    constexpr const u32& val() const noexcept { return v; }
    constexpr modint operator+(const modint& rhs) const noexcept { return modint(*this) += rhs; }
    constexpr modint operator-(const modint& rhs) const noexcept { return modint(*this) -= rhs; }
    constexpr modint operator*(const modint& rhs) const noexcept { return modint(*this) *= rhs; }
    constexpr modint operator/(const modint& rhs) const noexcept { return modint(*this) /= rhs; }
    constexpr modint& operator+=(const modint& rhs) noexcept {
        v += rhs.v;
        if (v >= mod) v -= mod;
        return *this;
    }
    constexpr modint& operator-=(const modint& rhs) noexcept {
        if (v < rhs.v) v += mod;
        v -= rhs.v;
        return *this;
    }
    constexpr modint& operator*=(const modint& rhs) noexcept {
        v = (u64)v * rhs.v % mod;
        return *this;
    }
    constexpr modint& operator/=(const modint& rhs) noexcept { return *this *= rhs.inv(); }
    constexpr modint pow(long long n) const noexcept {
        assert(0 <= n);
        modint self(*this), res(1);
        while (n > 0) {
            if (n & 1) res *= self;
            self *= self;
            n >>= 1;
        }
        return res;
    }
    constexpr modint inv() const noexcept {
        assert(*this != 0);
        return pow(mod - 2);
    }
    constexpr modint& operator++() noexcept {
        if (++v == mod) v = 0;
        return *this;
    }
    constexpr modint& operator--() noexcept {
        if (v == 0) v = mod;
        return --v, *this;
    }
    constexpr modint operator++(int) noexcept {
        modint t = *this;
        return ++*this, t;
    }
    constexpr modint operator--(int) noexcept {
        modint t = *this;
        return --*this, t;
    }
    constexpr modint operator-() const noexcept { return modint(mod - v); }
    template <class T> friend constexpr modint operator+(T x, modint y) noexcept { return modint(x) + y; }
    template <class T> friend constexpr modint operator-(T x, modint y) noexcept { return modint(x) - y; }
    template <class T> friend constexpr modint operator*(T x, modint y) noexcept { return modint(x) * y; }
    template <class T> friend constexpr modint operator/(T x, modint y) noexcept { return modint(x) / y; }
    constexpr bool operator==(const modint& rhs) const noexcept { return v == rhs.v; }
    constexpr bool operator!=(const modint& rhs) const noexcept { return v != rhs.v; }
    constexpr bool operator!() const noexcept { return !v; }
    friend std::istream& operator>>(std::istream& s, modint& rhs) noexcept {
        i64 v;
        rhs = modint{(s >> v, v)};
        return s;
    }
    friend std::ostream& operator<<(std::ostream& s, const modint& rhs) noexcept { return s << rhs.v; }
};

/**
 * @brief modint
 * @docs docs/modulo/modint.md
 */

#include <cassert>
#include <vector>

template <typename T> struct Binomial {
    Binomial(int MAX = 0) : n(1), facs(1, T(1)), finvs(1, T(1)), invs(1, T(1)) {
        while (n <= MAX) extend();
    }

    T fac(int i) {
        assert(i >= 0);
        while (n <= i) extend();
        return facs[i];
    }

    T finv(int i) {
        assert(i >= 0);
        while (n <= i) extend();
        return finvs[i];
    }

    T inv(int i) {
        assert(i >= 0);
        while (n <= i) extend();
        return invs[i];
    }

    T P(int n, int r) {
        if (n < 0 || n < r || r < 0) return T(0);
        return fac(n) * finv(n - r);
    }

    T C(int n, int r) {
        if (n < 0 || n < r || r < 0) return T(0);
        return fac(n) * finv(n - r) * finv(r);
    }

    T H(int n, int r) {
        if (n < 0 || r < 0) return T(0);
        return r == 0 ? 1 : C(n + r - 1, r);
    }

    T C_naive(int n, int r) {
        if (n < 0 || n < r || r < 0) return T(0);
        T res = 1;
        r = std::min(r, n - r);
        for (int i = 1; i <= r; i++) res *= inv(i) * (n--);
        return res;
    }

private:
    int n;
    std::vector<T> facs, finvs, invs;

    inline void extend() {
        int m = n << 1;
        facs.resize(m);
        finvs.resize(m);
        invs.resize(m);
        for (int i = n; i < m; i++) facs[i] = facs[i - 1] * i;
        finvs[m - 1] = T(1) / facs[m - 1];
        invs[m - 1] = finvs[m - 1] * facs[m - 2];
        for (int i = m - 2; i >= n; i--) {
            finvs[i] = finvs[i + 1] * (i + 1);
            invs[i] = finvs[i] * facs[i - 1];
        }
        n = m;
    }
};

/**
 * @brief binomial
 * @docs docs/combinatorics/binomial.md
 */

const int INF = 1e9;
const long long IINF = 1e18;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const char dir[4] = {'D', 'R', 'U', 'L'};
const long long MOD = 1000000007;
// const long long MOD = 998244353;

using mint = modint<MOD>;
const int MAX_N = 205;
mint dp[MAX_N][MAX_N][MAX_N][2];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    Binomial<mint> BINOM;
    int n;
    cin >> n;
    vector<int> a(n);
    for (int& x : a) cin >> x;

    sort(a.rbegin(), a.rend());
    vector<int> len(n);
    int sum = 0;
    for (int i = 0; i < n; i++) {
        len[i] = to_string(a[i]).size();
        sum += len[i];
    }
    vector<mint> power(sum + 1, 1);
    for (int i = 0; i < sum; i++) power[i + 1] = power[i] * 10;

    // vector<vector<mint>> left(n + 1, vector<mint>(n + 1, 0)), right(n + 1, vector<mint>(n + 1, 0));
    // left[0][0] = right[n][0] = 1;
    // for (int i = 0; i < n; i++) {
    //     for (int j = 0; j <= sum; j++) {
    //         mint val = left[i][j];
    //         if (val == 0) continue;
    //         left[i + 1][j] += val * 2;
    //         left[i + 1][j + 1] += val * power[len[i]];
    //     }
    // }
    // for (int i = n - 1; i >= 0; i--) {
    //     for (int j = 0; j <= sum; j++) {
    //         mint val = right[i + 1][j];
    //         if (val == 0) continue;
    //         right[i][j] += val * 2;
    //         right[i][j + 1] += val * power[len[i]];
    //     }
    // }

    // mint ans = 0;
    // for (int i = 0; i < n; i++) {
    //     for (int j = 0; j <= i; j++) {
    //         if (left[i][j] == 0) continue;
    //         for (int k = 0; k <= n - 1 - i; k++) {
    //             ans += left[i][j] * right[i + 1][k] * a[i]*BINOM.P(j+k)*BINOM.P(N)
    //         }
    //     }
    // }

    dp[0][0][0][0] = 1;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j <= i; j++) {
            for (int k = 0; j + k <= i; k++) {
                for (int l = 0; l < 2; l++) {
                    mint val = dp[i][j][k][l];
                    if (val == 0) continue;
                    dp[i + 1][j][k][l] += val;
                    if (l == 0) dp[i + 1][j][k][1] += val * a[i];
                    dp[i + 1][j + 1][k][l] += val;
                    dp[i + 1][j][k + 1][l] += val * power[len[i]];
                }
            }
        }
    }
    mint ans = 0;
    for (int j = 0; j <= n; j++) {
        for (int k = 0; j + k <= n; k++) {
            ans += dp[n][j][k][1] * BINOM.fac(j) * BINOM.fac(k);
        }
    }
    if (a.back() == 0) {
        for (int j = 0; j <= n; j++) {
            for (int k = 0; j + k <= n; k++) {
                ans -= dp[n - 1][j][k][1] * BINOM.fac(j) * BINOM.fac(k);
            }
        }
    }

    cout << ans << '\n';
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 67436
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;

#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

template<class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
tree_order_statistics_node_update>;
// usage:
//   ordered_set::find_by_order (access)
//   ordered_set::order_of_key (lower_bound)

template<class K, class V>
using hashmap = gp_hash_table<K, V>;

#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define ALL(x) begin(x), end(x)
#define DUMP(x) cerr << #x << " = " << (x) << endl
//#define ASSERT(x) while (!(x)) {}
#define ASSERT(x) assert(x)

struct fast_ios {
    fast_ios() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(20);
    };
} fast_ios_;

using ll = long long;

template <class T> bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T> bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <class T> ostream& operator<<(ostream& os, const vector<T>& v);
template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p);
template <class T, class U>
ostream& operator<<(ostream& os, const map<T, U>& mp);

template <class T> ostream& operator<<(ostream& os, const vector<T>& v) {
    os << '[';
    REP(i, v.size()) {
        if (i) os << ',';
        os << v[i];
    }
    return os << ']';
}

template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    return os << '(' << p.first << ' ' << p.second << ')';
}

template <class T> ostream& operator<<(ostream& os, const set<T>& st) {
    os << '{';
    int a = 0;
    for (const auto& e : st) {
        if (a) os << ' ';
        a = 1;
        os << e;
    }
    return os << '}';
}

template <class T, class U>
ostream& operator<<(ostream& os, const map<T, U>& mp) {
    os << '{';
    int a = 0;
    for (const auto& p : mp) {
        if (a) os << ' ';
        a = 1;
        os << p;
    }
    return os << '}';
}

const int INF = numeric_limits<int>::max();
const ll LINF = numeric_limits<ll>::max();


template<typename T>
struct Combination {
    std::vector<T> _fact, _rfact, _inv;

    explicit Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {
        _fact[0] = _rfact[sz] = _inv[0] = 1;
        for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;
        _rfact[sz] /= _fact[sz];
        for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);
        for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];
    }

    inline T fact(int k) const { return _fact[k]; }

    inline T rfact(int k) const { return _rfact[k]; }

    inline T inv(int k) const { return _inv[k]; }

    T P(int n, int r) const {
        if(r < 0 || n < r) return 0;
        return fact(n) * rfact(n - r);
    }

    T C(int p, int q) const {
        if(q < 0 || p < q) return 0;
        return fact(p) * rfact(q) * rfact(p - q);
    }

    T H(int n, int r) const {
        if(n < 0 || r < 0) return (0);
        return r == 0 ? 1 : C(n + r - 1, r);
    }
};


#include <cassert>
#include <numeric>
#include <type_traits>

#ifdef _MSC_VER
#include <intrin.h>
#endif


#include <utility>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

constexpr long long safe_mod(long long x, long long m) {
    x %= m;
    if (x < 0) x += m;
    return x;
}

struct barrett {
    unsigned int _m;
    unsigned long long im;

    explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}

    unsigned int umod() const { return _m; }

    unsigned int mul(unsigned int a, unsigned int b) const {

        unsigned long long z = a;
        z *= b;
#ifdef _MSC_VER
        unsigned long long x;
        _umul128(z, im, &x);
#else
        unsigned long long x =
            (unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
        unsigned int v = (unsigned int)(z - x * _m);
        if (_m <= v) v += _m;
        return v;
    }
};

constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
    if (m == 1) return 0;
    unsigned int _m = (unsigned int)(m);
    unsigned long long r = 1;
    unsigned long long y = safe_mod(x, m);
    while (n) {
        if (n & 1) r = (r * y) % _m;
        y = (y * y) % _m;
        n >>= 1;
    }
    return r;
}

constexpr bool is_prime_constexpr(int n) {
    if (n <= 1) return false;
    if (n == 2 || n == 7 || n == 61) return true;
    if (n % 2 == 0) return false;
    long long d = n - 1;
    while (d % 2 == 0) d /= 2;
    constexpr long long bases[3] = {2, 7, 61};
    for (long long a : bases) {
        long long t = d;
        long long y = pow_mod_constexpr(a, t, n);
        while (t != n - 1 && y != 1 && y != n - 1) {
            y = y * y % n;
            t <<= 1;
        }
        if (y != n - 1 && t % 2 == 0) {
            return false;
        }
    }
    return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);

constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
    a = safe_mod(a, b);
    if (a == 0) return {b, 0};

    long long s = b, t = a;
    long long m0 = 0, m1 = 1;

    while (t) {
        long long u = s / t;
        s -= t * u;
        m0 -= m1 * u;  // |m1 * u| <= |m1| * s <= b


        auto tmp = s;
        s = t;
        t = tmp;
        tmp = m0;
        m0 = m1;
        m1 = tmp;
    }
    if (m0 < 0) m0 += b / s;
    return {s, m0};
}

constexpr int primitive_root_constexpr(int m) {
    if (m == 2) return 1;
    if (m == 167772161) return 3;
    if (m == 469762049) return 3;
    if (m == 754974721) return 11;
    if (m == 998244353) return 3;
    int divs[20] = {};
    divs[0] = 2;
    int cnt = 1;
    int x = (m - 1) / 2;
    while (x % 2 == 0) x /= 2;
    for (int i = 3; (long long)(i)*i <= x; i += 2) {
        if (x % i == 0) {
            divs[cnt++] = i;
            while (x % i == 0) {
                x /= i;
            }
        }
    }
    if (x > 1) {
        divs[cnt++] = x;
    }
    for (int g = 2;; g++) {
        bool ok = true;
        for (int i = 0; i < cnt; i++) {
            if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
                ok = false;
                break;
            }
        }
        if (ok) return g;
    }
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);

unsigned long long floor_sum_unsigned(unsigned long long n,
                                      unsigned long long m,
                                      unsigned long long a,
                                      unsigned long long b) {
    unsigned long long ans = 0;
    while (true) {
        if (a >= m) {
            ans += n * (n - 1) / 2 * (a / m);
            a %= m;
        }
        if (b >= m) {
            ans += n * (b / m);
            b %= m;
        }

        unsigned long long y_max = a * n + b;
        if (y_max < m) break;
        n = (unsigned long long)(y_max / m);
        b = (unsigned long long)(y_max % m);
        std::swap(m, a);
    }
    return ans;
}

}  // namespace internal

}  // namespace atcoder


#include <cassert>
#include <numeric>
#include <type_traits>

namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value ||
                                  std::is_same<T, __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int128 =
    typename std::conditional<std::is_same<T, __uint128_t>::value ||
                                  std::is_same<T, unsigned __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using make_unsigned_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value,
                              __uint128_t,
                              unsigned __int128>;

template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
                                                  is_signed_int128<T>::value ||
                                                  is_unsigned_int128<T>::value,
                                              std::true_type,
                                              std::false_type>::type;

template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
                                                 std::is_signed<T>::value) ||
                                                    is_signed_int128<T>::value,
                                                std::true_type,
                                                std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<(is_integral<T>::value &&
                               std::is_unsigned<T>::value) ||
                                  is_unsigned_int128<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
    is_signed_int128<T>::value,
    make_unsigned_int128<T>,
    typename std::conditional<std::is_signed<T>::value,
                              std::make_unsigned<T>,
                              std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
    typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<is_integral<T>::value &&
                                  std::is_unsigned<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
                                              std::make_unsigned<T>,
                                              std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

}  // namespace internal

}  // namespace atcoder


namespace atcoder {

namespace internal {

struct modint_base {};
struct static_modint_base : modint_base {};

template <class T> using is_modint = std::is_base_of<modint_base, T>;
template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;

}  // namespace internal

template <int m, std::enable_if_t<(1 <= m)>* = nullptr>
struct static_modint : internal::static_modint_base {
    using mint = static_modint;

  public:
    static constexpr int mod() { return m; }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    static_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    static_modint(T v) {
        long long x = (long long)(v % (long long)(umod()));
        if (x < 0) x += umod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    static_modint(T v) {
        _v = (unsigned int)(v % umod());
    }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v -= rhs._v;
        if (_v >= umod()) _v += umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        unsigned long long z = _v;
        z *= rhs._v;
        _v = (unsigned int)(z % umod());
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        if (prime) {
            assert(_v);
            return pow(umod() - 2);
        } else {
            auto eg = internal::inv_gcd(_v, m);
            assert(eg.first == 1);
            return eg.second;
        }
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static constexpr unsigned int umod() { return m; }
    static constexpr bool prime = internal::is_prime<m>;
};

template <int id> struct dynamic_modint : internal::modint_base {
    using mint = dynamic_modint;

  public:
    static int mod() { return (int)(bt.umod()); }
    static void set_mod(int m) {
        assert(1 <= m);
        bt = internal::barrett(m);
    }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    dynamic_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        long long x = (long long)(v % (long long)(mod()));
        if (x < 0) x += mod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        _v = (unsigned int)(v % mod());
    }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v += mod() - rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        _v = bt.mul(_v, rhs._v);
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        auto eg = internal::inv_gcd(_v, mod());
        assert(eg.first == 1);
        return eg.second;
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static internal::barrett bt;
    static unsigned int umod() { return bt.umod(); }
};
template <int id> internal::barrett dynamic_modint<id>::bt(998244353);

using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
using modint = dynamic_modint<-1>;

namespace internal {

template <class T>
using is_static_modint = std::is_base_of<internal::static_modint_base, T>;

template <class T>
using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;

template <class> struct is_dynamic_modint : public std::false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};

template <class T>
using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;

}  // namespace internal

}  // namespace atcoder

using namespace atcoder;

int main() {
    int n; cin >> n;
    vector<int> a(n);
    vector<int> len(n);
    REP(i, n) {
        string s; cin >> s;
        len[i] = s.size();
        a[i] = stoi(s);
    }


    int len_sum = accumulate(ALL(len), 0);

    int total_zero = 0;
    REP(i, n) total_zero += (a[i] == 0);

    using mint = modint1000000007;
    using vi = vector<mint>;
    using vvi = vector<vi>;
    using vvvi = vector<vvi>;

    mint ans = 0;

    Combination<mint> comb(n+1);

    REP(i, n) {
        vvvi dp(n+1, vvi(n, vi(total_zero+1)));
        dp[0][0][0] = 1;
        REP(j, n) {
            if (j == i) {
                dp[j+1] = dp[j];
                continue;
            }

            REP(k, n) {
                // k := 選んだ個数
                REP(l, total_zero+1) {
                    // l := 選んだ 0 の個数

                    // 使わない
                    dp[j+1][k][l] += dp[j][k][l];
                    // 使う
                    if (k+1 < n and l + (a[j] == 0) <= total_zero) {
                        dp[j+1][k+1][l + (a[j] == 0)] += dp[j][k][l] * mint(10).pow(len[j]);
                    }
                }
            }
        }

        mint total = 0;
        REP(k, n) {
            // k := 選んだ個数

            mint tmp = 0;

            for (int l = 1; l <= n-1-k; l++) {
                tmp += comb.P(n-1-k, l);
            }

            REP(l, total_zero+1) {
                // l := 選んだ 0 の個数

                if (dp[n][k][l].val() == 0) continue;

                if (n-1-k == 0) {
                    total += dp[n][k][l] * comb.fact(k);
                } else {
                    mint ratio = 1 + tmp * (n - 1 - k - (total_zero - (a[i] == 0) - l)) / (n - 1 - k);
                    total += dp[n][k][l] * ratio * comb.fact(k);
                }
            }
        }

        ans += total * a[i];
    }

    cout << ans.val() << endl;
};
accuracy: 1
time_ms: 320
memory_kb: 5612
final_rank: 4

code_snippet: #include<bits/stdc++.h> 
using namespace std;
using ll=long long int;
//using Int=__int128;
#define ALL(A) A.begin(),A.end()
template<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}
template<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}
template<typename T> constexpr int bitUP(T x,int a){return (x>>a)&1;}
//→　↓　←　↑ 
int dh[4]={0,1,0,-1}, dw[4]={1,0,-1,0};
//右上から時計回り
//int dh[8]={-1,0,1,1,1,0,-1,-1}, dw[8]={1,1,1,0,-1,-1,-1,0};
long double EPS = 1e-6;
long double PI = acos(-1);
const ll INF=(1LL<<62);
const int MAX=(1<<30);
constexpr ll MOD=1000000000+7;
//constexpr ll MOD=998244353;


inline void bin101(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(20);
}

using pii=pair<int,int>;
using pil=pair<int,ll>;
using pli=pair<ll,int>;
using pll=pair<ll,ll>;
using psi=pair<string,int>;
using pis=pair<int,string>;
using psl=pair<string,ll>;
using pls=pair<ll,string>;
using pss=pair<string,string>;

using Graph=vector<vector<int>>;

template<typename T >
struct edge {
	int to;
	T cost;
	edge()=default;
	edge(int to, T cost) : to(to), cost(cost) {}

};
template<typename T>
using WeightGraph=vector<vector<edge<T>>>;

template<typename T>
void CinGraph(int M,WeightGraph<T> &g,bool directed=false,bool index1=true){
    while(M--){
        int s,t;
        T cost;
        cin>>s>>t>>cost;
        if(index1) s--,t--;
        g[s].emplace_back(t,cost);
        if(not directed) g[t].emplace_back(s,cost);
    }
}

void CinGraph(int M,Graph &g,bool directed=false,bool index1=true){
    while(M--){
        int s,t;
        cin>>s>>t;
        if(index1) s--,t--;
        g[s].push_back(t);
        if(not directed) g[t].push_back(s);
    }
}


//0-indexed vector cin
template<typename T>
inline istream &operator>>(istream &is,vector<T> &v) {
    for(size_t i=0;i<v.size();i++) is>>v[i];
	return is;
}
 
//0-indexed vector cin
template<typename T>
inline istream &operator>>(istream &is,vector<vector<T>> &v) {
    for(size_t i=0;i<v.size();i++){
        is>>v[i];
    }
    return is;
}
//vector cout
template<typename T>
inline ostream &operator<<(ostream &os,const vector<T> &v) {
    for(size_t i=0;i<v.size();i++){
        if(i) os<<" ";
        os<<v[i];
    }
    return os;
}
//vector<vector> cout
template<typename T>
inline ostream &operator<<(ostream &os,const vector<vector<T>> &v) {
    for(size_t i=0;i<v.size();i++){
        os<<v[i];
        if(i+1!=v.size()) os<<"\n";
    }
    return os;
}

//Graph out
template<typename T>
inline ostream &operator<<(ostream &os,const Graph &g) {
    for(size_t i=0;i<g.size();i++){
        for(int to:g[i]){
            os<<i<<"->"<<to<<" ";
        }
        os<<endl;
    }
    return os;
}

//WeightGraph out
template<typename T>
inline ostream &operator<<(ostream &os,const WeightGraph<T> &g) {
    for(size_t i=0;i<g.size();i++){
        for(auto e:g[i]){
            os<<i<<"->"<<e.to<<"("<<e.cost<<") ";
        }
        os<<endl;
    }
    return os;
}


//要素数n 初期値x
template<typename T>
inline vector<T> vmake(size_t n,T x){
	return vector<T>(n,x);
}

//a,b,c,x data[a][b][c] 初期値x
template<typename... Args>
auto vmake(size_t n,Args... args){
	auto v=vmake(args...);
	return vector<decltype(v)>(n,move(v));
}
template<typename V,typename T>
void fill(V &v,const T value){
    v=value;
}

template<typename V,typename T>
void fill(vector<V> &vec,const T value){
    for(auto &v:vec) fill(v,value);
}

//pair cout
template<typename T, typename U>
inline ostream &operator<<(ostream &os,const pair<T,U> &p) {
	os<<p.first<<" "<<p.second;
	return os;
}
 
//pair cin
template<typename T, typename U>
inline istream &operator>>(istream &is,pair<T,U> &p) {
	is>>p.first>>p.second;
	return is;
}
 
//ソート
template<typename T>
inline void vsort(vector<T> &v){
    sort(v.begin(),v.end());
}
 
//逆順ソート
template<typename T>
inline void rvsort(vector<T> &v){
	sort(v.rbegin(),v.rend());
}

//1ビットの数を返す
inline int popcount(int x){
	return __builtin_popcount(x);
}
//1ビットの数を返す
inline int popcount(ll x){
	return __builtin_popcountll(x);
}
template<typename T>
inline void Compress(vector<T> &C){
    sort(C.begin(),C.end());
    C.erase(unique(C.begin(),C.end()),C.end());
}
template<typename T>
inline int lower_idx(const vector<T> &C,T value){
    return lower_bound(C.begin(),C.end(),value)-C.begin();
}
template<typename T>
inline int upper_idx(const vector<T> &C,T value){
    return upper_bound(C.begin(),C.end(),value)-C.begin();
}
//時計回りに90度回転
template<typename T>
inline void rotate90(vector<vector<T>> &C){
    vector<vector<T>> D(C[0].size(),vector<T>(C.size()));
    for(int h=0;h<C.size();h++){
        for(int w=0;w<C[h].size();w++){
            D[w][C.size()-1-h]=C[h][w];
        }
    }
    C=D;
}
//補グラフを返す
//i→iのような辺は加えない
Graph ComplementGraph(const Graph &g){
    size_t sz=g.size();
    bool used[sz][sz];
    fill(used[0],used[sz],false);
    for(size_t i=0;i<sz;i++){
        for(int to:g[i]){
            used[i][to]=true;
        }
    }
    Graph ret(sz);
    for(size_t i=0;i<sz;i++){
        for(size_t j=0;j<sz;j++){
            if(used[i][j]) continue;
            if(i==j) continue;
            ret[i].push_back(j);
        }
    }
    return ret;
}
//グラフの分解 secondはある頂点がどこに対応するか id[i]={2,3}のとき,頂点iはret[2][3]に対応
//無効グラフのみに対応
pair<vector<Graph>,vector<pair<int,int>>> GraphDecomposition(const Graph &g){
    vector<pair<int,int>> id(g.size(),pair<int,int>(-1,-1));
    vector<Graph> ret;
    vector<int> now;
    for(size_t i=0;i<g.size();i++){
        if(id[i].first!=-1) continue;
        id[i].first=ret.size();
        id[i].second=0;
        now.push_back(i);
        for(size_t j=0;j<now.size();j++){
            for(int to:g[now[j]]){
                if(id[to].first==-1){
                    id[to].first=ret.size();
                    id[to].second=now.size();
                    now.push_back(to);
                }
            }
        }
        Graph r(now.size());
        for(size_t j=0;j<now.size();j++){
            r[j]=g[now[j]];
            for(int &to:r[j]){
                to=id[to].second;
            }
        }
        ret.push_back(r);
        now.clear();
    }
    return make_pair(ret,id);
}
//0indexを想定
bool OutGrid(ll h,ll w,ll H,ll W){
    return (h>=H or w>=W or h<0 or w<0);
}

void NO(){
    cout<<"NO"<<"\n";
}
void YES(){
    cout<<"YES"<<"\n";
}
void No(){
    cout<<"No"<<"\n";
}
void Yes(){
    cout<<"Yes"<<"\n";
}
namespace overflow{
    template<typename T>
    T max(){
        return numeric_limits<T>::max();
    }
    template<typename T>
    T ADD(T a,T b){
        T res;
        return __builtin_add_overflow(a,b,&res)?max<T>():res;
    }
    template<typename T>
    T MUL(T a,T b){
        T res;
        return __builtin_mul_overflow(a,b,&res)?max<T>():res;
    }
};
using namespace overflow;
struct ModInt{
    using u64=uint_fast64_t;
    u64 a;
    constexpr ModInt() :a(0){}
    constexpr ModInt(ll x) :a((x>=0)?(x%MOD):(x%MOD+MOD) ) {}

    inline constexpr ModInt operator+(const ModInt rhs)const noexcept{
        return ModInt(*this)+=rhs;
    }
    inline constexpr ModInt operator-(const ModInt rhs)const noexcept{
        return ModInt(*this)-=rhs;
    }
    inline constexpr ModInt operator*(const ModInt rhs)const noexcept{
        return ModInt(*this)*=rhs;
    }
    inline constexpr ModInt operator/(const ModInt rhs)const noexcept{
        return ModInt(*this)/=rhs;
    }
    inline constexpr ModInt operator+(const ll rhs) const noexcept{
        return ModInt(*this)+=ModInt(rhs);
    }
    inline constexpr ModInt operator-(const ll rhs)const noexcept{
        return ModInt(*this)-=ModInt(rhs);
    }
    inline constexpr ModInt operator*(const ll rhs)const noexcept{
        return ModInt(*this)*=ModInt(rhs);
    }
    inline constexpr ModInt operator/(const ll rhs)const noexcept{
        return ModInt(*this)/=ModInt(rhs);
    }

    inline constexpr ModInt &operator+=(const ModInt rhs)noexcept{
        a+=rhs.a;
        if(a>=MOD) a-=MOD;
        return *this;
    }
    inline constexpr ModInt &operator-=(const ModInt rhs)noexcept{
        if(rhs.a>a) a+=MOD;
        a-=rhs.a;
        return *this;
    }
    inline constexpr ModInt &operator*=(const ModInt rhs)noexcept{
        a=(a*rhs.a)%MOD;
        return *this;
    }
    inline constexpr ModInt &operator/=(ModInt rhs)noexcept{
        a=(a*rhs.inverse().a)%MOD;
        return *this;
    }
    inline constexpr ModInt &operator+=(const ll rhs)noexcept{
        return *this+=ModInt(rhs);
    }
    inline constexpr ModInt &operator-=(const ll rhs)noexcept{
        return *this-=ModInt(rhs);
    }
    inline constexpr ModInt &operator*=(const ll rhs)noexcept{
        return *this*=ModInt(rhs);
    }
    inline constexpr ModInt &operator/=(const ll rhs)noexcept{
        return *this/=ModInt(rhs);
    }

    inline constexpr ModInt operator=(const ll x)noexcept{
        a=(x>=0)?(x%MOD):(x%MOD+MOD);
        return *this;
    }

    inline constexpr bool operator==(const ModInt p)const noexcept{
        return a==p.a;
    }

    inline constexpr bool operator!=(const ModInt p)const noexcept{
        return a!=p.a;
    }

    inline constexpr ModInt pow(ll N) const noexcept{
        ModInt ans(1LL),p(a);
        while(N>0){
            if(bitUP(N,0)){
                ans*=p;
            }
            p*=p;
            N>>=1;
        }
        return ans;
    }
    inline constexpr ModInt inverse() const noexcept{
        return pow(MOD-2);
    }

};
inline constexpr ModInt operator+(const ll &a,const ModInt &b)noexcept{
    return ModInt(a)+=b;
}
inline constexpr ModInt operator-(const ll &a,const ModInt &b)noexcept{
    return ModInt(a)-=b;
}
inline constexpr ModInt operator*(const ll &a,const ModInt &b)noexcept{
    return ModInt(a)*=b;
}
inline constexpr ModInt operator/(const ll &a,const ModInt &b)noexcept{
    return ModInt(a)/=b;
}
//cout
inline ostream &operator<<(ostream &os,const ModInt &p) {
    return os<<p.a;
}

//cin
inline istream &operator>>(istream &is,ModInt &p) {
    ll t;
    is>>t;
    p=t;
    return is;
}

struct Binominal{
    vector<ModInt> fac,finv,inv; //fac[n]:n! finv:(n!)の逆元
    int sz;
    Binominal(int n=10) :sz(1){
        if(n<=0) n=10;
        init(n);
    }
    inline void init(int n){
        fac.resize(n+1,1);
        finv.resize(n+1,1);
        inv.resize(n+1,1);
        for(int i=sz+1;i<=n;i++){
            fac[i]=fac[i-1]*i;
            inv[i]=MOD-inv[MOD%i]*(MOD/i);
            finv[i]=finv[i-1]*inv[i];
        }
        sz=n;
    }
    //nCk(n,k<=N) をO(1)で求める
    inline ModInt com(int n,int k){
        if(n<k) return ModInt(0);
        if(n<0 || k<0) return ModInt(0);
        if(n>sz) init(n);
        return fac[n]*finv[k]*finv[n-k];
    }
    //nCk(k<=N) をO(k)　で求める 
    inline ModInt rcom(ll n,int k){
        if(n<0 || k<0 || n<k) return ModInt(0);
        if(k>sz) init(k);
        ModInt ret(1);
        for(int i=0;i<k;i++){
            ret*=n-i;
        }
        ret*=finv[k];
        return ret;
    }

    //重複組み合わせ n種類のものから重複を許してk個を選ぶ
    //〇がn個,|がk個
    inline ModInt h(int n,int k){
        return com(n+k-1,k);
    }
};
vector<int> Subset(int S,bool zero=false,bool full=false){
    vector<int> ret;
    int now=(S-1)&S;
    if(full and S){
        ret.push_back(S);
    }
    do{
        ret.push_back(now);
        now=(now-1)&S;
    }while(now!=0);
    if(zero){
        ret.push_back(0);
    }
    return ret;
}
template<typename T>
T SUM(const vector<T> &v,int s,int t){
    chmax(s,0);
    chmin(t,int(v.size())-1);
    if(s>t) return 0;
    if(s==0) return v[t];
    else return v[t]-v[s-1];
}
template<typename T>
void buildSUM(vector<T> &v){
    for(size_t i=1;i<v.size();i++){
        v[i]+=v[i-1];
    }
    return;
}


void solve(){
    int N; cin>>N;
    vector<ModInt> a(N); cin>>a;
    vector<int> sz(N);
    int zero=0;
    for(int i=0;i<N;i++){
        if(a[i]==0){
            zero++;
            break;
        }
    }
    for(int i=0;i<N;i++){
        sz[i]=to_string(a[i].a).size();
    }
    Binominal B(100000);
    vector<ModInt> ten(1e6);
    ten[0]=1;
    for(int i=1;i<ten.size();i++) ten[i]=ten[i-1]*10;
    auto A=vmake(N+1,2,ModInt());
    for(int i=0;i<=N;i++){
        for(int j=0;j<=i;j++){
            A[i][0]+=B.com(i,j)*B.fac[j];
        }
        for(int j=0;j<i;j++){
            auto x=B.com(i-1,j)*B.fac[j];
            A[i][1]+=(x)*(1+j);
        }
    }
    ModInt ans;
    map<pii,ModInt> M;
    for(int i=0;i<N;i++){
        if(M.count(pii(sz[i],a[i]==0))){
            continue;
        }
        pii id=pii(sz[i],a[i]==0);
        //j  k   used
        //i 桁数 個数 ゼロを使ったか:何個か
        auto dp=vmake(2,(N+1)*5,N+1,2,ModInt());
        dp[0][0][0][0]=1;
        for(int j=0;j<N;j++){
            int J=j%2;
            //cerr<<" "<<j<<endl;
            fill(dp[1-J],0);
            for(int k=0;k<(N+1)*5;k++){
                for(int used=0;used<=j;used++){
                    for(int z=0;z<2;z++){
                        //cerr<<J<<endl;
                        if(dp[J][k][used][z].a==0) continue;

                        if(j!=i){
                            dp[1-J][k+sz[j]][used+1][(a[j]==0)|z]+=dp[J][k][used][z];
                        }
                        dp[1-J][k][used][z]+=dp[J][k][used][z];
                    }
                }
            }
        }
        for(int k=0;k<(N+1)*5;k++){
            for(int used=0;used<=N;used++){
                for(int z=0;z<2;z++){
                    auto x=dp[N%2][k][used][z]*ten[k];
                    if(x.a==0) continue;
                    int nokori=N-1-used;
                    if(zero and z==0){
                        M[id]+=x*A[nokori][1]*B.fac[used];
                    }else{
                        M[id]+=x*A[nokori][0]*B.fac[used];
                    }
                }
            }
        }
    }
    for(int i=0;i<N;i++){
        ans+=a[i]*M[pii(sz[i],a[i]==0)];
    }
    cout<<ans<<endl;
}

int main(){
    bin101();
    int T=1;
    //cin>>T;
    while(T--) solve();
}
accuracy: 1
time_ms: 920
memory_kb: 46356
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;

template <int mod = (int)(1e9 + 7)>
struct ModInt {
  int x;
  ModInt() : x(0) {}
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
  ModInt operator-() const { return ModInt(-x); }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  bool operator==(const ModInt &p) const { return x == p.x; }
  bool operator!=(const ModInt &p) const { return x != p.x; }
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
  ModInt pow(int64_t n) const {
    ModInt res(1), mul(x);
    while (n) {
      if (n & 1) res *= mul;
      mul *= mul;
      n >>= 1;
    }
    return res;
  }
  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }
  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }
  static int get_mod() { return mod; }
};

struct Combination {
  vector<ModInt<>> _fact, _rfact, _inv;
  Combination(long long nsize = 5000000)
      : _fact(nsize + 1), _rfact(nsize + 1), _inv(nsize + 1) {
    _fact[0] = _rfact[nsize] = _inv[0] = 1;
    for (int i = 1; i <= nsize; i++) _fact[i] = _fact[i - 1] * i;
    _rfact[nsize] /= _fact[nsize];
    for (int i = nsize - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);
    for (int i = 1; i <= nsize; i++) _inv[i] = _rfact[i] * _fact[i - 1];
  }
  inline ModInt<> fact(int k) const { return _fact[k]; }

  inline ModInt<> rfact(int k) const { return _rfact[k]; }

  inline ModInt<> inv(int k) const { return _inv[k]; }

  ModInt<> P(int n, int r) const {
    if (r < 0 || n < r) return 0;
    return fact(n) * rfact(n - r);
  }

  ModInt<> C(int p, int q) const {
    if (q < 0 || p < q) return 0;
    return fact(p) * rfact(q) * rfact(p - q);
  }

  ModInt<> largeC(long long p, long long q) const {
    if (q < 0 || p < q) return 0;
    if (q >= (long long)_fact.size()) q = p - q;
    // if (q >= (long long)5000) q = p - q;
    ModInt<> res = rfact(q);
    for (int i = 0; i < q; ++i) res *= p - i;
    return res;
  }

  // n types,choose r
  ModInt<> H(int n, int r) const {
    if (n < 0 || r < 0) return (0);
    return r == 0 ? 1 : C(n + r - 1, r);
  }

  ModInt<> largeH(long long n, long long r) const {
    if (n < 0 || r < 0) return (0);
    return r == 0 ? 1 : largeC(n + r - 1, r);
  }

  ModInt<> Catalan(int n) {
    // C(2n,n) / (n + 1)
    return fact(2 * n) * rfact(n + 1) * rfact(n);
  }
};
using mint = ModInt<>;

int n, zero = 0;
vector<int> cnt(5, 0), sum(5, 0);
vector<mint> ten, perm;
Combination com;

mint solve();
mint calc();

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    int len = to_string(a).size();
    ++cnt[len];
    sum[len] += a;
    zero |= !a;
  }
  cout << solve() << endl;
  return 0;
}

mint solve() {
  {
    for (int i = 0; i <= n; ++i) {
      mint now = 0;
      for (int j = 0; j <= i; ++j) now += com.P(i, j);
      perm.push_back(now);
    }
    ten.assign(1, 1);
    for (int i = 0; i < 3000000; ++i) ten.push_back(ten.back() * 10);
  }
  mint res = calc();
  if (zero) {
    --n, --cnt[1];
    res -= calc();
  }
  return res;
}

mint calc() {
  mint res = 0;
  for (int dig = 1; dig <= 4; ++dig)
    if (cnt[dig]) {
      vector<vector<mint>> dp(n + 1, vector<mint>(4 * n + 1, 0));
      --cnt[dig];
      dp[0][0] = 1;
      for (int nd = 1; nd <= 4; ++nd)
        for (int i = n; i >= 0; --i)
          for (int j = 4 * n; j >= 0; --j)
            for (int k = 1; k <= cnt[nd] && i + k <= n && j + k * nd <= 4 * n;
                 ++k)
              dp[i + k][j + k * nd] += dp[i][j] * com.C(cnt[nd], k);
      for (int i = 0; i < n; ++i)
        for (int j = 0; j <= 4 * n; ++j)
          res += perm[n - 1 - i] * sum[dig] * dp[i][j] * ten[j] * com.fact(i);
      ++cnt[dig];
    }
  return res;
}
accuracy: 1
time_ms: 920
memory_kb: 80036
final_rank: 16

code_snippet: #pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = (1<<30)-1;
const ll linf = 1LL<<61;
const int MAX = 510000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    int sz = a.size();
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr;
        sz--;
        if(sz) cout << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
//#include <atcoder/all>
//using namespace atcoder;
const int mod = 1e9 + 7;
//const int mod = 998244353;

template <int mod> struct ModInt {
    int x;

    ModInt() : x(0) {}

    ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

    ModInt &operator+=(const ModInt &p) {
        if((x += p.x) >= mod) x -= mod;
        return *this;
    }

    ModInt &operator-=(const ModInt &p) {
        if((x += mod - p.x) >= mod) x -= mod;
        return *this;
    }

    ModInt &operator*=(const ModInt &p) {
        x = (int)(1LL * x * p.x % mod);
        return *this;
    }

    ModInt &operator/=(const ModInt &p) {
        *this *= p.inverse();
        return *this;
    }

    ModInt operator-() const { return ModInt(-x); }

    ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

    ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

    ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

    ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

    bool operator==(const ModInt &p) const { return x == p.x; }

    bool operator!=(const ModInt &p) const { return x != p.x; }

    ModInt inverse() const {
        int a = x, b = mod, u = 1, v = 0, t;
        while(b > 0) {
            t = a / b;
            swap(a -= t * b, b);
            swap(u -= t * v, v);
        }
        return ModInt(u);
    }

    ModInt pow(int64_t n) const {
        ModInt ret(1), mul(x);
        while(n > 0) {
            if(n & 1) ret *= mul;
            mul *= mul;
            n >>= 1;
        }
        return ret;
    }

    friend ostream &operator<<(ostream &os, const ModInt &p) {
        return os << p.x;
    }

    friend istream &operator>>(istream &is, ModInt &a) {
        int64_t t;
        is >> t;
        a = ModInt<mod>(t);
        return (is);
    }

    static int get_mod() { return mod; }
};

using mint = ModInt<mod>;

vector<ll> fac(MAX), finv(MAX), inv(MAX);

void comInit(){
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for(ll i=2; i<MAX; i++){
        fac[i] = fac[i-1]*i % mod;
        inv[i] = mod - inv[mod%i] * (mod/i) % mod;
        finv[i] = finv[i-1] * inv[i] % mod;
    }
}


ll com(ll n, ll k){
    if(n < k) return 0;
    if(n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n-k] % mod) % mod;
}

mint dp[202][202];
mint ep[202][202];
mint mul[202];
mint p[10];

int main(){
    int n; cin >> n;
    vl a(n); rep(i,n) cin >> a[i];
    comInit();
    sort(all(a));
    int zero = 0;
    dp[0][0] = ep[n][0] = 1;
    p[0] = 1;
    rep(i,8) p[i+1] = p[i] * 10;
    rep(i,n+1) rep(j,i+1) mul[i] += com(i,j) * fac[j] % mod;
    rep(i,n){
        int sz = to_string(a[i]).size();
        rep(j,n+1){
            dp[i+1][j] += dp[i][j];
            if(a[i] != 0) dp[i+1][j+1] += dp[i][j] * p[sz];
        }
    }
    for(int i=n-1; i>=0; i--){
        int sz = to_string(a[i]).size();
        rep(j,n+1){
            ep[i][j] += ep[i+1][j];
            ep[i][j+1] += ep[i+1][j] * p[sz];
        }
    }
    mint ans = 0, mi = 0;
    rep(i,n){
        if(a[i] == 0){
            zero++;
            continue;
        }
        rep(j,n+1) rep(k,n-j){
            mint r1 = dp[i][j] * ep[i+1][k] * 10 * fac[j+k+1];
            mint r2 = dp[i][j] * ep[i+1][k] * fac[j+k]; 
            if(zero){
                if(n-j-k-2>=0) ans += r1 * mul[n-j-k-2] * a[i];
            }
            ans += r2 * mul[n-j-k-1] * a[i];
            if(n-j-k-2>=0 && zero) mi += r2 * mul[n-j-k-2] * a[i];
        }
    }
    cout << ans - mi << endl;
}
accuracy: 1
time_ms: 70
memory_kb: 15188
final_rank: 1

code_snippet: /*	author:  Kite_kuma
	created: 2021.09.09 18:00:06 */

// #ifdef LOCAL
// #define _GLIBCXX_DEBUG
// #endif
#include <bits/stdc++.h>
using namespace std;

#pragma region macros

#define foa(s, v) for(auto &s : v)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define popcnt(n) __builtin_popcountll((long long)n)

#define REPname(a, b, c, d, e, ...) e
#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)
#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)
#define REP1(i, x) for(int i = 0; i < (x); ++i)
#define REP2(i, l, r) for(int i = (l); i < (r); ++i)
#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))

#define DREPname(a, b, c, d, e, ...) e
#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)
#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)
#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)
#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))

#pragma endregion

#pragma region aliases

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = std::vector<int>;
using vvi = std::vector<std::vector<int>>;
using vvvi = std::vector<std::vector<std::vector<int>>>;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vvvll = std::vector<vvll>;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
template <class T = ll>
using V = std::vector<T>;
template <class T = ll>
using VV = V<V<T>>;
template <class T = ll>
using VVV = V<V<V<T>>>;
template <class T = ll>
using pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T = ll>
using pqdn = std::priority_queue<T>;

#pragma endregion

#pragma region constants

const int inf = 1e9;
const long long INF = 1e18;
const long double pi = acos(-1);
const char dl = '\n';
const char sp = ' ';
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

const int mod_1000000007 = 1000000007;
const int mod_998244353 = 998244353;

#pragma endregion

#pragma region basic_operation

template <class T0, class T1, class T2>
inline bool in_range(T0 x, T1 lef, T2 rig) {
	return ((lef <= x) && (x < rig));
}

template <class T>
inline bool chmin(T &a, T b) {
	if(a > b) {
		a = b;
		return true;
	}
	return false;
}
template <class T>
inline bool chmax(T &a, T b) {
	if(a < b) {
		a = b;
		return true;
	}
	return false;
}

void Yes(bool f = 1) { std::cout << (f ? "Yes" : "No") << '\n'; }
void No() { std::cout << "No\n"; }
void YES(bool f = 1) { std::cout << (f ? "YES" : "NO") << '\n'; }
void NO() { std::cout << "NO\n"; }

template <class T>
void drop(T answer) {
	std::cout << answer << '\n';
	exit(0);
}

void err(bool flag = true) {
	if(!flag) return;
	std::cout << -1 << '\n';
	exit(0);
}

template <class T>
void vout(std::vector<T> const &v, bool vertically = 0) {
	if(vertically) {
		for(auto const &a : v) {
			std::cout << a << '\n';
		}
		return;
	}
	for(auto it = v.begin(); it != v.end(); it++) {
		std::cout << (*it);
		if(it != v.end() - 1) {
			std::cout << ' ';
		}
	}
	std::cout << '\n';
	return;
}

inline void print() { std::cout << '\n'; }
template <class T>
inline void print(T x) {
	std::cout << x << '\n';
	return;
}
template <typename Head, typename... Tail>
void print(Head H, Tail... T) {
	std::cout << H << " ";
	print(T...);
}

template <class T>
void add(std::vector<T> &v, T val) {
	for(auto &a : v) a += val;
	return;
}

template <class T>
T dup(T a, T b) {
	assert(b != 0);
	return (a + b - 1) / b;
}

template <class T>
T greatest_lower_multiple(T x, T d) {
	if(d == 0) return 0;
	if(d < 0) d *= -1;
	T y = x % d;
	if(y < 0) y += d;
	return x - y;
}

template <class T>
T least_upper_multiple(T x, T d) {
	return -greatest_lower_multiple(-x, d);
}

long long POW(long long a, long long n) {
	long long res = 1;
	while(n > 0) {
		if(n & 1) res = res * a;
		a = a * a;
		n >>= 1;
	}
	return res;
}

long long modpow(long long a, long long n, long long mod) {	 // a^n mod
	assert(n >= 0 && mod);
	if(mod == 1) return 0LL;
	long long res = 1;
	a %= mod;
	while(n > 0) {
		if(n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res < 0 ? res + mod : res;
}

// return x which satisfies a * x % mod == gcd(a, mod)
// not (mod | a)
long long modinv(long long a, long long mod) {
	long long b = mod, u = 1, v = 0;
	while(b) {
		long long t = a / b;
		a -= t * b;
		std::swap(a, b);
		u -= t * v;
		std::swap(u, v);
	}
	u %= mod;
	if(u < 0) u += mod;
	return u;
}

template <class T>
int lb(const std::vector<T> &a, const T x) {
	return std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));
}
template <class T>
int ub(const std::vector<T> &a, const T x) {
	return std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));
}
template <class T>
void unq_sort(std::vector<T> &a) {
	std::sort(a.begin(), a.end());
	a.erase(std::unique(a.begin(), a.end()), a.end());
}
template <class T>
std::vector<int> press(std::vector<T> &a) {
	auto vec = a;
	unq_sort(vec);
	std::vector<int> ret;
	for(auto &v : a) ret.push_back(lb(vec, v));
	return ret;
}

#pragma endregion

#pragma region input
#define VEC(type, name, size) \
	vector<type> name(size);  \
	scanner::INPUT(name)
#define VVEC(type, name, h, w)                     \
	vector<vector<type>> name(h, vector<type>(w)); \
	scanner::INPUT(name)
#define INT(...)     \
	int __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LL(...)            \
	long long __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define STR(...)        \
	string __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define CHR(...)      \
	char __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define DBL(...)        \
	double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LD(...)              \
	long double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define TPL3(type0, type1, type2, name)   \
	std::tuple<type0, type1, type2> name; \
	scanner::INPUT(name);
#define TPL4(type0, type1, type2, type3, name)   \
	std::tuple<type0, type1, type2, type3> name; \
	scanner::INPUT(name);

namespace scanner {
template <class T>
void scan(T &a) {
	std::cin >> a;
}

template <class T, class L>
void scan(std::pair<T, L> &p) {
	scan(p.first);
	scan(p.second);
}

template <class T0, class T1, class T2>
void scan(std::tuple<T0, T1, T2> &p) {
	T0 t0;
	T1 t1;
	T2 t2;
	scan(t0);
	scan(t1);
	scan(t2);
	p = std::make_tuple(t0, t1, t2);
}

template <class T0, class T1, class T2, class T3>
void scan(std::tuple<T0, T1, T2, T3> &p) {
	T0 t0;
	T1 t1;
	T2 t2;
	T3 t3;
	scan(t0);
	scan(t1);
	scan(t2);
	scan(t3);
	p = std::make_tuple(t0, t1, t2, t3);
}

template <class T>
void scan(std::vector<T> &a) {
	for(auto &i : a) scan(i);
}

void INPUT() {}
template <class Head, class... Tail>
void INPUT(Head &head, Tail &... tail) {
	scan(head);
	INPUT(tail...);
}
}  // namespace scanner

template <typename T1, typename T2>
std::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {
	is >> p.first >> p.second;
	return is;
}
#pragma endregion

#pragma region debug

#pragma region output
template <typename T1, typename T2>
std::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {
	os << p.first << " " << p.second;
	return os;
}
template <class T>
std::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {
	for(int i = 0; i < (int)v.size(); i++) {
		if(i) os << " ";
		os << v[i];
	}
	return os;
}
#pragma endregion

#pragma region view

namespace viewer {
void view(const long long e) {
	if(e == INF)
		std::cerr << "INF";
	else if(e == -INF)
		std::cerr << "-INF";
	else
		std::cerr << e;
}

void view(const int e) {
	if(e == inf)
		std::cerr << "inf";
	else if(e == -inf)
		std::cerr << "-inf";
	else
		std::cerr << e;
}

template <typename T>
void view(const T e) {
	std::cerr << e;
}

template <typename T, typename U>
void view(const std::pair<T, U> &p) {
	std::cerr << "(";
	view(p.first);
	std::cerr << ", ";
	view(p.second);
	std::cerr << ")";
}

template <class T0, class T1, class T2>
void view(const std::tuple<T0, T1, T2> &p) {
	std::cerr << "(";
	view(std::get<0>(p));
	std::cerr << ", ";
	view(std::get<1>(p));
	std::cerr << ", ";
	view(std::get<2>(p));
	std::cerr << ")";
}

template <class T0, class T1, class T2, class T3>
void view(const std::tuple<T0, T1, T2, T3> &p) {
	std::cerr << "(";
	view(std::get<0>(p));
	std::cerr << ", ";
	view(std::get<1>(p));
	std::cerr << ", ";
	view(std::get<2>(p));
	std::cerr << ", ";
	view(std::get<3>(p));
	std::cerr << ")";
}

template <typename T>
void view(const std::set<T> &s) {
	if(s.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(auto &t : s) {
		view(t);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::unordered_set<T> &s) {
	if(s.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(auto &t : s) {
		view(t);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<T> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(const auto &e : v) {
		view(e);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<std::vector<T>> &vv) {
	std::cerr << "{\n";
	for(const auto &v : vv) {
		std::cerr << "\t";
		view(v);
		std::cerr << '\n';
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::vector<std::pair<T, U>> &v) {
	std::cerr << "{\n";
	for(const auto &c : v) {
		std::cerr << "\t(";
		view(c.first);
		std::cerr << ", ";
		view(c.second);
		std::cerr << ")\n";
	}
	std::cerr << "}";
}

template <class T0, class T1, class T2>
void view(const std::vector<std::tuple<T0, T1, T2>> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << '{';
	for(const auto &t : v) {
		std::cerr << "\n\t";
		view(t);
		std::cerr << ",";
	}
	std::cerr << "\n}";
}

template <class T0, class T1, class T2, class T3>
void view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << '{';
	for(const auto &t : v) {
		std::cerr << "\n\t";
		view(t);
		std::cerr << ",";
	}
	std::cerr << "\n}";
}

template <typename T, typename U>
void view(const std::map<T, U> &m) {
	std::cerr << "{\n";
	for(const auto &t : m) {
		std::cerr << "\t[";
		view(t.first);
		std::cerr << "] : ";
		view(t.second);
		std::cerr << '\n';
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::unordered_map<T, U> &m) {
	std::cerr << "{\n";
	for(const auto &t : m) {
		std::cerr << "\t[";
		view(t.first);
		std::cerr << "] : ";
		view(t.second);
		std::cerr << '\n';
	}
	std::cerr << "}";
}
}  // namespace viewer

#pragma endregion

// when debugging : g++ foo.cpp -DLOCAL
#ifdef LOCAL
void debug_out() {}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	viewer::view(H);
	std::cerr << ", ";
	debug_out(T...);
}
#define debug(...)                                                \
	do {                                                          \
		std::cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
		debug_out(__VA_ARGS__);                                   \
		std::cerr << "\b\b]\n";                                   \
	} while(0)
#define dump(x)                                      \
	do {                                             \
		std::cerr << __LINE__ << " " << #x << " : "; \
		viewer::view(x);                             \
		std::cerr << '\n';                           \
	} while(0)

#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

#pragma endregion

#pragma region modint

const int mod = mod_1000000007;

template <int mod>
struct ModInt {
	int x;
	ModInt() : x(0) {}
	ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
	ModInt &operator+=(const ModInt &p) {
		if((x += p.x) >= mod) x -= mod;
		return *this;
	}
	ModInt &operator-=(const ModInt &p) {
		if((x += mod - p.x) >= mod) x -= mod;
		return *this;
	}
	ModInt &operator*=(const ModInt &p) {
		x = (int)(1LL * x * p.x % mod);
		return *this;
	}
	ModInt &operator/=(const ModInt &p) {
		*this *= p.inverse();
		return *this;
	}
	ModInt operator-() const { return ModInt(-x); }
	ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
	ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
	ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
	ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
	bool operator==(const ModInt &p) const { return x == p.x; }
	bool operator!=(const ModInt &p) const { return x != p.x; }
	ModInt inverse() const {
		int a = x, b = mod, u = 1, v = 0, t;
		while(b > 0) {
			t = a / b;
			swap(a -= t * b, b);
			swap(u -= t * v, v);
		}
		return ModInt(u);
	}
	ModInt pow(long long n) const {
		ModInt ret(1), mul(x);
		while(n > 0) {
			if(n & 1) ret *= mul;
			mul *= mul;
			n >>= 1;
		}
		return ret;
	}
	friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }
	friend istream &operator>>(istream &is, ModInt &a) {
		long long t;
		is >> t;
		a = ModInt<mod>(t);
		return (is);
	}
	static int get_mod() { return mod; }
};
using mint = ModInt<mod>;

#pragma endregion

#pragma region binomial_coefficient

// normal binomial coefficient
vector<vector<long long>> binom_memo(100, vector<long long>(100, -1));
long long binom(int n, int k) {
	assert(n < 100 && k < 100);
	if(n < k || k < 0 || n < 0) return 0;
	if(binom_memo[n][k] != -1) return binom_memo[n][k];
	long long res;
	if(n - k < k)
		res = binom(n, n - k);
	else if(k == 0)
		res = 1;
	else
		res = binom(n - 1, k - 1) + binom(n - 1, k);
	binom_memo[n][k] = res;
	return res;
}

vector<long long> fac, finv, inv;
struct mod_factorial {
	const int MAX;	// time: 300ms, when MAX = 3*10^7, time: 1900ms
	mod_factorial(const int MAX_ = 5100000) : MAX(MAX_) {
		fac.resize(MAX), finv.resize(MAX), inv.resize(MAX);
		fac[0] = fac[1] = 1;
		finv[0] = finv[1] = 1;
		inv[1] = 1;
		for(int i = 2; i < MAX; i++) {
			fac[i] = fac[i - 1] * i % mod;
			inv[i] = mod - inv[mod % i] * (mod / i) % mod;
			finv[i] = finv[i - 1] * inv[i] % mod;
		}
	}
} mod_factorial_;

long long com_mod(long long n, long long k) {
	if(n < k || n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

long long permutation_mod(long long n, long long k) {
	if(n < k || n < 0 || k < 0) return 0;
	return fac[n] * finv[n - k] % mod;
}

// patterns of n of undistinguished balls in k of distinguished boxes
long long multi_choose_mod(long long n, long long k) { return com_mod(n + k - 1, n); }

#pragma endregion

mint calc(int n, vector<int> a) {
	const int len_max = 812;
	const int use_max = n + 1;
	const int dig_max = 5;
	vector<int> len_cnt(dig_max);
	rep(i, n) { len_cnt[int((to_string(a[i])).size())]++; }

	debug(len_cnt);

	VVV<mint> dp(dig_max, VV<mint>(use_max, V<mint>(len_max, mint(0))));

	rep(i, 1, dig_max) {
		if(len_cnt[i] == 0) continue;
		len_cnt[i]--;

		auto &tmp = dp[i];
		tmp[0][0] = 1;

		rep(j, 1, dig_max) {
			drep(k, use_max) drep(l, len_max) {
				if(!tmp[k][l].x) continue;

				// debug(i, j, k, l, tmp[k][l]);
				rep(use_this_dig, 1, len_cnt[j] + 1) {
					int use_after = k + use_this_dig;
					int len_after = l + use_this_dig * j;
					if(len_after >= len_max || use_after >= use_max) continue;
					// debug(j, k, l, use_this_dig);
					tmp[use_after][len_after] += tmp[k][l] * com_mod(k + use_this_dig, k) * permutation_mod(len_cnt[j], use_this_dig);
				}
			}
		}

		len_cnt[i]++;
	}
	mint ret;

	VV<mint> table(len_max + 10, V<mint>(len_max + 10));  // permute j out of i
	rep(i, len_max + 10) rep(j, i + 1) {
		table[i][j] = 1;
		if(i && j) table[i][j] += table[i - 1][j - 1] * j;
	}

	V<mint> pw10(len_max + 10, 1);
	rep(i, len_max + 9) pw10[i + 1] = pw10[i] * 10;
	V<mint> pw2(len_max + 10, 1);
	rep(i, len_max + 9) pw2[i + 1] = pw2[i] * 2;

	rep(i, n) {
		mint add = 0;
		int len = int((to_string(a[i])).size());

		auto &tmp = dp[len];

		rep(j, use_max) rep(k, len_max) {
			if(1 + j > n) break;
			if(!tmp[j][k].x) continue;
			add += pw10[k] * tmp[j][k] * table[n - 1 - j][n - 1 - j];

			debug(n, i, j, k);
			debug(tmp[j][k], table[n - 1 - j][n - 1 - j]);
		}

		debug(i, a[i], add);
		ret += add * a[i];
	}

	return ret;
}

mint solve(int n, vector<int> a) {
	sort(all(a));
	mint ret = calc(n, a);

	if(a[0] == 0) {
		if(!n) return 0;
		a.erase(a.begin());
		debug(ret);
		ret -= calc(n - 1, a);
		debug(calc(n - 1, a));
	}
	return ret;
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	std::cout << std::fixed << std::setprecision(15);
	srand((unsigned)time(NULL));

	int n;
	while(cin >> n && n) {
		vi a(n);
		rep(i, n) cin >> a[i];
		cout << solve(n, a) << dl;
	}
}
accuracy: 1
time_ms: 190
memory_kb: 128716
final_rank: 15

code_snippet: #include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <iomanip>
using namespace std;
using ll = long long;
template<class T> inline bool chmax(T& a, T b){ if (a < b){ a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b){ if (a > b){ a = b; return true; } return false; }
const ll mod = 1000000007;
const int fact_size = 1000000;

int n, m;
ll dp[202][1111][2], G[1111], ten[1111];
ll fact[fact_size], inv[fact_size], factinv[fact_size];


long long ext_gcd(long long a, long long b, long long& x, long long& y) {
    if(b==0){ x=1; y=0; return a; }
    long long q = a/b;
    long long d = ext_gcd(b, a-q*b, x, y);
    long long z = x - q*y;
    x = y; y = z;
    return d;
}

long long invmod(long long a, long long p) {
    long long x, y;
    ext_gcd(a, p, x, y);
    x %= p;
    if (x < 0) x+=p;
    return x;
}

long long modpow(long long a, long long n, long long p) {
    if (n==0) return 1LL;
    if (n&1) return modpow(a, n-1, p) * a % p;
    long long tmp = modpow(a, n/2, p);
    return tmp * tmp % p;
}

void fact_init() {
    fact[0] = fact[1] = 1;
    for(int i=2; i<fact_size; ++i) fact[i] = fact[i-1] * i % mod;
    factinv[fact_size-1] = modpow(fact[fact_size-1], mod-2, mod);
    for(int i=fact_size-2; i>=0; --i) factinv[i] = factinv[i+1] * (i+1) % mod;
}

ll comb(int n, int k) {
    if (k<0 || k>n) return 0;
    return fact[n] * factinv[k] % mod * factinv[n-k] % mod;
}

ll nPk(int n, int k) {
    if (k<0 || k>n) return 0;
    return fact[n] * factinv[n-k] % mod;
}


void add(int a) {
    int len = to_string(a).size();
    for(int i=n; i>0; --i) {
        for(int j=len; j<=m; ++j) {
            for(int k=0; k<2; ++k) {
                int nk = k;
                if (a==0) nk = 1;
                dp[i][j][nk] += dp[i-1][j-len][k];
                dp[i][j][nk] %= mod;
            }
        }
    }
}

void sub(int a) {
    int len = to_string(a).size();
    for(int i=1; i<=n; ++i) {
        for(int j=len; j<=m; ++j) {
            for(int k=0; k<2; ++k) {
                dp[i][j][k] -= dp[i-1][j-len][k];
                if (dp[i][j][k]<0) dp[i][j][k] += mod;
            }
        }
    }
}

ll arange(int n, bool zero) {
    if (n==0) return 1;
    ll res = G[n];
    if (zero) {
        res -= G[n-1];
        if (res < 0) res += mod;
    }
    return res;
}


ll solve() {
    cin >> n;
    bool zero = false;

    vector<int> A(n);
    for(int i=0; i<n; ++i) {
        cin>>A[i];
        m += to_string(A[i]).size();
        if (A[i]==0) zero = true;
    }

    ten[0] = 1;
    for(int i=1; i<=m; ++i) ten[i] = ten[i-1]*10%mod;
    for(int i=0; i<=n; ++i) for(int j=0; j<=i; ++j) G[i] = (G[i] + nPk(i, j)) % mod;

    dp[0][0][0] = 1;
    for(int a: A) add(a);

    ll ans = 0;
    for(int a: A) {
        if(a==0) continue;
        sub(a);

        for(int i=0; i<n; ++i) {
            for(int j=0; j<m; ++j) {
                for(int k=0; k<2; ++k) {
                    if(dp[i][j][k] == 0) continue;
                    ll val = a * ten[j] % mod;
                    val = val * fact[i] % mod;
                    val = val * dp[i][j][k] % mod;
                    val = val * arange(n-1-i, zero&(k==0)) % mod;
                    ans = (ans + val) % mod;
                }
            }
        }
        add(a);
    }
    return ans;
}

int main() {
    fact_init();
    cout << solve() << endl;
    return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 24236
final_rank: 5