void main() { runSolver(); } void problem() { auto H = scan!int; auto W = scan!int; auto G = scan!string(H); enum long MOD = 10^^9 + 7; auto solve() { alias V = long[string]; V[][] dp; foreach(i; 0..H) { V[] v; v.length = W; dp ~= v; } dp[0][0][G[0][0..1]] = 1; const stepMax = W+H-1; const stepHalf = stepMax / 2; foreach(step; 1..W+H-1) { foreach(x; 0..W) { const y = step - x; if (y >= H || y < 0) continue; // [x, y].deb; foreach(pre; [[x - 1, y], [x, y - 1]]) { if (pre.minElement < 0) continue; if (step < stepHalf) { foreach(k, v; dp[pre[1]][pre[0]]) { dp[y][x][k ~ G[y][x]] += v; dp[y][x][k ~ G[y][x]] %= MOD; } } else if(stepMax % 2 == 1 && step == stepHalf) { foreach(k, v; dp[pre[1]][pre[0]]) { dp[y][x][k] += v; dp[y][x][k] %= MOD; } } else { foreach(k, v; dp[pre[1]][pre[0]]) { if (k[$ - 1] != G[y][x]) continue; dp[y][x][k[0..$ - 1]] += v; dp[y][x][k[0..$ - 1]] %= MOD; } } } } } return dp[$ - 1][$ - 1].values.sum; } outputForAtCoder(&solve); } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric, std.traits, std.functional, std.bigint, std.datetime.stopwatch, core.time, core.bitop; T[][] combinations(T)(T[] s, in long m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(long n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); Point invert(Point p) { return Point(p.y, p.x); } long[] divisors(long n) { long[] ret; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { ret ~= i; if (i * i != n) ret ~= n / i; } } return ret.sort.array; } bool chmin(T)(ref T a, T b) { if (b < a) { a = b; return true; } else return false; } bool chmax(T)(ref T a, T b) { if (b > a) { a = b; return true; } else return false; } string charSort(alias S = "a < b")(string s) { return (cast(char[])((cast(byte[])s).sort!S.array)).to!string; } ulong comb(ulong a, ulong b) { if (b == 0) {return 1;}else{return comb(a - 1, b - 1) * a / b;}} string toAnswerString(R)(R r) { return r.map!"a.to!string".joiner(" ").array.to!string; } struct ModInt(uint MD) if (MD < int.max) {ulong v;this(string v) {this(v.to!long);}this(int v) {this(long(v));}this(long v) {this.v = (v%MD+MD)%MD;}void opAssign(long t) {v = (t%MD+MD)%MD;}static auto normS(ulong x) {return (x>>".writefln(benchmark!problem(1)); BORDER.writeln; } } else problem(); } enum YESNO = [true: "Yes", false: "No"]; // -----------------------------------------------