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
#include <bits/stdc++.h>
#define ll long long
using namespace std;
constexpr int MOD = 1'000'000'007;
string s[605];

int consts_l[605][605];
int consts_p[605][605];
int first_l[605][605];
int first_p[605][605];
int base_res[605];

int dp[605][605], consts[605][605];

int next_l[605], next_p[605];

int preprocess(int i) {
    string dribble = s[i];
    int n = dribble.size();

    int last_l = n + 1, last_p = n + 1;
    for (int last = n; last >= 1; last--) {
        next_l[last] = last_l;
        next_p[last] = last_p;
        if (dribble[last - 1] == 'L')
            last_l = last;
        else
            last_p = last;
    }

    next_l[0] = last_l, next_p[0] = last_p;

    for (int open = 0; open <= n + 1; open++) dp[n + 1][open] = 0;
    for (int last = n; last >= 0; last--) {
        for (int open = 0; open <= n; open++) {
            dp[last][open] = (open == 0);

            // Próbujemy przedłużyć o "("
            int next_last = next_l[last];
            if (next_last <= dribble.size()) {
                dp[last][open] += dp[next_last][open + 1];
                if (dp[last][open] >= MOD) dp[last][open] -= MOD;
            }

            // Prójbujemy przedłużyć o ")"
            if (open > 0) {
                next_last = next_p[last];
                if (next_last <= dribble.size()) {
                    dp[last][open] += dp[next_last][open - 1];
                    if (dp[last][open] >= MOD) dp[last][open] -= MOD;
                }
            }
        }
    }

    // Wyliczamy śmieszne stałe.
    for (int last = 0; last <= n; last++) {
        for (int open = 0; open <= n; open++) {
            consts[last][open] = 0;
        }
    }
    consts[0][0] = 1;
    for (int last = 0; last <= n; last++) {
        for (int open = 0; open <= n; open++) {
            // Próbujemy przedłużyć o "("
            int next_last = next_l[last];
            if (next_last <= dribble.size()) {
                consts[next_last][open + 1] += consts[last][open];
                if (consts[next_last][open + 1] >= MOD) consts[next_last][open + 1] -= MOD;
            } else {
                consts_l[i][open + 1] += consts[last][open];
                if (consts_l[i][open + 1] >= MOD) consts_l[i][open + 1] -= MOD;
            }

            // Prójbujemy przedłużyć o ")"
            if (open > 0) {
                next_last = next_p[last];
                if (next_last <= dribble.size()) {
                    consts[next_last][open - 1] += consts[last][open];
                    if (consts[next_last][open - 1] >= MOD) consts[next_last][open - 1] -= MOD;
                } else {
                    consts_p[i][open - 1] += consts[last][open];
                    if (consts_p[i][open - 1] >= MOD) consts_p[i][open - 1] -= MOD;
                }
            }
        }
    }

    base_res[i] = (dp[0][0] - 1 + MOD) % MOD;
    for (int last = 1; last <= n; last++) {
        if (dribble[last - 1] == 'L') {
            for (int j = 0; j <= n; j++) first_l[i][j] = dp[last][j];
            break;
        }
    }

    for (int last = 1; last <= n; last++) {
        if (dribble[last - 1] == 'P') {
            for (int j = 0; j <= n; j++) first_p[i][j] = dp[last][j];
            break;
        }
    }

    return (dp[0][0] - 1 + MOD) % MOD;
}

int solve(int i, int j) {
    int res = base_res[i];
    int n = s[i].size();
    for (int open = 0; open <= n; open++) {
        res += 1LL * consts_l[i][open] * first_l[j][open] % MOD;
        if (res >= MOD) res -= MOD;

        res += 1LL * consts_p[i][open] * first_p[j][open] % MOD;
        if (res >= MOD) res -= MOD;
    }

    return res;
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> s[i];
        preprocess(i);
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cout << solve(i, j) << " ";
        }
        cout << "\n";
    }
}