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
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

#define LL long long

using namespace std;
constexpr int MAXH = 600;
constexpr int MOD = 1e9 + 7;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;

    vector<string> seq(n);
    for (int i = 0; i < n; ++i) cin >> seq[i];

    vector<vector<int>> prefl(n, vector<int>(MAXH + 1));
    vector<vector<int>> prefr(n, vector<int>(MAXH + 1));
    vector<vector<int>> sufl(n, vector<int>(MAXH + 1));
    vector<vector<int>> sufr(n, vector<int>(MAXH + 1));
    vector<vector<int>> sufwl(n, vector<int>(MAXH + 1));
    vector<vector<int>> sufwr(n, vector<int>(MAXH + 1));
    vector<int> count(n);
    vector<bool> proper(n);

    for (int i = 0; i < n; ++i)
    {
        prefl[i][0] = 1;
        prefr[i][0] = 1;
        sufwl[i][0] = 1;
        sufwr[i][0] = 1;

        for (char c : seq[i])
        {
            if (c == 'L')
            {
                proper[i] = true;
                for (int j = MAXH - 1; j >= 0; --j)
                {
                    prefl[i][j + 1] += prefl[i][j];
                    prefr[i][j + 1] += prefl[i][j];
                    prefl[i][j + 1] %= MOD;
                    prefr[i][j + 1] %= MOD;
                    prefl[i][j] = 0;
                }
            }
            else
            {
                count[i] += prefr[i][1];
                count[i] %= MOD;
                for (int j = 1; j <= MAXH; ++j)
                {
                    prefl[i][j - 1] += prefr[i][j];
                    prefr[i][j - 1] += prefr[i][j];
                    prefl[i][j - 1] %= MOD;
                    prefr[i][j - 1] %= MOD;
                    prefr[i][j] = 0;
                }
            }
        }

        reverse(seq[i].begin(), seq[i].end());

        for (char c : seq[i])
        {
            if (c == 'P')
            {
                for (int j = MAXH - 1; j >= 0; --j)
                {
                    sufwl[i][j + 1] += sufwr[i][j];
                    sufwr[i][j + 1] += sufwr[i][j];
                    sufr[i][j + 1] += sufwr[i][j];
                    sufwl[i][j + 1] %= MOD;
                    sufwr[i][j + 1] %= MOD;
                    sufr[i][j + 1] %= MOD;
                    sufwr[i][j] = 0;
                }
            }
            else
            {
                for (int j = 1; j <= MAXH; ++j)
                {
                    sufwl[i][j - 1] += sufwl[i][j];
                    sufwr[i][j - 1] += sufwl[i][j];
                    sufl[i][j - 1] += sufwl[i][j];
                    sufwl[i][j - 1] %= MOD;
                    sufwr[i][j - 1] %= MOD;
                    sufl[i][j - 1] %= MOD;
                    sufwl[i][j] = 0;
                }
            }
        }
    }

    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < n; ++j)
        {
            if (!proper[i])
            {
                cout << sufl[j][0] << " ";
                continue;
            }

            LL res = count[i];
            for (int h = 0; h <= MAXH; ++h)
            {
                res += (LL)prefl[i][h] * (LL)sufl[j][h];
                res %= MOD;
                res += (LL)prefr[i][h] * (LL)sufr[j][h];
                res %= MOD;
            }

            cout << res << " ";
        }
        cout << "\n";
    }
}