1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include <bits/stdc++.h>
using namespace std;

#include <array>

template <typename T, int NDIMS> struct tensor_view {
	static_assert(NDIMS >= 0, "NDIMS must be nonnegative");

protected:
	std::array<int, NDIMS> shape;
	std::array<int, NDIMS> strides;
	T* data;

	tensor_view(std::array<int, NDIMS> shape_, std::array<int, NDIMS> strides_, T* data_) : shape(shape_), strides(strides_), data(data_) {}

public:
	tensor_view() : shape{0}, strides{0}, data(nullptr) {}

protected:
	int flatten_index(std::array<int, NDIMS> idx) const {
		int res = 0;
		for (int i = 0; i < NDIMS; i++) { res += idx[i] * strides[i]; }
		return res;
	}
	int flatten_index_checked(std::array<int, NDIMS> idx) const {
		int res = 0;
		for (int i = 0; i < NDIMS; i++) {
			assert(0 <= idx[i] && idx[i] < shape[i]);
			res += idx[i] * strides[i];
		}
		return res;
	}

public:
	T& operator[] (std::array<int, NDIMS> idx) const {
#ifdef _GLIBCXX_DEBUG
		return data[flatten_index_checked(idx)];
#else
		return data[flatten_index(idx)];
#endif
	}
	T& at(std::array<int, NDIMS> idx) const {
		return data[flatten_index_checked(idx)];
	}

	template <int D = NDIMS>
	typename std::enable_if<(0 < D), tensor_view<T, NDIMS-1>>::type operator[] (int idx) const {
		std::array<int, NDIMS-1> nshape; std::copy(shape.begin()+1, shape.end(), nshape.begin());
		std::array<int, NDIMS-1> nstrides; std::copy(strides.begin()+1, strides.end(), nstrides.begin());
		T* ndata = data + (strides[0] * idx);
		return tensor_view<T, NDIMS-1>(nshape, nstrides, ndata);
	}
	template <int D = NDIMS>
	typename std::enable_if<(0 < D), tensor_view<T, NDIMS-1>>::type at(int idx) const {
		assert(0 <= idx && idx < shape[0]);
		return operator[](idx);
	}

	template <int D = NDIMS>
	typename std::enable_if<(0 == D), T&>::type operator * () const {
		return *data;
	}

	template <typename U, int D> friend struct tensor_view;
	template <typename U, int D> friend struct tensor;
};

template <typename T, int NDIMS> struct tensor {
	static_assert(NDIMS >= 0, "NDIMS must be nonnegative");

protected:
	std::array<int, NDIMS> shape;
	std::array<int, NDIMS> strides;
	int len;
	T* data;

public:
	tensor() : shape{0}, strides{0}, len(0), data(nullptr) {}

	explicit tensor(std::array<int, NDIMS> shape_, const T& t = T()) {
		shape = shape_;
		len = 1;
		for (int i = NDIMS-1; i >= 0; i--) {
			strides[i] = len;
			len *= shape[i];
		}
		data = new T[len];
		std::fill(data, data + len, t);
	}

	tensor(const tensor& o) : shape(o.shape), strides(o.strides), len(o.len), data(new T[len]) {
		for (int i = 0; i < len; i++) {
			data[i] = o.data[i];
		}
	}

	tensor& operator=(tensor&& o) noexcept {
		using std::swap;
		swap(shape, o.shape);
		swap(strides, o.strides);
		swap(len, o.len);
		swap(data, o.data);
		return *this;
	}
	tensor(tensor&& o) : tensor() {
		*this = std::move(o);
	}
	tensor& operator=(const tensor& o) {
		return *this = tensor(o);
	}
	~tensor() { delete[] data; }

	using view_t = tensor_view<T, NDIMS>;
	view_t view() {
		return tensor_view<T, NDIMS>(shape, strides, data);
	}
	operator view_t() {
		return view();
	}

	using const_view_t = tensor_view<const T, NDIMS>;
	const_view_t view() const {
		return tensor_view<const T, NDIMS>(shape, strides, data);
	}
	operator const_view_t() const {
		return view();
	}

	T& operator[] (std::array<int, NDIMS> idx) { return view()[idx]; }
	T& at(std::array<int, NDIMS> idx) { return view().at(idx); }
	const T& operator[] (std::array<int, NDIMS> idx) const { return view()[idx]; }
	const T& at(std::array<int, NDIMS> idx) const { return view().at(idx); }

	template <int D = NDIMS>
	typename std::enable_if<(0 < D), tensor_view<T, NDIMS-1>>::type operator[] (int idx) {
		return view()[idx];
	}
	template <int D = NDIMS>
	typename std::enable_if<(0 < D), tensor_view<T, NDIMS-1>>::type at(int idx) {
		return view().at(idx);
	}

	template <int D = NDIMS>
	typename std::enable_if<(0 < D), tensor_view<const T, NDIMS-1>>::type operator[] (int idx) const {
		return view()[idx];
	}
	template <int D = NDIMS>
	typename std::enable_if<(0 < D), tensor_view<const T, NDIMS-1>>::type at(int idx) const {
		return view().at(idx);
	}

	template <int D = NDIMS>
	typename std::enable_if<(0 == D), T&>::type operator * () {
		return *view();
	}
	template <int D = NDIMS>
	typename std::enable_if<(0 == D), const T&>::type operator * () const {
		return *view();
	}
};

template <int MOD_> struct modnum {
	static constexpr int MOD = MOD_;
	static_assert(MOD_ > 0, "MOD must be positive");

private:
	using ll = long long;

	int v;

	static int minv(int a, int m) {
		a %= m;
		assert(a);
		return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a);
	}

public:

	modnum() : v(0) {}
	modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; }
	explicit operator int() const { return v; }
	friend std::ostream& operator << (std::ostream& out, const modnum& n) { return out << int(n); }
	friend std::istream& operator >> (std::istream& in, modnum& n) { ll v_; in >> v_; n = modnum(v_); return in; }

	friend bool operator == (const modnum& a, const modnum& b) { return a.v == b.v; }
	friend bool operator != (const modnum& a, const modnum& b) { return a.v != b.v; }

	modnum inv() const {
		modnum res;
		res.v = minv(v, MOD);
		return res;
	}
	friend modnum inv(const modnum& m) { return m.inv(); }
	modnum neg() const {
		modnum res;
		res.v = v ? MOD-v : 0;
		return res;
	}
	friend modnum neg(const modnum& m) { return m.neg(); }

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

	modnum& operator ++ () {
		v ++;
		if (v == MOD) v = 0;
		return *this;
	}
	modnum& operator -- () {
		if (v == 0) v = MOD;
		v --;
		return *this;
	}
	modnum& operator += (const modnum& o) {
		v += o.v;
		if (v >= MOD) v -= MOD;
		return *this;
	}
	modnum& operator -= (const modnum& o) {
		v -= o.v;
		if (v < 0) v += MOD;
		return *this;
	}
	modnum& operator *= (const modnum& o) {
		v = int(ll(v) * ll(o.v) % MOD);
		return *this;
	}
	modnum& operator /= (const modnum& o) {
		return *this *= o.inv();
	}

	friend modnum operator ++ (modnum& a, int) { modnum r = a; ++a; return r; }
	friend modnum operator -- (modnum& a, int) { modnum r = a; --a; return r; }
	friend modnum operator + (const modnum& a, const modnum& b) { return modnum(a) += b; }
	friend modnum operator - (const modnum& a, const modnum& b) { return modnum(a) -= b; }
	friend modnum operator * (const modnum& a, const modnum& b) { return modnum(a) *= b; }
	friend modnum operator / (const modnum& a, const modnum& b) { return modnum(a) /= b; }
};

template <typename T> T pow(T a, long long b) {
	assert(b >= 0);
	T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r;
}

using num = modnum<int(1e9)+7>;

vector<num> fact, ifact;

void init(){
	int N = 1100000;
	fact.resize(N);
	fact[0] = 1;
	for(int i = 1; i < N; i++) fact[i] = i * fact[i-1];
	ifact.resize(N);
	ifact.back() = 1 / fact.back();
	for(int i = N - 1; i > 0; i--) ifact[i-1] = i * ifact[i];
}

num ncr(int n, int k){
	if(k < 0 || k > n) return 0;
	return fact[n] * ifact[k] * ifact[n-k];
}

int main(){
	ios_base::sync_with_stdio(false), cin.tie(nullptr);
	init();
	int N, M, K;
	cin >> N >> M >> K;
	vector<string> S(K);
	for(string& s : S){
		cin >> s;
		for(char& c : s){
			if(c == 'N') c = '+';
			if(c == 'C') c = '-';
		}
	}
	vector<int> decimal_place(K);
	int extra_overall_sum = 0;
	auto get_sgn = [&](char c) { return c == '+' ? 1 : -1; };
	vector<int> extra_sums(M+1);
	for(int i = 0; i < K; i++){
		int j = 0;
		while(j < M && S[i][j] == S[i][0]) j++;
		extra_overall_sum += (2 * j - (j < M)) * get_sgn(S[i][0]);
		for(int k = 1; k < M-j; k++){
			extra_sums[k+1] += get_sgn(S[i][j+k]);
		}
	}

	int D1 = N * (2*M);
	// overall_sum abs <= N * (2*M)
	// active <= N-K
	int D3 = 2 * N + 1;
	// new_sum abs <= 2 * N + 1

	// M = pile size
	// d in [K .. N] stacks
	using dp_t = tensor<num, 3>;
	dp_t dp({2*D1+1, N-K+1, 2*D3+1}, 0);
	dp_t tmp({2*D1+1, N-K+1, 2*D3+1});
	dp[{extra_overall_sum + D1, 0, D3}] = 1;
	for(int place = M-1; place >= 1; place--){
		// process +1/2^k for place+1
		{
			tmp = dp_t({2*D1+1, N-K+1, 2*D3+1}, 0);
			for(int x = 0; x <= 2*D1; x++) for(int y = 0; y <= N-K; y++) for(int z = 0; z <= 2*D3; z++){
				if(dp[{x, y, z}] == 0) continue;
				int overall_sum = x - D1; int active = y; int small_sum = z - D3; num freq = dp[{x, y, z}];
				for(int d = 0; d <= active; d++){
					int new_sum = small_sum + d - (active - d) + extra_sums[place+1];
					if(new_sum % 2 == 0) tmp[{overall_sum + D1, active, new_sum / 2 + D3}] += freq * ncr(active, d);
				}
			}
			dp = tmp;
		}
		// add in things with place decimal points
		// small_sum is over 1/2^place
		for(int sgn : {-1, 1}){
			int del = sgn * (2*(M - place) - 1);
			tmp = dp_t({2*D1+1, N-K+1, 2*D3+1}, 0);
			for(int x = 0; x <= 2*D1; x++) for(int y = 0; y <= N-K; y++) for(int z = 0; z <= 2*D3; z++){
				if(dp[{x, y, z}] == 0) continue;
				int overall_sum = x - D1; int active = y; int small_sum = z - D3; num freq = dp[{x, y, z}];
				for(int cnt = 0; cnt + active <= N-K; cnt++){
					tmp[{overall_sum + del * cnt + D1, active + cnt, small_sum + D3}] += freq * ncr(active + cnt, cnt);
				}
			}
			dp = tmp;
		}
		// at the end: fraction over 1/2^place
	}
	for(int sgn : {-1, 1}){
		int del = sgn * 2 * M;
		tmp = dp_t({2*D1+1, N-K+1, 2*D3+1}, 0);
		for(int x = 0; x <= 2*D1; x++) for(int y = 0; y <= N-K; y++) for(int z = 0; z <= 2*D3; z++){
			if(dp[{x, y, z}] == 0) continue;
			int overall_sum = x - D1; int active = y; int small_sum = z - D3; num freq = dp[{x, y, z}];
			for(int cnt = 0; cnt + active <= N-K; cnt++){
				tmp[{overall_sum + del * cnt + D1, active + cnt, small_sum + D3}] += freq * ncr(active + cnt, cnt);
			}
		}
		dp = tmp;
	}
	vector<num> ans(N-K+1, 0);
	for(int x = 0; x <= 2*D1; x++) for(int y = 0; y <= N-K; y++) for(int z = 0; z <= 2*D3; z++){
		if(dp[{x, y, z}] == 0) continue;
		int overall_sum = x - D1; int active = y; int small_sum = z - D3; num freq = dp[{x, y, z}];
		if(overall_sum + small_sum == 0){
			ans[active] += freq;
		}
	}
	for(int i = K; i <= N; i++){
		cout << ans[i-K] << " \n"[i == N];
	}
}