#include <bits/stdc++.h>
using namespace std;
long long tb[1000005], inv[1000005], invtb[1000005];
void getA() {
  tb[0] = tb[1] = inv[0] = inv[1] = invtb[0] = invtb[1] = 1;
  for (long long i = 2; i <= 1000005 - 1; i++) {
    tb[i] = tb[i - 1] * i % 998244353;
    inv[i] = (998244353 - 998244353 / i) * inv[998244353 % i] % 998244353;
    invtb[i] = invtb[i - 1] * inv[i] % 998244353;
  }
}
long long C(long long d, long long u) {
  return tb[d] * invtb[d - u] % 998244353 * invtb[u] % 998244353;
}
long long qpow(long long a, long long b) {
  long long res = 1;
  a %= 998244353;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % 998244353;
    a = a * a % 998244353;
  }
  return res;
}
long long bpow(long long a, long long b) {
  long long res = 1;
  for (; b; b >>= 1, a = a * a % 998244353)
    if (b & 1) res = res * a % 998244353;
  return res;
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int t = 1;
  getA();
  while (t--) {
    long long n;
    cin >> n;
    long long a1 = 0, a2 = 0;
    for (long long i = 1; i <= n; i++) {
      long long x = qpow(3, n - i);
      long long tmp =
          (998244353 + qpow(qpow(3, n - i) - 1, n) - qpow(qpow(3, n - i), n)) %
          998244353;
      (a1 += C(n, i) * tmp % 998244353 * (i % 2 ? 1 : -1) + 998244353) %=
          998244353;
    }
    a1 = a1 * 3 % 998244353;
    for (long long i = 1; i <= n; i++) {
      long long tmp = qpow(3, i + n * (n - i));
      (a2 += C(n, i) * tmp % 998244353 * (i % 2 ? 1 : -1) + 998244353) %=
          998244353;
    }
    a2 = a2 * 2 % 998244353;
    cout << (a1 + a2 + 998244353) % 998244353 << '\n';
  }
  return 0;
}
