Problem:
Slot Machines
Aizu Industry has developed a new slot machine. This slot machine displays $W$ numbers horizontally and $H$ numbers vertically from 0 to 9. An example of the numbers displayed where $W=5$ and $H=3$ is shown in Figure 1.
Figure 1: Example of numbers displayed on a slot machine ($W=5, H=3$)
In this slot machine, you get points according to the numbers displayed. The score is calculated as follows: when a specific same number appears in all the rows from column 1 to column $W$ (regardless of whether the rows match), you can get the score (that number) by connecting the numbers with a line to form a single broken line. If you can make multiple lines with different shapes, the sum of the scores of all the lines will be the score you get from the slot machine. If you can't make a single broken line, your score will be zero.
Let's consider the scores obtained by the numbers displayed in Figure 1 in Figure 2.
Figure 2: Example of calculating the score of a slot machine
In the example of Figure 1, since 3 appears in every row, a polyline can be formed by connecting 3. The score for each line is 3 points, so the total score for the slot machine is 6 points.
Write a program that reads the numbers shown in the slot machine and finds the score obtained.
Input
The input is given in the following format.
$W$ $H$
$row_1$
$row_2$
 :
$row_H$
In the first row, the number of numbers in the horizontal direction $W$ ($1 \leq W \leq 1,000$), and the number of numbers in the vertical direction $H$ ($1 \leq H \leq 1,000$) are given. In the following $H$ rows, the $row_i$ of $W$ numbers to be displayed in row $i$ are given, where $row_i$ is a sequence of numbers from 0 to 9 with length $W$.
Output
Output the obtained score in a line. Print the remainder divided by 998,244,353.
Sample Input and Output
Sample Input 1
5 3
03077
50333
35743
Sample Output 1
6
Sample Input 2
4 4
3351
5335
3573
7350
Sample Output 2
28
In Sample Input 2, there are 6 lines in which 3 appears in any column, and 2 lines in which 5 appears in any column. Therefore, the score is 3 x 6 + 5 x 2 = 28.
Sample Input 3
12 5
999999999999
999999999999
999999999999
999999999999
999999999999
Sample Output 3
200776919

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main() {
    int h, w;
    cin >> w >> h;
    vector<string> s(h);
    for (auto &i : s) cin >> i;
    vector<vector<long long>> c(w, vector<long long>(10, 0));
    vector<vector<long long>> dp(w, vector<long long>(10, 0));
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            c[j][s[i][j] - '0']++;
        }
    }
    for (int i = 0; i < 10; i++) dp[0][i] = c[0][i];
    for (int i = 1; i < w; i++) {
        for (int j = 0; j < 10; j++) {
            dp[i][j] = (c[i][j] * dp[i - 1][j]) % 998244353;
        }
    }
    long long ans = 0;
    for (int i = 0; i < 10; i++) {
        ans += dp[w - 1][i] * i;
        ans %= 998244353;
    }
    cout << ans << "\n";
}
accuracy: 1
time_ms: 10
memory_kb: 5516
final_rank: 9

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

int main(){
	int W, H;cin>>W>>H;
	vector<string> row(H);
	vector<vector<ll>> nums(W, vector<ll>(10, 0));//[w][9]
	for(int i = 0;i<H;i++) cin >> row[i];
	
	vector<ll> sums(10, 1);
	for(int j =0;j<W;j++){
	
		for(int i = 0;i<H;i++){
			int index =(row[i][j]-'0');
			//cout << i <<", "<<j<<", "<<index<<endl;
			nums[j][index]++;
			
		}
	}
//	for(int i=0;i<10;i++){
//		for(int j=0;j<W;j++){
//			cout << nums[j][i] <<" ";
//		}
//		cout <<endl;
//	}
	
	for(int a = 1;a<10;a++){
	//sums[a] = 得点aについて得られる折れ線の数
	//nums[i][j] = i列目にあるjの数
		for(int i = 0;i<W;i++){
			sums[a]=(sums[a] % 998244353) * (nums[i][a] %998244353);
	//		if(0<sums[a]&&0<nums[i][a])
	//		cout << a<<": "<<sums[a] <<"*" << nums[i][a] << endl;
		}
	}
	ll ans = 0;
	for(ll i = 0;i<10;i++){
	ans+=(i*sums[i]%998244353);
	}
	cout<<ans%998244353<<endl;
}
accuracy: 1
time_ms: 10
memory_kb: 5632
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std; 
/*#include <atcoder/all>
using namespace atcoder;
using mint = modint;*/
//long long型=int64_tという認識で大丈夫です。
//#define int long long
#define ll long long
#define ull unsigned long long
#define vec vector
#define vi vector<int>
#define vll vector<ll>
#define pq priority_queue
#define pb push_back
#define rep(i, l, r) for(ll i=(ll)(l); i<(ll)(r); i++)
#define ld long double

const ll INF = 2e9;
const ll inf = 5e18;
const ld ep = 1e-15;

struct Edge {
    int to;
    ll cost;
};

struct SEdge {
    int to;
    pair<ll, ll> cost;
};

template<class T>
using spq = priority_queue<T, vec<T>, greater<T>>; 

const vector<ll> di = {-1, 0, 1, 0}, dj = {0, -1, 0, 1};

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, class U> 
istream &operator>>(istream &is, pair<T, U> &p) {
    is >> p.first >> p.second;
    return is;
}

template<class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << p.first << " " << p.second;
    return os;
}
template <class T>
istream &operator>>(istream &is, vector<T> &v) {
    for(auto &x : v) {
        is >> x;
    }
    return is;
}

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

template <class T>
bool jf(pair<T, T> a, pair<T, T> b) {
    if(a.second > b.second) return false;
    else return true;
}

using S = ll;

S op(S a, S b){ return max(a, b); }
S e(){ return 0;}

signed main(){
    int h, w; cin >> w >> h;
    vec<string> s(h); rep(i, 0, h) cin >> s[i];
    vec<vi> dp(w, vi(h, -1)); vi cnt(10, 0); rep(i, 0, h) cnt[(int)(s[i][0] - '0')]++;
    rep(i, 1, w) {
        vi ncnt(10);
        rep(j, 0, h) {
            ncnt[(int)(s[j][i] - '0')] += cnt[(int)(s[j][i] - '0')];
            ncnt[(int)(s[j][i] - '0')] %= 998244353;
        }
        cnt = ncnt;
    }
    int ans = 0;
    rep(i, 0, 10) {
        rep(j, 0, i) {
            ans += cnt[i];
            ans %= 998244353;
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 9204
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main()
{
    vector<ll> v[10];
    map<char,ll> mp;
    ll w,h,ss,ans=0,sum=1;
    cin>>w>>h;
    string s[1001];
    for(int i=0;i<h;i++){
        cin>>s[i];
    }
    for(int i=0;i<w;i++){
        for(int j=0;j<h;j++){
            mp[s[j][i]]++;
        }
        for(auto x:mp){
            ss=x.first-'0';
            v[ss].push_back(x.second);
        }
        mp.clear();
    }
    for(int i=0;i<10;i++){
        if(v[i].size()==w){
            for(int j=0;j<v[i].size();j++){
                sum*=v[i][j];
                sum=sum%998244353;
            }
            ans+=i*sum%998244353;
            sum=1;
        }
    }
    cout<<ans%998244353<<endl;
}
accuracy: 1
time_ms: 30
memory_kb: 5376
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(){
    long long w,h,c=0,g=0; cin>>w>>h;
    int b[10*w]={},d[w*h]={}; char a[w*h];
    for(int i=0;i<h*w;i++){
        cin>>a[i];
        d[i]=a[i]-'0';
    }
    for(int i=0;i<w;i++){
        for(int j=0;j<h;j++){
            b[d[i+(w*j)]+(i*10)]++;
        }
    }
    for(int i=0;i<10;i++){
        for(int j=0;j<w;j++){
            if(j==0)c=b[i+(j*10)]%998244353;
            else c=c*b[i+(j*10)]%998244353;
            // cout<<b[i+(j*10)]%998244353;
        }
        g+=c*i;
        // cout<<" "<<g;
        // cout<<endl;
        c=0;
    }
    cout<<g%998244353<<endl;
}
accuracy: 1
time_ms: 20
memory_kb: 8140
final_rank: 20

code_snippet: #include<bits/stdc++.h>
#include <vector>
using namespace std;
typedef pair<int,int>P;
char s[1001][1001];
long long w,h,sm,sum,cnt,a[1001][11],b;
int main(void)
{
	cin>>w>>h;
	for(int i=1;i<=h;i++){
		for(int j=1;j<=w;j++){
			cin>>s[i][j];
		}
	}
	for(int i=1;i<=w;i++){
		for(int j=1;j<=h;j++){
			b=s[j][i]-'0';
			a[i][b]++;	
		}
	}
	for(int i=1;i<=9;i++){
		sm=1;
		for(int j=1;j<=w;j++){
			sm*=a[j][i];
			sm=sm%998244353;
			//cout<<a[j][i]<<endl;
		}
		sm*=i;
		sm=sm%998244353;
		sum+=sm;
		sum=sum%998244353;
	}
	cout<<sum<<endl;
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4404
final_rank: 12

code_snippet: #include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
  int h,w;
  cin >> w >> h;
  vector<vector<int>>grid(h,vector<int>(w));
  for(int i=0;i<h;i++){
    string s;
    cin >> s;
    for(int j=0;j<w;j++)grid[i][j]=(s[j]-'0');
  }
  long long mod=998244353,ans=0;
  for(int num=1;num<=9;num++){
    long long count=1;
    for(int i=0;i<w;i++){
      int cnt=0;
      for(int j=0;j<h;j++)if(grid[j][i]==num)cnt++;
      count*=cnt;
      count%=mod;
    }
    ans+=count*num;
    ans%=mod;
  }
  cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 7316
final_rank: 18

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

const int MOD{998244353};

int main() {
    int W, H;
    std::cin >> W >> H;
    std::vector cnt(10, std::vector<int>(W));
    for (int i{} ; i < H ; i++) {
        for (int j{} ; j < W ; j++) {
            char c;
            std::cin >> c;
            cnt[c - '0'][j]++;
        }
    }
    long long ans{};
    for (int i{} ; i < 10 ; i++) {
        long long num{1};
        for (int j{} ; j < W ; j++) {
            num *= cnt[i][j];
            num %= MOD;
        }
        ans += (long long)i * num;
        ans %= MOD;
    }
    std::cout << ans << '\n';
}
accuracy: 1
time_ms: 20
memory_kb: 3488
final_rank: 10

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

int main() {
    int h, w;
    std::cin >> w >> h;
    std::vector<std::string> s(h);
    for (int i = 0; i < h; i++) std::cin >> s[i];
    constexpr int W = 10;
    using Table = std::array<int, W>;
    std::vector<Table> freq(w);
    for (int i = 0; i < w; i++) freq[i].fill(0);
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            freq[j][s[i][j] - '0']++;
        }
    }
    // for (int i = 0; i < w; i++) {
    //     for (int j = 0; j < W; j++) {
    //         std::cerr << freq[i][j] << ' ';
    //     }
    //     std::cerr << std::endl;
    // }

    constexpr long long MOD = 998244353;
    long long ans = 0;
    for (int i = 0; i < W; i++) {
        long long now = i;
        for (int j = 0; j < w; j++) {
            now *= freq[j][i];
            now %= MOD;
        }
        ans += now;
        ans %= MOD;
    }
    std::cout << ans << '\n';
}
accuracy: 1
time_ms: 10
memory_kb: 5404
final_rank: 7

code_snippet: #include <iostream>
using namespace std;
int main(void){
    // Your code here!
    
    char row;
    int cnt[10][1000];
    int W, H;
    long int SuM = 0, SuM2;
    cin >> W >> H;
    for(int i = 0; i < 10; i++)
    {
        for(int j = 0; j < 1000; j++)
            cnt[i][j] = 0;
    }
    for(int i = 0; i < H; i++)
    {
        for(int j = 0; j < W; j++)
        {
            cin >> row;
            row -= 48;
            cnt[(int)row][j]++;
        }
    }
    for(int i = 1; i < 10; i++)
    {
        SuM2 = 1;
        for(int j = 0; j < W; j++)
        {
            if(cnt[i][j] == 0)
            {
                SuM2 = 0;
                break;
            }
            SuM2 *= cnt[i][j];
            SuM2 %= 998244353;
        }
        SuM += SuM2 * i;
        SuM %= 998244353;
    }
    cout << SuM << endl;
    
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3096
final_rank: 15

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

#define int long long
using namespace std;

signed main(){
	int w, h;cin >> w >> h;
	vector<vector<int>> A(w, vector<int>(10,0));
	for (int i = 0; i < h; ++i){
		string s;cin >> s;
		for (int j = 0; j < w; ++j){
			A[j][(int)s[j] - 48] ++;
		}
	}
	int ans = 0;
	for (int i = 1; i < 10; ++ i){
		int c = 1;
		for (int j = 0; j < w; ++j){
			c *= A[j][i];
			c %= 998244353;
		}
		ans += c * i;
		ans %= 998244353;
	}
	cout << ans % 998244353 << endl;
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3544
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(void){
    int MAX = 998244353;
    int w,h;
    cin >> w >> h;
    vector<vector<int>> vec(w,vector<int>(h));
    vector<long long> record(10);
    for(int i = 0; i < 10; i++) record[i] = i;
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            char c;
            cin >> c;
            vec[j][i] = (int)c - 48;
        }
    }
    
    for(auto v : vec){
        vector<int> count(10);
        
        for(auto i : v){
            count[i]++;
        }
        
        for(int i = 0; i < 10; i++){
            record[i] = (record[i] % MAX) * (count[i] % MAX) % MAX;
        }
    }
    long long sum = 0;
    for(int i = 0; i < 10; i++) sum = (sum % MAX + record[i] % MAX) % MAX;
    
    cout << sum << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 7288
final_rank: 17

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

int main()
{
	long long w,h,mod = 998244353;
	cin >> w >> h;
	vector<vector<char>> s(h,vector<char>(w));
	vector<vector<long long>> cou(w,vector<long long>(11,0));
	for(long long i = 0;i < h;i++){
		string S;
		cin >> S;
		for(long long j = 0;j < w;j++){
			s[i][j] = S[j];
		}
	}
	for(long long j = 0;j < w;j++){
		for(long long i = 0;i < h;i++){
			cou[j][s[i][j] - '0'] += 1ll;
		}
	}
	long long ans = 0;
	long long it = 1ll;
	for(long long i = 1ll;i <= 9ll;i++){
		it = i;
		for(long long j = 0;j < w;j++){
			it *= cou[j][i];
			it %= mod;
		}
		while(it < 0) it += mod;
		ans += it;
		ans %= mod;
	}
	ans %= mod;
	while(ans < 0) ans += mod;
	cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 4544
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define INF INT64_MAX;
int main()
{
    int h,w;
    LL sum=1,ans=0;
    cin>>w>>h;
    vector<string> g(h+2);
    for(int i=0;i<h;i++) cin>>g[i];
    for(int st=1;st<10;st++){
        //cout<<"st="<<st<<endl;
        LL sum=1,cnt=0;
        for(int j=0;j<w;j++){
            cnt=0;
            for(int i=0;i<h;i++){
                //cout<<g[i][j]-'0'<<endl;
                if((g[i][j]-'0')==st) cnt++;//cout<<"cnt="<<cnt<<endl;
            }
            sum=(sum*cnt)%998244353;
            //cout<<"sum="<<sum<<endl;
        }
        //cout<<"sum="<<sum<<endl;
        ans=(ans+sum*st)%998244353;
    }
    cout<<ans<<endl;
}
accuracy: 1
time_ms: 30
memory_kb: 3896
final_rank: 13

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define INF INT64_MAX;
int main()
{
    int h,w;
    LL sum=1,ans=0;
    cin>>w>>h;
    vector<string> g(h+2);
    for(int i=0;i<h;i++) cin>>g[i];
    for(int st=1;st<10;st++){
        //cout<<"st="<<st<<endl;
        LL sum=1,cnt=0;
        for(int j=0;j<w;j++){
            cnt=0;
            for(int i=0;i<h;i++){
                //cout<<g[i][j]-'0'<<endl;
                if((g[i][j]-'0')==st) cnt++;//cout<<"cnt="<<cnt<<endl;
            }
            sum=(sum*cnt)%998244353;
            //cout<<"sum="<<sum<<endl;
        }
        //cout<<"sum="<<sum<<endl;
        ans=(ans+sum*st)%998244353;
    }
    cout<<ans<<endl;
}
accuracy: 1
time_ms: 30
memory_kb: 3968
final_rank: 14

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

int ctoi(char c) {
	if (c >= '0' && c <= '9') {
		return c - '0';
	}
	return 0;
}

int main()
{
    int w,h; cin>>w>>h;
    int cnt[w][10];
    for(int i=0; i<w; i++)
    {
        for(int t=0; t<10; t++)
        {
            cnt[i][t]=0;
        }
    }
    for(int i=0; i<h; i++)
    {
        string s;
        cin>>s;
        for(int t=0; t<w; t++)
        {
            //sはstring
            int h=(s[t]-'0');
            cnt[t][h]++;
        }
    }

    ll ans=0;
    for(int i=0; i<10; i++)
    {
        ll now=1;
        for(int t=0; t<w; t++)
        {
            now*=(ll)cnt[t][i];
            now%=998244353;
        }
        now*=i;
        ans+=now;
        ans%=998244353;
    }
    cout<<ans<<endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3436
final_rank: 1

code_snippet: #include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define vi vector<int>
using namespace std;
using ll = long long;
constexpr int mod = 998244353;

int main(){
	int h,w;
	cin >> w >> h;
	int cnt[w][10] = {};
	rep(i, h){
		rep(j, w){
			char c; cin >> c;
			cnt[j][c - '0']++;
		}
	}
	ll ans = 0;
	for(int i = 1; i < 10; i++){
		ll val = 1;
		rep(j, w) (val *= cnt[j][i]) %= mod;
		ans += (val * i) % mod;
		ans %= mod;
	}
	cout << ans << "\n";
}
accuracy: 1
time_ms: 20
memory_kb: 3372
final_rank: 8

code_snippet: #include <iostream>
#include <string>
#include <string.h>
using namespace std;
const long long int m1=998244353; 
long long int as[1002][10];
int main() {
	// your code goes here
	int w,h;
	cin>>w>>h;
	string s1;
	memset(as,0,sizeof(as));
	for(int i=0;i<=9;i++)as[0][i]=1;
	
	for(int i=0;i<h;i++){
		
		cin>>s1;
		for(int j=1;j<=w;j++){
			int t=s1[j-1]-'0';
			as[j][t]++;
		}
	}
	long long int ans=0;
	for(int i=1;i<=w;i++){
		for(int j=1;j<=9;j++){
			as[i][j]=(as[i][j]*as[i-1][j])%m1;
			if(i==w)ans=(ans+(j*as[i][j])%m1)%m1;
		}
	}
	cout<<ans<<endl;
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3280
final_rank: 5

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

using namespace std;

#define lol long long
#define INF INT_MAX
#define P pair<int,int>
#define Pci pair<char,int>
#define Pll pair<lol,lol>
#define fi first
#define se second
#define mp(f,s) make_pair(f,s)

// const int mod = (1e9) + 7;
const int mod = 998244353;

int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};

int main(){
    int w,h;
    cin>>w>>h;

    // vec[i][j] : 列iでjが何回現れるか
    vector<vector<int>> vec(w,vector<int>(10,0));
    
    string str;
    for(int i=0;i<h;i++){
        cin>>str;
        for(int j=0;j<w;j++){
            vec[j][str[j]-'0']++;
            vec[j][str[j]-'0']%=mod;
        }
    }

    vector<lol> ans(10,1);

    for(int i=0;i<w;i++){
        for(int j=0;j<10;j++){
            ans[j] *= vec[i][j] % mod;
            ans[j] %= mod;
        }
    }

    lol a = 0;
    for(int i=0;i<10;i++){
        a += (ans[i]%mod * i)%mod; 
    }
    cout<<a%mod<<"\n";
    return(0);
}
accuracy: 1
time_ms: 10
memory_kb: 3440
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
using lint = long long;
const lint mod = 998244353;
#define rep(i, n) for(int i = 0; i < int(n); i++)
#define rep2(i, l, r) for(int i = int(l); i < int(r); i++)
#define repr(i, n) for(int i = int(n) - 1; i >= 0; i--)
#define repr2(i, l, r) for(int i = int(r) - 1; i >= int(l); i--)

int main(){
    int w, h;
    cin >> w >> h;
    string s[h];
    rep(i, h){
        cin >> s[i];
    }
    
    int cnt[w][10];
    rep(i, w) rep(j, 10) cnt[i][j] = 0;
    
    rep(i, h) rep(j, w){
        int num = s[i][j] - '0';
        cnt[j][num]++;
    }
    
    lint ans = 0;
    rep(i, 10){
        lint res = 1;
        rep(j, w){
            res = (res * cnt[j][i]) % mod;
        }
        
        ans += res * i;
    }
    
    cout << ans % mod << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 5320
final_rank: 6