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
#include <bits/stdc++.h>


using namespace std;

const int MOD = 1'000'000'007;


int addMod(int x, int y) {
    x += y;
    if (x >= MOD) {
        x -= MOD;
    }

    return x;
}

void addModInPlace(int &x, int y) {
    x = addMod(x, y);
}

vector <array<int, 2>> computeDp(const vector <int> &seq) {
    int n = seq.size();
    vector <array<int, 2>> dp(n + 1, {0, 0});

    for (int i = 0; i < n; i++) {
        vector <array<int, 2>> dpNew = dp;

        int x = seq[i];
        for (int depth = 0; depth <= i + 1; depth++) {
            dpNew[depth][x] = 0;

            int depthPrev = depth - (x ? -1 : 1);
            if (depthPrev < 0 || depthPrev > i) {
                continue;
            }

            if (depthPrev == 0) {
                addModInPlace(dpNew[depth][x], 1);
            }

            if (i > 0) {
                for (int prev = 0; prev <= 1; prev++) {
                    addModInPlace(dpNew[depth][x], dp[depthPrev][prev]);
                }
            }
        }

        dp = dpNew;
    }

    return dp;
}

void invert(vector <int> &seq) {
    reverse(seq.begin(), seq.end());
    for (int &x : seq) {
        x = 1 - x;
    }
}

vector <vector<int>> solve(int n, vector <vector<int>> &seqs) {
    vector <bool> allRight(n, true);
    for (int i = 0; i < n; i++) for (int x : seqs[i]) {
        if (!x) {
            allRight[i] = false;
            break;
        }
    }

    vector <vector<array<int, 2>>> dpLeft(n), dpRight(n);

    for (int i = 0; i < n; i++) {
        dpLeft[i] = computeDp(seqs[i]);

        invert(seqs[i]);
        dpRight[i] = computeDp(seqs[i]);
    }

    vector <vector<int>> ans(n, vector <int> (n, 0));
    for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) {
        ans[i][j] = dpLeft[i][0][1];

        if (allRight[i]) {
            addModInPlace(ans[i][j], dpRight[j][0][1]);
        }

        for (int depth = 0; depth < min(dpLeft[i].size(), dpRight[j].size()); depth++) {
            int totalLeft = addMod(dpLeft[i][depth][0], dpLeft[i][depth][1]);

            for (int x = 0; x <= 1; x++) {
                int depthOther = depth + (x ? -1 : 1);

                int sub;
                if (depthOther >= 0 && depthOther < dpLeft[i].size()) {
                    sub = dpLeft[i][depthOther][x];
                } else {
                    sub = 0;
                }

                if (depth == 0 && !allRight[i]) {
                    addModInPlace(sub, MOD - 1);
                }

                int nonExtendable = addMod(totalLeft, MOD - sub);
                ans[i][j] = (ans[i][j] + (long long) nonExtendable * dpRight[j][depth][1-x]) % MOD;
            }
        }
    }

    return ans;
}

int main() {
    ios_base::sync_with_stdio(false);

    int n;
    cin >> n;

    vector <vector<int>> seqs(n);
    for (int i = 0; i < n; i++) {
        string s;
        cin >> s;

        seqs[i].resize(s.size());
        for (int j = 0; j < (int) s.size(); j++) {
            seqs[i][j] = s[j] == 'P';
        }
    }

    auto ans = solve(n, seqs);
    for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) {
        cout << ans[i][j] << " \n"[j == n - 1];
    }

    return 0;
}