#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx2")
#include <iostream>
#include <iomanip>

constexpr int HeighestDim = 505000;

int n, m, k;
long long int res[101];
int dim_a = 0, dim_b = 0;
int A[HeighestDim + 1], B[HeighestDim];

void mult_a(int v){
    for(int i = dim_a, j = i - v; j >= 0; --i, --j){ A[i] -= A[j]; if(A[i] < 0) A[i] += m; }
}
void div_a(int v){
    for(int i = v, j = 0; i <= dim_a; ++i, ++j){
        A[i] += A[j]; if(A[i] >= m) A[i] -= m;
    }
}

void mult_b(int v){
    dim_b += v;
    for(int i = dim_b, j = i - v; j >= 0; --i, --j){
        B[i] -= B[j]; if(B[i] < 0) B[i] += m;
    }
}
void div_b(int v){
    for(int i = v, j = 0; i <= dim_b; ++i, ++j){
        B[i] += B[j]; if(B[i] >= m) B[i] -= m;
    }
}

int main(void){
    std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); 
    std::cout << std::fixed << std::setprecision(16);
    A[0] = 1; B[0] = 1;
    std::cin >> n >> k >> m; ++k;
    for(int i = 1, j = k; i < n; ++i, j += k){ dim_a += j; mult_a(j); div_a(i); }
    for(int i = 0, j = n, I = k, J = k * (n - 1); ; I += k, J -= k){
        long long int res_v = 0;
        for(int d = 0; d <= dim_b; ++d) res_v += static_cast<long long int>(A[d]) * B[d] % m;
        if(res_v) res_v = (k * res_v - 1) % m; else res_v = m - 1;
        res[i++] = res_v; res[--j] = res_v;
        if(i > j) break;
        mult_a(j); div_a(J);
        mult_b(I); div_b(i);
    }

    for(int i = 0; i < n; ++i) std::cout << res[i] << '\n';
    return 0;
}


#include <bits/stdc++.h>

#define ll long long
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define vi vector<int>
#define pii pair<int, int>
#define rep(i, a, b) for(int i = (a); i < (b); i++)
using namespace std;
template<typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    string s;
    cin >> n >> s;
    vi a(n + 1);
    rep(i, 0, n + 1) cin >> a[i];
    int ans = INT_MAX;
    rep(i, 0, n) ans = min(ans, abs(a[i] - a[i + 1]));
    vector<vi> b(n + 1, vi(ans, 0));
    int k = 0;
    rep(i, 0, n + 1) {
        rep(j, 0, ans) b[i][j] = a[i] / ans;
        int dif = a[i] - (a[i] / ans) * ans;
        if(i % 2 == 0) {
            rep(j, 0, dif) {
                b[i][k]++;
                k = (k + 1) % ans;
            }
        }else {
            rep(j, 0, dif) {
                k = (k + ans - 1) % ans;
                b[i][k]++;
            }
        }
    }

    cout << ans << '\n';
    rep(j, 0, ans) {
        rep(i, 0, n + 1) {
            cout << b[i][j] << ' ';
        }
        cout << '\n';
    }
}