Problem p01550 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>
using namespace std;

long long n, a[209], sz[209], ret, mod = 1000000007;
long long dp[209][209][209], power[5] = {1, 10, 100, 1000, 10000};

long long solve(vector<long long> E, long long num, bool iszero) {
  for (int i = 0; i <= E.size(); i++) {
    for (int j = 0; j <= i; j++) {
      for (int k = 0; k <= i - j; k++)
        dp[i][j][k] = 0;
    }
  }
  dp[0][0][0] = 1;
  for (int k = 0; k < E.size(); k++) {
    for (int i = 0; i <= k; i++) {
      for (int j = 0; j <= k - i; j++) {
        if (dp[k][i][j] == 0)
          continue;
        long long X = i + 1, Y = j + 1;
        dp[k + 1][i + 1][j] += dp[k][i][j] * X;
        dp[k + 1][i][j + 1] += dp[k][i][j] * Y * power[sz[E[k]]];
        dp[k + 1][i][j] += dp[k][i][j];
      }
    }
    for (int i = 0; i <= k + 1; i++) {
      for (int j = 0; j <= k + 1 - i; k++)
        dp[k + 1][i][j] %= mod;
    }
  }
  long long Z = 0;
  for (int j = 0; j <= E.size(); j++) {
    for (int k = 0; k <= E.size() - j; k++) {
      if (j == 0 && num == 0)
        continue;
      Z += dp[E.size()][j][k];
      Z %= mod;
      if (iszero == true) {
        Z += dp[E.size()][j][k] * j;
        Z %= mod;
        Z += dp[E.size()][j][k] * (k + 1) * 10LL;
        Z %= mod;
      }
    }
  }
  return Z;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sz[i] = to_string(a[i]).size();
  }
  for (int i = 1; i <= n; i++) {
    vector<long long> X;
    bool OK = false;
    for (int j = 1; j <= n; j++) {
      if (i != j) {
        if (a[j] != 0)
          X.push_back(j);
        else
          OK = true;
      }
    }
    long long G = solve(X, a[i], OK);
    ret += a[i] * G;
    ret %= mod;
  }
  cout << ret << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <string>
#include <vector>
using namespace std;

long long n, a[209], sz[209], ret, mod = 1000000007;
long long dp[209][209][209], power[5] = {1, 10, 100, 1000, 10000};

long long solve(vector<long long> E, long long num, bool iszero) {
  for (int i = 0; i <= E.size(); i++) {
    for (int j = 0; j <= i; j++) {
      for (int k = 0; k <= i - j; k++)
        dp[i][j][k] = 0;
    }
  }
  dp[0][0][0] = 1;
  for (int k = 0; k < E.size(); k++) {
    for (int i = 0; i <= k; i++) {
      for (int j = 0; j <= k - i; j++) {
        if (dp[k][i][j] == 0)
          continue;
        long long X = i + 1, Y = j + 1;
        dp[k + 1][i + 1][j] += dp[k][i][j] * X;
        dp[k + 1][i][j + 1] += dp[k][i][j] * Y * power[sz[E[k]]];
        dp[k + 1][i][j] += dp[k][i][j];
      }
    }
    for (int i = 0; i <= k + 1; i++) {
      for (int j = 0; j <= k + 1 - i; j++)
        dp[k + 1][i][j] %= mod;
    }
  }
  long long Z = 0;
  for (int j = 0; j <= E.size(); j++) {
    for (int k = 0; k <= E.size() - j; k++) {
      if (j == 0 && num == 0)
        continue;
      Z += dp[E.size()][j][k];
      Z %= mod;
      if (iszero == true) {
        Z += dp[E.size()][j][k] * j;
        Z %= mod;
        Z += dp[E.size()][j][k] * (k + 1) * 10LL;
        Z %= mod;
      }
    }
  }
  return Z;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sz[i] = to_string(a[i]).size();
  }
  for (int i = 1; i <= n; i++) {
    vector<long long> X;
    bool OK = false;
    for (int j = 1; j <= n; j++) {
      if (i != j) {
        if (a[j] != 0)
          X.push_back(j);
        else
          OK = true;
      }
    }
    long long G = solve(X, a[i], OK);
    ret += a[i] * G;
    ret %= mod;
  }
  cout << ret << endl;
  return 0;
}