#include <bits/stdc++.h>
using namespace std;
const long long P = 998244353;
const int N = 1e6 + 5;
long long n;
long long fac[N], rev[N];
long long POW(long long a, long long n) {
  long long b = 1;
  while (n) {
    if (n & 1) {
      (b *= a) %= P;
    }
    (a *= a) %= P;
    n /= 2;
  }
  return b;
}
long long C(int n, int k) {
  if (k == 0 || k == n) {
    return 1ll;
  } else {
    return fac[n] * rev[k] % P * rev[n - k] % P;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  fac[0] = 1;
  for (int i = 1; i <= n; i++) {
    fac[i] = fac[i - 1] * i % P;
    rev[i] = POW(fac[i], P - 2);
  }
  long long ans1 = 0, ans2 = 0;
  for (int i = 0; i < n; i++) {
    int f = i & 1 ? 1 : -1;
    ans1 =
        ((ans1 +
          f * C(n, i) *
              (((POW(1 - POW(3, i) + P, n) - POW(-POW(3, i) + P, n)) % P + P) %
               P) %
              P) %
             P +
         P) %
        P;
    f *= -1;
    ans2 = ((ans2 + f * C(n, i + 1) * POW(3, i + 1) % P *
                        POW(3, n * (n - i - 1)) % P) %
                P +
            P) %
           P;
  }
  (ans1 *= 3) %= P;
  (ans2 *= 2) %= P;
  cout << (ans1 + ans2) % P << endl;
  return 0;
}
